4

I'm using VS 2008.

I have a service that needs to be deployed. The service won't be used from a .NET client (like a website or Windows client). It needs a database connection.

I initially had a console app that called the service, and the connection string was set in the app.config of the console app. I want to move the connection string to the service. So in the web.config that come with the service I put in the following:

<connectionStrings>

     <clear />
     <add name="REConnectionString"
          connectionString="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True;"
          providerName="System.Data.SqlClient" />

</connectionStrings>

And in my MyService.svc.cs I have the following:

private readonly string connectionString = ConfigurationManager.ConnectionStrings["REConnectionString"].ConnectionString;

But when I run the service this value connectionString is null. How do I get this value from the web.config?

I have even added the following but it is also not working:

<appSettings>
     <add key="REConnectionString" value="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True;"/>
</appSettings>

If I loop through the list of connection string then it keeps on bring back the connection string from the calling app. Is there no way that the config file can be used?

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • Try to debug and see what comes in ConfigurationManager.ConnectionStrings... Seems that your code might be work. – Chuck Norris Mar 07 '12 at 06:54
  • See this http://social.msdn.microsoft.com/Forums/en-IE/csharplanguage/thread/594cc2ef-ad6b-4e96-8b07-42ea67b2e351 ... – Chuck Norris Mar 07 '12 at 06:57
  • And this, maybe they can help you http://stackoverflow.com/questions/9494699/how-to-get-connection-string-from-another-project-in-linq-to-sql – Chuck Norris Mar 07 '12 at 06:57
  • What happens if you loop through `ConfigurationManager.ConnectionStrings`? Perhaps your app is using a different config file than the one you are looking at? – Jeroen Mar 07 '12 at 07:07
  • @Jeroen: I looped through the connection string and it is still returning the connection string from the calling app. I don't want it here because the calling app will be a Java app. I would like to set the connection string in the service or in my data layer. – Brendan Vogt Mar 07 '12 at 07:17
  • @BrendanVogt I meant try looping through the connection strings in your web service code, not in the calling (test) app. Just to check what the ConfigurationManager *thinks* is in your web.config's ConnectionStrings section. Also: it may help if you tell us about your project structure, perhaps a screenshot of the solution explorer? (As the code you posted seems just fine...) – Jeroen Mar 07 '12 at 08:08

2 Answers2

1

I will try with something like this:

Configuration config = ConfigurationManager.OpenExeConfiguration("name_of_your_service.exe");
string connectString = config.ConnectionStrings["YourConnectionStringNameHere"];

You should pay attention to the string passed as name of the config file.
It should be the name of an exe or dll not the config file itself. OpenExeConfiguration will open as Configuration object a file named "name_of_your_service.exe.config"

Details on OpenExeConfiguration

Just found an interesting question here on SO

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
0

I right clicked on the project and added a setting of type connection string. And then I retrieve it like:

MyProject.Properties.Settings.Default.MyConnectionString;
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234