dependency-injection

Better way of checking null in dependency injection

我是研究僧i 提交于 2020-08-27 08:30:21
问题 When using dependency injection through a constructor I always need to check for nulls before passing the instance to an internal property. e.g. public UserManager(User user, IStateManager stateManager) { if(user == null) throw new ArgumentNullException; if(statemanager == null) throw new ArgumentNullException("stateManager"); _user = user; _stateManager = statemanager; } repeating this pattern on every controller / class seems repetitive. Is there a better way to handle this? btw different

Using Autofac with ASP.Net Core 3.1 generic host “Worker Service” application

不问归期 提交于 2020-08-25 04:21:50
问题 In an ASP.Net Core application, its easy to configure Autofac using: public class Program { public static void Main(string[] args) { // ASP.NET Core 3.0+: // The UseServiceProviderFactory call attaches the // Autofac provider to the generic hosting mechanism. var host = Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration()

Using Autofac with ASP.Net Core 3.1 generic host “Worker Service” application

烈酒焚心 提交于 2020-08-25 04:20:27
问题 In an ASP.Net Core application, its easy to configure Autofac using: public class Program { public static void Main(string[] args) { // ASP.NET Core 3.0+: // The UseServiceProviderFactory call attaches the // Autofac provider to the generic hosting mechanism. var host = Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration()

Using Autofac with ASP.Net Core 3.1 generic host “Worker Service” application

两盒软妹~` 提交于 2020-08-25 04:19:08
问题 In an ASP.Net Core application, its easy to configure Autofac using: public class Program { public static void Main(string[] args) { // ASP.NET Core 3.0+: // The UseServiceProviderFactory call attaches the // Autofac provider to the generic hosting mechanism. var host = Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration()

How to modify IConfiguration natively injected in Azure Functions

China☆狼群 提交于 2020-08-20 04:38:06
问题 We need to add configuration providers to the native IConfiguration that is supplied to the Azure Functions natively. Currently we are completely replacing it with our custom Iconfiguration using the following code: public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { ... var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddAzureKeyVault(...) .AddJsonFile("local.settings.json", true, true)

How to resolve a generic type service from the DependencyInjection at Asp.net Core runtime

狂风中的少年 提交于 2020-08-11 04:51:34
问题 I am dealing with a WeChat project that post XML message to my server. The message could be any one of several types. Hence I first deserialize the message to corresponding object with a base "WxMessage", and then return the object to a dispatcher, which will find out a correct message handler to handle the message. The handlers, corresponding to each type of messages, were registered to the DependencyInjection of Asp.net core 2.1 with a IWxMessageHandler<> interface. services.AddScoped

How to prevent Hilt from picking dependency from a library?

a 夏天 提交于 2020-08-09 08:17:06
问题 Okay, let's make this simple. I have created a simple library called my-network-library with two classes in it. First one is a Hilt module called BaseNetworkModule @Module @InstallIn(ApplicationComponent::class) object BaseNetworkModule { // Client @Singleton @Provides fun provideOkHttpClient(): OkHttpClient { return OkHttpClient.Builder() // my default okhttp setup goes here .build() } } and the second one is a simple class. class MyAwesomeClass { fun doMagic() { // magic code goes here } }

Should Class Implement Both IAsyncDisposable and IDisposable?

妖精的绣舞 提交于 2020-08-08 06:38:07
问题 For example I have this class: public class UnitOfWork : IUnitOfWork { private readonly ApplicationDbContext _context; public IProductRepository Products { get; private set; } public ICategoryRepository Categories { get; private set; } public IPhotoRepository Photos { get; private set; } public UnitOfWork(ApplicationDbContext context, IProductRepository productRepository, ICategoryRepository categoryRepository, IPhotoRepository photoRepository) { _context = context; Products =

What's the difference between the ApplicationServices (root) IServiceProvider and an injected IServiceProvider?

。_饼干妹妹 提交于 2020-08-07 05:34:43
问题 I have a hard time understanding why the behaviour is different to apply the service locator pattern in the Startup Configure method when using the IApplicationBuilder.ApplicationServices in stead of IServiceProvider . When I use IApplicationBuilder.ApplicationServices (which sets the IServiceProvider that provides access to the application 's service container ) I get the following error: Cannot resolve scoped service '...' from root provider In contrast, when I use the injected

What's the difference between the ApplicationServices (root) IServiceProvider and an injected IServiceProvider?

China☆狼群 提交于 2020-08-07 05:33:13
问题 I have a hard time understanding why the behaviour is different to apply the service locator pattern in the Startup Configure method when using the IApplicationBuilder.ApplicationServices in stead of IServiceProvider . When I use IApplicationBuilder.ApplicationServices (which sets the IServiceProvider that provides access to the application 's service container ) I get the following error: Cannot resolve scoped service '...' from root provider In contrast, when I use the injected