AspNetCoreLocalizationSample | example containing different aspects of ASP.NET | Code Editor library
kandi X-RAY | AspNetCoreLocalizationSample Summary
kandi X-RAY | AspNetCoreLocalizationSample Summary
An example containing different aspects of ASP.NET Core Localization, including model binding error messages, validation messages, allowing user to change culture at run-time, ... .
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of AspNetCoreLocalizationSample
AspNetCoreLocalizationSample Key Features
AspNetCoreLocalizationSample Examples and Code Snippets
Community Discussions
Trending Discussions on AspNetCoreLocalizationSample
QUESTION
Sometimes, during service registrations, I need to resolve other (already registered) services from the DI container. With containers like Autofac or DryIoc this was no big deal since you could register the service on one line and on the next line you could immediately resolve it.
But with Microsoft's DI container you need to register the service, then build a service provider and only then you are able resolve the services from that IServiceProvider
instance.
See the accepted answer this SO question: ASP.NET Core Model Binding Error Messages Localization
...ANSWER
Answered 2020-Apr-28 at 09:03Each service provider has its own cache. Building multiple service provider instances can, therefore, lead to a problem called Torn Lifestyles:
When multiple [registrations] with the same lifestyle map to the same component, the component is said to have a torn lifestyle. The component is considered torn because each [registration] will have its own cache of the given component, which can potentially result in multiple instances of the component within a single scope. When the registrations are torn the application may be wired incorrectly which could lead to unexpected behavior.
This means that each service provider will have its own cache of singleton instances. Building multiple service providers from the same source (i.e. from the same service collection) will cause a singleton instance to be created more than once—this breaks the guarantee that there is at most one instance for a given singleton registration.
But there are other, just as subtle bugs that can appear. For instance, when resolving object graphs that contain scoped dependencies. Building a separate temporary service provider for the creation of an object graph that is stored in the next container might cause those scoped dependencies to be kept alive for the duration of the application. This problem is commonly referred to as Captive Dependencies.
With containers like Autofac or DryIoc this was no big deal since you could register the service on one line and on the next line you could immediately resolve it.
This statement implies that there are no problems with trying to resolve instances from the container while the registration phase is still in progress. This, however, is incorrect—altering the container by adding new registrations to it after you already resolved instances is a dangerous practice—it can lead to all sorts of hard to track bugs.
It is especially because of those hard to track bugs that DI Containers, such as Autofac, Simple Injector, and Microsoft.Extensions.DependencyInjection (MS.DI) prevent you from doing this in the first place. Autofac and MS.DI do this by having registrations made in a 'container builder' (AutoFac's ContainerBuilder
and MS.DI's ServiceCollection
). Simple Injector, on the other hand, does not make this split. Instead, it locks the container from any modifications after the first instance is resolved. The effect, however, is similar; it prevents you from adding registrations after you resolve.
The Simple Injector documentation actually contains some decent explanation on why this Register-Resolve-Register pattern is problematic:
Imagine the scenario where you want to replace some
FileLogger
component for a different implementation with the sameILogger
interface. If there’s a component that directly or indirectly depends onILogger
, replacing theILogger
implementation might not work as you would expect. If the consuming component is registered as singleton, for example, the container should guarantee that only one instance of this component will be created. When you are allowed to change the implementation ofILogger
after a singleton instance already holds a reference to the “old” registered implementation the container has two choices—neither of which are correct:
- Return the cached instance of the consuming component that has a reference to the “wrong”
ILogger
implementation.- Create and cache a new instance of that component and, in doing so, break the promise of the type being registered as a singleton and the guarantee that the container will always return the same instance.
For this same reason you see that the ASP.NET Core Startup
class defines two separate phases:
- The “Add” phase (the
ConfigureServices
method), where you add registrations to the “container builder” (a.k.a.IServiceCollection
) - The “Use” phase (the
Configure
method), where you state you want to use MVC by setting up routes. During this phase, theIServiceCollection
has been turned into aIServiceProvider
and those services can even be method injected into theConfigure
method.
The general solution, therefore, is to postpone resolving services (like your IStringLocalizerFactory
) until the “Use” phase, and with it postpone the final configuration of things that depend on the resolving of services.
This, unfortunately, seems to cause a chicken or the egg causality dilemma when it comes to configuring the ModelBindingMessageProvider
because:
- Configuring the
ModelBindingMessageProvider
requires the use of theMvcOptions
class. - The
MvcOptions
class is only available during the “Add” (ConfigureServices
) phase. - During the “Add” phase there is no access to an
IStringLocalizerFactory
and no access to a container or service provider and resolving it can’t be postponed by creating such value using aLazy
. - During the “Use” phase,
IStringLocalizerFactory
is available, but at that point, there is noMvcOptions
any longer that you can use to configure theModelBindingMessageProvider
.
The only way around this impasse is by using private fields inside the Startup
class and use them in the closure of AddOptions
. For instance:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install AspNetCoreLocalizationSample
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page