How can I access app.config on the BeforeBuild event from a custom MSBuild task?
I've wri开发者_开发技巧tten a custom MSBuild task to generate model code from MSSQL stored procedures. I want to use the same configuration for my task to connect to the database as the application does. I've created a config section that looks like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CoreDataConnections" type="CoreData.ConfigHandler, CoreData"></section>
</configSections>
<CoreDataConnections>
<Connection Name="BillingDB" ConnectionString="Data Source=SERVER0;Initial Catalog=DB0;persist security info=False;user id=user;password=password;packet size=4096"/>
<Connection Name="ValidationDB" ConnectionString="data source=SERVER1;initial catalog=DB1;persist security info=False;user id=user;password=password;packet size=4096"/>
</CoreDataConnections>
</configuration>
and access it all day from my app like so:
Dictionary<string,string> Connections = (Dictionary<string,string>)ConfigurationSettings.GetConfig("CoreDataConnections");
My custom task can't see it, though, and GetConfig
returns null
.
What should I do here? I'd prefer not to have to rewrite my custom config section handler, and am more interested in, say, specifying the app.config file in an MSBuild property, but I'll do what it takes.
more interested in, say, specifying the app.config file in an MSBuild property
That's what I did. Worked fine.
Have you tried the ConfigurationManager.OpenExeConfiguration method? You should probably have that path passed into your task from the MSBuild script calling it.
Since the application that is running at the time is msbuild.exe the configuration that was loaded was msbuild.exe.config when you try to access config elements as you normally do in your app you will not get the values in your app.config file.
精彩评论