I have a .NET 3.5 assembly, a DAL, that connects to a database through Linq2SQL. I deploy this assembly in the GAC as it can be used by multiple business layers.
The question is: in a dev environme开发者_如何学JAVAnt I have a connection string different than the one in the production environment. Before deploying the assembly to the prod GAC I need to recompile it with the appropriate connection string.
Is there any way to allow deploying the assembly to the GAC independently of the connection string, being that info read from some config?
It doesn't matter whether you deploy the assembly to the GAC or in the bin folder of a website, the config of the application that uses the assembly is the one that overrides the connection in the LinqToSQL classes.
You should pass this connection string in from the config when you create the data context
Does the DAL have the connection string hardcoded somewhere? If so, that's not the best option for configurable deployment. If you have access to the DAL code, refactor it so that it takes the connection string as a parameter on construction of any of its child classes. Then, any consuming application can use the connectionStrings
section of their configuration file to store the connection string. This way, you can change the connection string whenever you like.
You should have your connection strings stored in your web/app.config file, and reference that config setting in your assembly.
Connection strings should not be hard coded, and config
files have an existing connectionStrings
section for this purpose.
If you need to secure your connection string, you can encrypt this section, but you shouldn't rely on it being in the GAC as protection, as it is easy to use ILDASM to see all the strings in an assembly (including connection strings).
IMO, I think it is a mistake to embed an environment specific value like a connection string into a GAC assembly (or any assembly for that matter). Instead, the component in the GAC should allow for having the connection string passed to it.
Consider updating your MACHINE.CONFIG file to add your connection string or other config section. This will inherit to all .NET apps.
精彩评论