New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using AllowOverridingRegistrations = true together with RegisterConditional #653

Open
khodaie opened this Issue Jan 12, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@khodaie
Copy link

khodaie commented Jan 12, 2019

I have a service container that includes conditional registrations and I want my registrations be overridable using some dynamic plugins. In SimpleInjector when the AllowOverridingRegistrations is set to true, conditional registrations are prohibited with this exception message:

The making of conditional registrations is not supported when AllowOverridingRegistrations is set, because it is impossible for the container to detect whether the registration should replace a different registration or not

But is this workaround correct?

container.Options.AllowOverridingRegistrations = false;
// Register conditional services:
this.Container.RegisterConditional(typeof(ILogger<>), typeof(NLogProxy<>), Lifestyle.Singleton,
    c => !c.Handled);

// Registering a service that can be overridden:
container.Register<IMyService,MockService>(Lifestyle.Singleton);
...

// Register or override plugin services:
container.Options.AllowOverridingRegistrations = true;
RegisterPluginServices(container); // May override some registrations.

For example, a plugin can override the IMyService with its own implementation:

container.Register<IMyService,PluginService>(Lifestyle.Singleton);
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

dotnetjunkie commented Jan 12, 2019

I'm not sure I do understand what your workaround actually is.

@khodaie

This comment has been minimized.

Copy link
Author

khodaie commented Jan 12, 2019

I'm not sure I do understand what your workaround actually is.

First, I disable the AllowOverridingRegistrations and register using the RegisterConditional. Then, I enable the AllowOverridingRegistrations and register the overiden services:

container.Options.AllowOverridingRegistrations = false;
RegisterServices(container); // Including conditional registrations
container.Options.AllowOverridingRegistrations = true;
RegisterPluginServices(container); // May include service overriding.
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

dotnetjunkie commented Jan 12, 2019

But is this workaround correct?

I can't really say as you don't actually show the registations. You just show the call to RegisterPluginServices. But if this works for you, in that case the workaround is correct.

@khodaie

This comment has been minimized.

Copy link
Author

khodaie commented Jan 12, 2019

I can't really say as you don't actually show the registations. You just show the call to RegisterPluginServices. But if this works for you, in that case the workaround is correct.

Thanks for your response.
The RegisterPluginServices registers services dynamically from the plugin assemblies. But, for simplifying the issue:

container.Options.AllowOverridingRegistrations = false;
container.RegisterConditional(typeof(ILogger<>), typeof(NLogProxy<>), Lifestyle.Singleton,
    c => !c.Handled);
container.Register<IMyService, MockService>(Lifestyle.Singleton);

container.Options.AllowOverridingRegistrations = true;
container.Register<IMyService, RealService>(Lifestyle.Singleton);
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

dotnetjunkie commented Jan 12, 2019

Ah, so your plugins are not registering anything that is conditional. In that case, this will work as expected, and intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment