c# - Unity Dependency Injection - How to create the instance to be injected at runtime -
ASP.Net is trying to implement dependency injection in the web API project.
I am able to inject a frequency of The user should create a So I have in my service: And in my application startup, I can do this - where the container is a new This is not good at all because it will use the same code for each user / request etc. If I try to use something like this : ... Where either GetTheUsersID () is required to check a cookie or ASP.NET identity request, this is definitely not available in App Startup. So - where / how (please, in simple terms, this digit is hurting my mind) I apply it so that I instanced You generally do not want to mix behaviors for state and components, which are resolved through the container They should be used for DI - those components can be prepared as pure services. He said, sometimes it is understood to wrap a global or context-specific situation as a component. In your case, if you need a local ID in one or more services locally (in other words, do not pass it from one service to another) you have mentioned that the cookie is userId If possible, you might see something like this: Now you can inject ICookieService where a UserId is required. More complex cases may require an abstract factory: account in some of my services.
account example
Guid and this runtime is not known.
Public Transaction Service (Account ACC) {_account = ACC; }
UnityContainer :
container.RegisterType & Lt; Instanet.Engine.Account & gt; (New injection congestion (New Guid ("xxxxxx"));
container. Registration Type & lt; Instanet.Engine.Account & gt; (New Injection Constructor (GetTheUsersID ());
account can be injected.
Public class cookie service: ICiki service {Public Injects GetCurrentUserId () {// Pseudo Code Returns HttpContext.Current .GetCookie ["User Id "]; }}
Comments
Post a Comment