/// <summary> /// Use this method to pass your custom pre-configured container to the `IHostBuilder.UseServiceProviderFactory` in "Program.cs" /// </summary> public static 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) );
public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(new WebApplicationOptions { Args = args, ContentRootPath = Directory.GetCurrentDirectory(), // etc. }); // ## Dependency Injection stuff // ----------------------------------------------------------- var container = new Container(rules => // optional: Enables property injection for Controllers rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Properties()(request) : null)); container.RegisterMyBusinessLogic(); // Here it goes the integration with the existing DryIoc container var diFactory = new DryIocServiceProviderFactory(container, ExampleOfCustomRegisterDescriptor); builder.Host.UseServiceProviderFactory(diFactory); builder.Services .AddMvc(options => options.EnableEndpointRouting = false) .AddControllersAsServices(); // other things... builder.Logging.ClearProviders(); builder.Logging.AddConsole(); //----------------------------------------------------------- var app = builder.Build(); app.UseMvc(); app.UseHttpsRedirection(); app.UseAuthorization(); app.Run(); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public IServiceProvider ConfigureServices(IServiceCollection services) { services .AddMvc() // Enables controllers to be resolved by DryIoc, OTHERWISE resolved by infrastructure .AddControllersAsServices(); return(new Container(rules => rules.With( // optional: Enables property injection for Controllers // NOTE: it will be overriden by MEF convention (ImportAttribute) with the `WithMef()` line below . propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Properties()(request) : null)) .WithMef() // optional: support for MEF service discovery .WithDependencyInjectionAdapter(services, // optional: get original DryIoc.ContainerException if specified type is not resolved, // and prevent fallback to default resolution by infrastructure throwIfUnresolved: type => type.Name.EndsWith("Controller"), // optional: You may Log or Customize the infrastructure components registrations registerDescriptor: (registrator, descriptor) => { #if DEBUG if (descriptor.ServiceType == typeof(ILoggerFactory)) { Console.WriteLine($"Logger factory is registered as instance: {descriptor.ImplementationInstance != null}"); } #endif return false; // fallback to default registration logic }) // Your registrations are defined in CompositionRoot class .ConfigureServiceProvider <CompositionRoot>()); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public IServiceProvider ConfigureServices(IServiceCollection services) { services .AddLogging(logging => logging.AddConsole()) .AddMvc() // Enables controllers to be resolved by DryIoc, OTHERWISE resolved by infrastructure .AddControllersAsServices(); // Container in V4 is directly implementing `IServiceProvider`, so it is fine to return it. return(new Container(rules => // optional: Enables property injection for Controllers // In current setup `WithMef` it will be overriden by properties marked with `ImportAttribute` rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Properties()(request) : null) ) // optional: support for MEF Exported services .WithMef() .WithDependencyInjectionAdapter(services, // optional: You may Log or Customize the infrastructure components registrations MyCustomRegisterDescriptor) // Your registrations are defined in CompositionRoot class .WithCompositionRoot <MyCompositionRoot>()); }
public static IContainer CreateConfiguredContainer() => new Container(rules => rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Properties()(request) : null) );