Dryioc and Microsoft Dependency injection with servicekey #626
Replies: 2 comments 33 replies
-
I would avoid this kind of context based resolutions:
Here, you are actually resolving the parameter value of a specific key as a resolution root and then using it for injection. Instead, it is much simpler to configure the injection of parameter builder.Register<Foo>(made: Parameters.Of.Name("erpData", serviceKey: "local")); If you need the Runtime based condition (do you really?), then add 2 registrations and selective condition: builder.Register<Foo>(made: Parameters.Of.Name("erpData", serviceKey: "local"), setup: Setup.With(condition: _ => env.IsDev));
builder.Register<Foo>(made: Parameters.Of.Name("erpData", serviceKey: "prod"), setup: Setup.With(condition: _ => env.IsProd)); |
Beta Was this translation helpful? Give feedback.
-
The support for keyed services for MS.DI is not yet released. But it only matters if the client calls your code expecting a keyed service. If you register via DryIoc container and not via the ServiceCollection - it should work. So to undertstand your particular issue I need more context. |
Beta Was this translation helpful? Give feedback.
-
I have a Dryioc container into which I register a number of components. One of these registrations uses a servicekey lookup to resolve a dependency at runtime.
builder.Register<IErpData, ErpData>(Reuse.Singleton, serviceKey: "local");
var erpAccessParams = Parameters.Of
.Name("erpData", (request) =>
{
// Perform late lookup to determine which concrete instance to inject
var erpAccess = builder.Resolve(serviceKey:"local");
return erpAccess;
});
builder.Register(made: Made.Of(parameters: erpAccessParams));
A MS IHostBuilder instance is used to set up the initial container definition for MS registrations via a call to IHostBuilder.UseServiceProviderFactory(new DryIocServiceProviderFactory());
The odd behaviour I'm seeing is that the Parameters.Of.Name lambda is never called to revolve the constructor argument in some situations.
If I use the following:
builder.RegisterDelegate((context) =>
{
return context.Resolve();
});
builder.Register();
Then the resolution works. It seems to be an issue on matching parameters. How does this constructor parameter selection process work in combination with MS DI container?
Beta Was this translation helpful? Give feedback.
All reactions