/// <summary>Adapts passed <paramref name="container"/> to Microsoft.DependencyInjection conventions, /// registers DryIoc implementations of <see cref="IServiceProvider"/> and <see cref="IServiceScopeFactory"/>, /// and returns NEW container. /// </summary> /// <param name="container">Source container to adapt.</param> /// <param name="descriptors">(optional) Specify service descriptors or use <see cref="Populate"/> later.</param> /// <param name="registerDescriptor">(optional) Custom registration action, should return true to skip normal registration.</param> /// <param name="registrySharing">(optional) Use DryIoc <see cref="RegistrySharing"/> capability.</param> /// <example> /// <code><![CDATA[ /// /// var container = new Container(); /// /// // you may register the services here: /// container.Register<IMyService, MyService>(Reuse.Scoped) /// /// // applies the MS.DI rules and registers the infrastructure helpers and service collection to the container /// var adaptedContainer = container.WithDependencyInjectionAdapter(services); /// /// // the container implements IServiceProvider /// IServiceProvider serviceProvider = adaptedContainer; /// ///]]></code> /// </example> /// <remarks>You still need to Dispose adapted container at the end / application shutdown.</remarks> public static IContainer WithDependencyInjectionAdapter( this IContainer container, IEnumerable <ServiceDescriptor> descriptors = null, Func <IRegistrator, ServiceDescriptor, bool> registerDescriptor = null, RegistrySharing registrySharing = RegistrySharing.Share ) { if (container.Rules != Rules.MicrosoftDependencyInjectionRules) { container = container.With( container.Rules.WithMicrosoftDependencyInjectionRules(), container.ScopeContext, registrySharing, container.SingletonScope ); } var capabilities = new DryIocServiceProviderCapabilities(container); var singletons = container.SingletonScope; #if NET6_0_OR_GREATER singletons.Use <IServiceProviderIsService>(capabilities); #endif singletons.Use <ISupportRequiredService>(capabilities); singletons.UseFactory <IServiceScopeFactory>(r => new DryIocServiceScopeFactory(r)); if (descriptors != null) { container.Populate(descriptors, registerDescriptor); } return(container); }
public static IContainer CreateChild(this IContainer container, RegistrySharing registrySharing, object childDefaultServiceKey, IfAlreadyRegistered?ifAlreadyRegistered = null, Rules newRules = null, bool withDisposables = false) { var rules = newRules != null && newRules != container.Rules ? newRules : container.Rules; if (childDefaultServiceKey != null) { rules = rules .WithDefaultRegistrationServiceKey(childDefaultServiceKey) .WithFactorySelector(Rules.SelectKeyedOverDefaultFactory(childDefaultServiceKey)); } if (ifAlreadyRegistered != null) { rules = rules .WithDefaultIfAlreadyRegistered(ifAlreadyRegistered.Value); } return(container.With( container.Parent, rules, container.ScopeContext, registrySharing, container.SingletonScope.Clone(withDisposables), container.CurrentScope?.Clone(withDisposables))); }
/// <summary> /// `container` is the existing container which will be cloned with the MS.DI rules and its cache will be dropped, /// unless the `registrySharing` is set to the `RegistrySharing.Share` or to `RegistrySharing.CloneButKeepCache`. /// `registerDescriptor` is the custom service descriptor handler. /// </summary> public DryIocServiceProviderFactory(IContainer container, RegistrySharing registrySharing, Func <IRegistrator, ServiceDescriptor, bool> registerDescriptor = null) { _container = container; _registrySharing = registrySharing; _registerDescriptor = registerDescriptor; }
public IContainer With(IResolverContext parent, Rules rules, IScopeContext scopeContext, RegistrySharing registrySharing, IScope singletonScope, IScope currentScope) { throw new NotImplementedException(); }
public IContainer With(Rules rules, IScopeContext scopeContext, RegistrySharing registrySharing, IScope singletonScope) { throw new NotImplementedException(); }