I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

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 i.e 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 });

Upvotes: 0

Views: 380

Answers (1)

Ren&#233;
Ren&#233;

Reputation: 36

Did you try....

using System.Web.Configuration;

WebConfigurationManager.AppSettings["connectionstring"]

Upvotes: 1

Related Questions