I have a SelfHosted WCF service and when I'm trying to read connection string from web.config file
string conn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
then I'm getting an error:
Object reference not set to an instance of an object.
My Web.config:
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="server=192.168.1.2;database=TestDB;uid=sa;pwd=saas" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
...
I tried to use the WebConfigurationManager
as described in this question , but it did not help.
Why I cannot get connectionString from web.config?
A bit safer settinng:
ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings["MyConnectionString"];
if (conn == null || string.IsNullOrEmpty(conn.ConnectionString))
{
throw new Exception("Couln't find connection string in web.config.");
}
myConnString = conn.ConnectionString
You can also try to set in visual studio once you select your config file, in the properties window set Copy to Output Directory
to Copy always
and rebuild your application.
In my WCF service I used this way to get connectionstring.
ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString()
But in case your problem has something to do with Asp.net and want to enjoy privileges it offers like accessing web.config you might need to add some configuration below. You mentioned Self-hosted, I assume it should be app.config not web.config right? "I have theory that your wcf has something to do with Asp.net, which is not mentioned in you question, Correct me if I'm wrong."
<system.serviceModel>
...
<serviceHostingEnvironment **aspNetCompatibilityEnabled="true"**/>
...
</system.serviceModel>
I found the solution. It was my fault. In the VS solution I have 2 projects: First(WCF service) and Second(Console app that hosted my service).
The problem was that I added connectionStrings
to the First project in web.config
file!
After I added the connectionStrings
to the Second project in app.config
file, everything works fine
If you're using .NET 4.5+ and have access to C# 6.0, you can make use of the null coalescing operator ( ?
) to have conn
set to null without automatically throwing an exception:
string conn = ConfigurationManager.ConnectionStrings["MyConnectionString"]?.ConnectionString;
//-------------------------------------------------------------------HERE-^-HERE-------------
The only way this is different from your original code is the additional question mark after the closing bracket. When ConfigurationManager.ConnectionStrings["MyConnectionString"]
is null, an exception isn't automatically thrown when you try to access the ConnectionString
property on a null object.
AFAIK the above statement that uses the null coalescing operator is functionally equivalent to the following code:
// First, see if you can get a reference to the main object in ```Web.config```.
ConnectionStringSettingsCollection connStringSettingsCollection = ConfigurationManager.ConnectionStrings["MyConnectionString"];
// Next, try to get the value -- but don't throw an exception if it can't be retreived.
string conn;
if (connectionStringsInfo == null)
conn = default(string);
else
conn = connectionStringsInfo.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.