Hi Abp Zero,
We're using .net Core 2.1 and Angular 8.
For certain reason I would like to overwrite MultiTenantLocalizationSource with my own CustomMultiTenantLocalizationSource.
I tried to use following code in the PreInitialize() in ProjectCoreModule but it didn't work as other replaced services:
Configuration.ReplaceService<IMultiTenantLocalizationSource, CustomMultiTenantLocalizationSource>(DependencyLifeStyle.Transient);
Could you please advise how I coud replace the IMultiTenantLocalizationSource service?
Thank you
6 Answer(s)
-
0
IMultiTenantLocalizationSource
is not injected anywhere, so you can't replace its implementation that way.MultiTenantLocalizationSource
is instantiated directly inEnableDbLocalization
in LanguageManagementConfig.cs#L40.You can comment out the following line in your
WebCoreModule
and instantiate your own implementation.//Use database for language management Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
-
0
Thank you Aaron.
Hmm... then could I do this? Not sure if ILanguageManagementConfig is injected...
Configuration.ReplaceService<ILanguageManagementConfig, CustomLanguageManagementConfig>(); Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
Thank you, Robin
-
0
Hi Robin, it is injected and you could do that.
-
0
Thank you Aaron. But somehow it's still not replace the service and going into the replaced implementation.
EnableDbLocalization() is still calling the default one instead of the CustomLanguageManagementConfig.EnableDbLocalization(). Seems LanguageManagementConfig has been initialized together with Configuration.Modules before everything else.
== Basically I had to use:
Configuration.ReplaceService<ILanguageManagementConfig, CustomLanguageManagementConfig>(); (new CustomLanguageManagementConfig(Configuration.IocManager, Configuration)).EnableDbLocalization();
Is there any concern of calling customized EnableDbLocalization() in this way?
-
0
I would recommend Option 1.2 below.
Option 1: Register custom
ILanguageManagementConfig
Seems LanguageManagementConfig has been initialized together with Configuration.Modules before everything else.
Good conjecture. More accurately,
ILanguageManagementConfig
is instantiated on the first call toConfiguration.Modules.Zero()
.Configuration.Modules.Zero()
is first called inYourProjectCoreModule.PreInitialize
.ReplaceService
only runs inAbpKernalModule.Initialize
, before other modules'Initialize
.
Therefore, calling
ReplaceService
for this inYourProjectWebCoreModule.PreInitialize
is too late.Option 1.1: Directly register as default before
ILanguageManagementConfig
is instantiatedYou can directly register
CustomLanguageManagementConfig
as the default implementation before that first call.IocManager.IocContainer.Register( // Add this Component.For<ILanguageManagementConfig>() // Add this .ImplementedBy<CustomLanguageManagementConfig>() // Add this .IsDefault() // Add this ); // Add this //Declare entity types // Existing code Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant); // Existing code
Option 1.2: Add service before
ILanguageManagementConfig
is registeredYou can
AddSingleton
toIServiceCollection
in yourWeb
project'sConfigureServices
.services.AddSingleton<ILanguageManagementConfig, CustomLanguageManagementConfig>();
Option 2: Instantiate custom
ILanguageManagementConfig
without replacingIs there any concern of calling customized EnableDbLocalization() in this way?
In general, it is not preferable since
Configuration.Modules.Zero().LanguageManagement
would have the unexpected implementation if used elsewhere — but it's not used elsewhere.Therefore, in this case, there is no concern.
-
0
Thank you Aaron! That's very detailed and helpful. Wondering why I forgot the IsDefault() at the first place. It's working now!