views:

995

answers:

4

I have written an ASP.Net 3.5 WCF service and when I created this project VS gave me a web.config file to go with it. Having done this the app now seems entirely blind to connectionstrings, a big problem as the app will be heavily reliant on reformatting and spitting out SQL Server DB data in various web friendly formats.

Most of the references I've seen for getting WCF services equipped with DB Connections tell you to put the connection strings in App.Config files, I was keen to avoid this... and so is VS2008, as when I try to add a new app.config a straight configuration file is not a choice on offer, only a second web configuration file.

Seeing as I already have one perfectly good web.config file I would rather my data access code could just use that. I guess I'm probably missing something really obvious but:

ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString

Doesn't work as in debug the configurationmanager claims there are no connectionstrings defined, despite the fact that I have seven connectionstrings fully defined in the web.config file. I have using statements for System.Configuration, System.Web and System.Web.Configuration.

It seems as if even though the project seems to be designed to refer to a web.config this is not in fact the case. It also seems to be unable to access a regular app.config at present.

Having said all this all the httphandlers and endpoints in the web.config seem fine. If I define the connectionstring specifically when I call the datacontext it all works perfectly. It's specifically the connectionstring section of the config file that seems to be having an issue...

Any thoughts?

EDIT:

FWIW this is the (only slightly doctored) content of the ConnectionStrings section of the web.config.

<connectionStrings>
     <add name="D_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
     <add name="PV_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
     <add name="L_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
     <add name="AL_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
     <add name="APV_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
     <add name="AD_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
     <add name="TV_CS" connectionString="[the_connection_string]" providerName="System.Data.SqlClient"/>
    </connectionStrings>

Like I said anything else the file is supposed to do to assist in the execution of its duties it appears to do without issue or complaint.

FURTHER EDIT: The application is currently only in development, so is attempting to run through the VSHOST it will in the fulness of time be hosted on the web through IIS.

And yes, I have an explicit project reference to System.Configuration

UPDATE: To try to lock this down I had a go at passing the ConnectionString as a string from an AppSetting, as so far I have been using

DataServiceContext dsc  = new DataServiceContext("connection string as string");

It can't see the AppSettings either.

A: 

Try..

using System.Configuration;

or

System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString
danrichardson
-1 If he's already able to use ConfigurationManager without a compilation error...he already has the proper using statement.
Justin Niessner
Yeah, sorry, forgot to mention I do have using System.Configuration.
he stated in his question that he'd added that in the using statements
BBlake
yes, only after and edit BBlake.Good point Justin :)
danrichardson
ahh, he must have already edited it when I looked at it and before I saw your answer. apologies.
BBlake
A: 

On a web server, you have to use WebConfigurationManager. Unless there is something I didn't know with the default ConfigurationManager.

Pierre-Alain Vigeant
I gave it a try. Sorry, it still can't see the ConnectionStrings or AppSettings.
@Pierre-Alain - The *recommendation* is to use the WebConfigurationManager in a web setting as that will correctly parse .config files up the hierarchy - so if I'm in /folder1 and there's a web.config in there, this will be combined with the root web.config when I request details from it. You can still use ConfigurationManager on a web application.
Zhaph - Ben Duguid
+1  A: 

Okay... this is interesting.

I wanted to test my REST URIs through a browser. Just to see that the URI typed into a browser would return the correct data.

Well, after much casting about I found that you need a sort of proxy WCF server to do that, just a little console app that runs a service did it nicely.

It turns out the wcf uses the Proxy's config, not the web.config of the IIS (or in this case VSHOST) hosted service. So if you tool that up with a reference to System.Configuration, bung in an app.config and copy the relevant connectionstrings section into that it works perfectly.

How irritating.

Y'all probably would have worked that out if I'd thought it was in any way relevant, which it shouldn't be... but is... bah.

Thanks for wasting time trying to help out.

That is interesting
Pierre-Alain Vigeant
Your WCF REST service needs to be hosted either in IIS and then it would use the web.config, or then you'll have to have your own service host to instantiate the WebServiceHost, and then yes, that app's config will be used. Doesn't seem too surprising to me....
marc_s
That was the reason I had asked whether you were hosting in IIS or self-hosting --> hosting IIS = web.config, self-hosting = host app's config.
marc_s
@marc_s: Well, yeah but for most practical purposes VSHOST tends to ape IIS. Having to write a tiny server and run that through VSHOST in order to test some functionality I was able to test "normally" using an ASP.Net 2 hack surprised me somewhat.
+1  A: 

WCF is designed to run on different types of bindings (HTTP, TCP, Message Queues, pipes, etc...). The classic webservice (asmx) is always running over HTTP. For this reason, by default, you don't get access to the web.config file.

If you want to be able to use asp.net's HTTPContext, you need to set the ASP.NET compatibility mode to true, like shown below:

<system.serviceModel>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

</system.serviceModel>

Please see this blog post for a more detailed discussion.

Gus