I have the following Unity configuration section
<register type="IDbContext" mapTo="ProjectEntities" name="ProjectEntitiesContext" >
<constructor>
<param name="connectionString" value="conString" />
</constructor>
Which works great. I get the value of "conString" in the connectionString parameter of my ProjectEntities class.
However, what I would like to do is change the value of the conString parameter in code.
some thing like
myIocContaniner.registratons.ProjectEntities.Constructor.value = "Different Connection String"
And have "Different Connection String" passed to the ProjectEntities constructor when its created.
The problem I am trying to solve is making a .net WebApi interact with different data bases based on the request.
I'm assuming you resolve your IDbContext every time you use it (something like this)?
IDbContext myDbContext = myIocContainer.Resolve<IDbContext>("ProjectEntitiesContext");
// Some code here using myDbContext
You can just remap the type in Unity.
myIocContainer.RegisterType<IDbContext, ProjectEntities>("ProjectEntitiesContext", new InjectionConstructor("Different Connection String"));
The next time it resolves it, it'll be the new mapped type with the new injected constructor parameter.
This is also assuming you only have the connection string as a parameter to your constructor. If you have other parameters, you'll need to adjust the InjectionConstructor accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.