开发者

How to separate configuration file?

I have a .net solution having a reference hierarchy like this MyWinApp->ServerCore->DataAccess where the last two are class libraries and myWinApp is a windows app.

Now, each time I want to run this project on different servers I need to rebuild the project since I couldn't manage to separate the configuration file(app.config) of DataAccess project that has connection string related configurations.

How can I separate database configurations from the application code? I tried to build action options but it doesn't work :S 开发者_如何学PythonWhat might be the most feasible solution?

Thanks in advance


The configuration should most likely go with the MyWinApp project. The configuration file goes with what is executing. So if you make an installer for your application, it'll have a configuration file called MyWinApp.exe.config that was created from your App.Config.

Basically the app.config with your Datalayer.dll doesn't really do much.

What you might want to do is look at how the configSource property works for configuration files in .net here: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx and see if this is what you're looking for. This lets you set a configSource for your connection strings that you can change per machine.

All I do is setup a simple xcopy before a deploy and I have the correct configuration settings before deploying an asp.net app. If you have to package an installer, copying the correct files before building the installer should do the trick as well.


For my DAL projects I typically have a DataLayer.dll.config file but I actually add the ConnectionString entries to my Program.exe.config file. I don't know that you need anything special for this. Possibly just reference a static readonly string or something from the DataLayer.dll in the Program.exe?

Or, do you really want to have separate configuration files? If so then you can use the ConfigurationManager class to open a loose configuration file with the OpenMappedMachineConfiguration method. See MSDN ref here: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedmachineconfiguration.aspx


I think you can use applicationSettings to include configuration for specific referenced assemblies:

For example:

<configuration>
 <applicationSettings>
     <ProjectName.Properties.Settings>
        <setting name="ConnectionString" serializeAs="String">
           <value>YourConnectionStringHere</value>
        </setting>
     </ProjectName.Properties.Settings>
  </applicationSettings>
</configuration>

Where "ProjectName" is the name of the reference you need to configure. Each project can have it's own app.config with the above applicationSettings entry and a value specific to the project itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜