You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassDryExamples{[Fact]// This works ok.publicvoidTransferMultipleThenResolveEnumerable(){varservices=new ServiceCollection();
services.AddScoped<IPrinter,Printer>();
services.AddScoped<IPrinter,PrinterA>();
services.AddScoped<IPrinter,PrinterB>();
services.AddScoped<IPrinter,NeighborPrinter>();varspf=new DryIocServiceProviderFactory();vardryContainer= spf.CreateBuilder(services);varmsContainer= dryContainer.GetServiceProvider();
Assert.Equal(
dryContainer.Resolve<IEnumerable<IPrinter>>().Count(),
msContainer.GetRequiredService<IEnumerable<IPrinter>>().Count());}[Fact]// I have not been able to get this to work.publicvoidTransferMultipleThenResolveEnumerableFromChild(){varservices=new ServiceCollection();
services.AddScoped<IPrinter,Printer>();
services.AddScoped<IPrinter,PrinterA>();
services.AddScoped<IPrinter,PrinterB>();
services.AddScoped<IPrinter,NeighborPrinter>();varspf=new DryIocServiceProviderFactory();varrootContainer= spf.CreateBuilder(new ServiceCollection());varchildContainer= rootContainer.CreateChild(RegistrySharing.Share,"child-stamp", IfAlreadyRegistered.AppendNewImplementation);//childContainer.Populate(services);foreach(var service in services){
childContainer.RegisterDescriptor(service, IfAlreadyRegistered.AppendNewImplementation,"child-stamp");}varmsContainer= childContainer.GetServiceProvider();
Assert.Equal(
childContainer.Resolve<IEnumerable<IPrinter>>().Count(),
msContainer.GetRequiredService<IEnumerable<IPrinter>>().Count());}}privateinterfaceIPrinter{}privateclassPrinter:IPrinter{}privateclassPrinterA:IPrinter{}privateclassPrinterB:IPrinter{}privateclassNeighborPrinter:IPrinter{}
The first test works, and I expect to be able to do similar with a child container. However, the fact that the child service has a default key seems to break this expectation.
childContainer.Populate(services); hard-codes append-if-not-keyed, so I tried calling childContainer.RegisterDescriptor in the loop, but discovered that AppendNewImplementation and AppendNotKeyed are basically not allowed in Regsitry.WithKeyedService(...).
Functionally, I'm looking to move away from a setup with unity container that has child containers. ie child containers can resolve from parent, but cannot interfere with parent registrations. Parent cannot see child services/registrations.
Please let me know if this is achievable.
The text was updated successfully, but these errors were encountered:
The first test works, and I expect to be able to do similar with a child container. However, the fact that the child service has a default key seems to break this expectation.
childContainer.Populate(services);
hard-codes append-if-not-keyed, so I tried callingchildContainer.RegisterDescriptor
in the loop, but discovered thatAppendNewImplementation
andAppendNotKeyed
are basically not allowed inRegsitry.WithKeyedService(...)
.Functionally, I'm looking to move away from a setup with unity container that has child containers. ie child containers can resolve from parent, but cannot interfere with parent registrations. Parent cannot see child services/registrations.
Please let me know if this is achievable.
The text was updated successfully, but these errors were encountered: