BaptX
BaptX

Reputation: 589

web.config problem

I have a question about MS webservices and web.config configuration.

I have a solution with two projects inside :

My website is connected to my website with "Web References" from Visual Studio. My webservice is connected to my database by :

public static SqlConnection sqlConn = new SqlConnection(myString);

In my webservice, I retrieve a connectionstring from webservice web.config with this method :

String sa = ConfigurationManager.ConnectionStrings["bdd"].ConnectionString;

I call the webservice via the website (WebReferences) and retrieve some informations from it.

But in order to retrieve the string in webservice, I need to declare this string in the website web.config. It is not logical because only the webservice need this string in order to save datas in database.

If I do not declare the string in the website web.config, I have a nullReferenceException in the webservice.

Anyone knows this problem and a solution in order to avoid to put the ConnectionString in the website ? It must don't know the database existance !

EDIT : To use my webservice, I do this in the website :

WebService1.WS_A webservice = new WebService1.WS_A();

Upvotes: 1

Views: 414

Answers (3)

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

This is a normal behavior, because the webservice will run in the context of the web application and hence it will access the configuration file of the web application.
The same if you have a dataaccess layer that connection to the database, you can use the dataaccess with web application or windows application or console appliction or..., so the dataaccess will access the configuration file of the project that it runs in its context.

Upvotes: 1

Chris Marisic
Chris Marisic

Reputation: 33128

If I'm understanding what you're saying that's not possible currently. The website is actually hosting the web service. So you are required to have all configuration in the web.config of the website.

If you don't want the website to have the connection string to the database, at this point you need to create a separate project to HOST the web service. You can then configure the database connection inside that separate project and it will completely unknown to the first website.

At this point you will be truly using a webservice instead of creating a fancy service that's self hosted and doesn't buy you anything over a simple class.

Upvotes: 1

Brian Driscoll
Brian Driscoll

Reputation: 19635

Each project only has access to its own configuration resources, and cannot access the other project's config resources. So, if you need to instantiate your web service with a connection string (not sure why that's necessary, but I digress...) from your website using a stored connection string, then the connection string needs to be available as a resource to the website.

Again I'm still not certain why it's necessary for you to call your web service with a connection string, as I use both ASMX and WCF services and I've never had to provide a connection string on the client proxy in order to call the service.

However, I must admit that my experience with the various config/setup options for web services in .NET is relatively limited, so I'm in no way ruling out the fact that it might be necessary in your case to instantiate your client proxy with a connection string.

Upvotes: 0

Related Questions