简体   繁体   中英

Read connection string from web.config file when deploy to IIS7

It seems to be pretty simple question but not for me. I am trying to read connection string from web.config file.

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).

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.

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.

Web.Config code.

<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          name="AzureDiagnostics">
          <filter type="" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
  <appSettings>
    <add key="ConnString" value=""/>
    <!--<add key="ConnString" value=""/>-->
  </appSettings>
  <connectionStrings/>
  <system.web>
    <httpRuntime maxRequestLength="2097151"
    useFullyQualifiedRedirectUrl="true"
    executionTimeout="14400"   />
    <compilation debug="true" />
  </system.web>

Code to read connstring

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 > 0)
            {
                connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnString"].ConnectionString;
            }

            //var connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnString"];
            //if (connectionString != null)
            //{

            //}

            return connString;

I am using .net framework 4.0, IIS 7, apppool is .net framework 4.0 and windows 7 machine.

Thanks, Awadhendra

The web.config has a dedicated section for connection strings, and the code you are using is using the configuration manager to access the connection strings section in the web.config. The problem you have is that you haven't put the connection string in the connection strings section, you have put it in the app settings. Move the connection string out of app settings and put into the connection strings section, or change your code to read it from app settings. Now storing a connection string in app settings was the way it was done in .NET 2.0, but since then (.NET 3.5) there has been a dedicated section made for connection strings.

please refer here

eg

<connectionStrings>
<add name="ConnString" connectionString="xxxwhateveritisxxx" />
</connectionStrings>

or code to read it from app settings (if you must have it in app settings, although I wouldn't recommend):

string connectionString = ConfigurationManager.AppSettings["ConnString"];

Can u give a try to this :-

string connString = ConfigurationManager.AppSettings["ConnString"].ToString()
        return connString;

It will read the app setting section of your web.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM