If you want to set some pre-configred settings just follow the steps:
Implementation
First, define a method with your custom setting.
1 2 3 4 5 6 7 8 9 10 11 12 13
// Startup.cs
///<summary> /// Use this method to pass your custom pre-configured container to the `IHostBuilderUseServiceProviderFactory` in "Program.cs" ///</summary> publicstatic IContainer CreateMyPreConfiguredContainer() => // This is an example configuration, // for possible options check the https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/RulesAndDefaultConventions.md new Container(rules => // Configures property injection for Controllers, ensure that you've added `AddControllersAsServices` in `ConfigureServices` rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Properties()(request) : null) );
// Plug-in the DryIoc // CreateMyPreConfiguredContainer method is the key. .UseServiceProviderFactory(new DryIocServiceProviderFactory(Startup.CreateMyPreConfiguredContainer()))
// Or // .UseDryIoc(Startup.CreateMyPreConfiguredContainer())
// uses DI to construct the controllers - required for the DryIoc diagnostics, property injection, etc. to work; mvcBuilder.AddControllersAsServices(); } ///<summary> /// Use this method to pass your custom pre-configured container to the `IHostBuilder.UseServiceProviderFactory` in "Program.cs" ///</summary> publicstatic IContainer CreateMyPreConfiguredContainer() => // This is an example configuration, // for possible options check the https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/RulesAndDefaultConventions.md new Container(rules => // Configures property injection for Controllers, ensure that you've added `AddControllersAsServices` in `ConfigureServices` rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Properties()(request) : null) );
// The most important conventional method to enable third-party IoCs. publicvoidConfigureContainer(IContainer container) { // You may place your registrations here or split them in different classes, or organize them in some kind of modules, e.g: BasicServicesRegistrator.Register(container); SpecialServicesRegistrator.Register(container); // NOTE: // Don't configure the container rules here because DryIoc uses the immutable container/configuration // and you customized container will be lost. // Instead you may use something like `CreateMyPreConfiguredContainer` above. }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. publicvoidConfigure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { var logger = loggerFactory.CreateLogger<Startup>();
// All DryIoc Container interfaces are available through the MS.DI services var container = app.ApplicationServices.GetRequiredService<IContainer>(); logger.LogInformation($"You may use the DryIoc container here: '{container}'");