title: Getting the connection string in a c# service
body: <p>I'm using VS 2008.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>&lt;connectionStrings&gt;
  
       &lt;clear /&gt;
       &lt;add name="REConnectionString"
            connectionString="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True;"
            providerName="System.Data.SqlClient" /&gt;
  
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>And in my MyService.svc.cs I have the following:</p>
  
  <pre><code>private readonly string connectionString = ConfigurationManager.ConnectionStrings["REConnectionString"].ConnectionString;
  </code></pre>
  
  <p>But when I run the service this value connectionString is null.  How do I get this value from the web.config?</p>
  
  <p>I have even added the following but it is also not working:</p>
  
  <pre><code>&lt;appSettings&gt;
       &lt;add key="REConnectionString" value="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True;"/&gt;
  &lt;/appSettings&gt;
  </code></pre>
  
  <p>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?</p>