views:

32

answers:

2

I've written 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.

A: 

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.

Sayed Ibrahim Hashimi
+1  A: 

more interested in, say, specifying the app.config file in an MSBuild property

That's what I did. Worked fine.

Si
That's what I ended up doing. Came back and saw this answer.
Chris McCall