简体   繁体   中英

Not getting connection string value in wcf service method

I am having 1 class library named as AuxiliaryLib which contains app settings like below:

<appSettings>
    <add key="connectionstring" value="Data Source=My-Pc;Initial Catalog=Db1;User ID=StackOverflow;Password=123456"></add>
  </appSettings>

Now, in this class library I have 1 class file which return connection string like below:

public static string ReturnConnectionString()
        {
            return System.Configuration.ConfigurationManager.AppSettings["connectionstring"];
        }

I have 1 wcf project in which i have given reference of above class library ie AuxiliaryLib and calling above connection string method but I am getting null.

 Public void MyMethod()
       {
         var connectionRepo = new ConnectionRepo();
         string str  = ConnectionRepo.ReturnConnectionString(); //getting null
       }

I have web.config in wcf project where I have added connection string but still getting null:

<appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="connectionstring" value="Data Source=My-Pc;Initial Catalog=Db1;User ID=StackOverflow;Password=123456"></add>
  </appSettings>
 <!--<connectionStrings>
    <add name="connectionstring" connectionString="Data Source=My-Pc;Initial Catalog=Db1;User ID=StackOverflow;Password=123456"></add>
  </connectionStrings>-->



 Public void MyMethod(string regionId)
           {
               string str = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString; // null
               string str = System.Configuration.ConfigurationManager.AppSettings["connectionstring"];// null
           }

Note : MyMethod is running on threadpool:

ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod), new object[] { regionId });

Did you try....

using System.Web.Configuration;

WebConfigurationManager.AppSettings["connectionstring"]

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