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
Replace default/existing services by custom implementations #983
Comments
Registrations are done in module's Initialize. You can register in PreInitialize to override it. But I will consider to add some 'replace' logic to simplify it. |
+1 |
Hi, I added ReplaceService method to startup configuration. For example, we can use such a code to replace IConnectionStringResolver with our custom implementation DbPerTenantConnectionStringResolver: Configuration.ReplaceService(typeof(IConnectionStringResolver), () =>
{
IocManager.Register<IConnectionStringResolver, DbPerTenantConnectionStringResolver>(DependencyLifeStyle.Transient);
}); We should write this in PreInitialize event of our module. Same service can be replaced/overrided multiple times. Last one will be valid. If Module B depends on Module A and both overrides same service, then the one overrided by module B will be valid sinde Modul B's PreInitialize runs after Module A. I made second parameter an action, instead of simply a type. Because, there may be complex replace scenarios. Example: Configuration.ReplaceService(typeof(IConnectionStringResolver), () =>
{
IocManager.IocContainer.Register(
Component.For<IConnectionStringResolver, IDbPerTenantConnectionStringResolver>()
.ImplementedBy<DbPerTenantConnectionStringResolver>()
.LifestyleTransient()
);
}); This is a real code from module zero that replaces IConnectionStringResolver (https://github.com/aspnetboilerplate/module-zero/blob/master/src/Abp.Zero.EntityFramework/Zero/EntityFramework/AbpZeroEntityFrameworkModule.cs#L18). Notice that it registers DbPerTenantConnectionStringResolver for two interfaces (IConnectionStringResolver, IDbPerTenantConnectionStringResolver). Also, you may need to use more advanced methods of Castle Windsor here. |
When you update the doco could we also have an example to replace the ILogger interface with say a Serilog.ILogger for instance? ;-) |
Replacing ILogger is a bit different. Because, it's registered before ABP initialized. But it's already registered in template, in global.asax file. You can just remove and register serilog. I did write it before (you can search for it). |
I added shortcut extension methods. Example: Configuration.ReplaceService<IServiceType, MyImplementation>(DependencyLifeStyle.Transient); |
Hello @hikalkan I've reached a situation where I create a custom module with a custom configuration. I would like to allow applications using my custom module to be able to set some property and only if this property is true, I will replace the service. Since App depends on MyCustomModule, I can't rely on PreInitialize to use ReplaceService. AppModule1 - depends on MyCustomModule
CustomModule should use this setting in order to replace a service. This is not working since it is located at Initialize rather than PreInitialize.
Any thoughts? Should I use other approach ? I would like to replace IAbpSession for example if a dependant module enable specific setting. Thanks a lot |
There is a possibility to configure MyCustomModule by AppModule1 before PreInitialize. Please check this: http://www.aspnetboilerplate.com/Pages/Documents/Module-System#DocCustomModule You can inject MyCustomModule into AppModule1's constructor and set a boolean property of it in the constructor. |
…thod to override built-in services.
Sometimes, I want to replace the implement for specific interface,but it seems that
IocManager.Register
only register the implemented class for the first register call.Anyway I finally found a way to do that.
But, it's not convenient.
Can
IIconManager
provide some method likeReplace<>
?The text was updated successfully, but these errors were encountered: