title: Read connection string from web.config file when deploy to IIS7
body: <p>It seems to be pretty simple question but not for me. I am trying to read connection string from web.config file. </p>
  
  <p>I have WCF service application which have web.config file. In web.config file I defined connection strings.
  Then I deployed wcf application on IIS7 under default web site(One template which comes when you install IIS7).</p>
  
  <p>Now when we read connection string then it is not giving me connection strings which define in wcf web.config file. Somehow I am not able to access it. And while debugging when I found a connection string which is actually not connection string which I defined in wcf web.config but it is default web site connection string which I don't want.</p>
  
  <p>I want to access connection string which I defined WCF web.config file only. I am stuck in it. I tried all but with no luck. For reference I am putting code which I tried and web.config code also.</p>
  
  <p>Web.Config code.</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.diagnostics&gt;
      &lt;trace&gt;
        &lt;listeners&gt;
          &lt;add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="AzureDiagnostics"&gt;
            &lt;filter type="" /&gt;
          &lt;/add&gt;
        &lt;/listeners&gt;
      &lt;/trace&gt;
    &lt;/system.diagnostics&gt;
    &lt;appSettings&gt;
      &lt;add key="ConnString" value=""/&gt;
      &lt;!--&lt;add key="ConnString" value=""/&gt;--&gt;
    &lt;/appSettings&gt;
    &lt;connectionStrings/&gt;
    &lt;system.web&gt;
      &lt;httpRuntime maxRequestLength="2097151"
      useFullyQualifiedRedirectUrl="true"
      executionTimeout="14400"   /&gt;
      &lt;compilation debug="true" /&gt;
    &lt;/system.web&gt;
  </code></pre>
  
  <p>Code to read connstring</p>
  
  <pre><code>string connString = string.Empty;
  
              string svcDir = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
              DirectoryInfo rootDir = Directory.GetParent(svcDir);
              string root = rootDir.FullName;
              string webConfigFilePath = root + "\\Web.config";
  
              ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
              fileMap.ExeConfigFilename = webConfigFilePath;
              Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
  
              var test = configuration.ConnectionStrings.ConnectionStrings;
  
              string connectionString = "";
              if (configuration.ConnectionStrings.ConnectionStrings["ConnString"].ConnectionString.Length &gt; 0)
              {
                  connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnString"].ConnectionString;
              }
  
              //var connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnString"];
              //if (connectionString != null)
              //{
  
              //}
  
              return connString;
  </code></pre>
  
  <p>I am using .net framework 4.0, IIS 7, apppool is .net framework 4.0 and windows 7 machine.</p>
  
  <p>Thanks,
  Awadhendra</p>