Reputation: 45285
I have the WCF application with Web Service. And I have console application to host Web Service:
ServiceHost host = new ServiceHost(typeof(MyService));
host.Open();
The Web Service connect to the database and I want to store the connection string in the configuration file to allow edit it without solution recompile.
I can add this connection string to App.config of web service, but when I build the Console Hosting Application it will have your own App.config file.
I can add the connection string to App.config of Console Host Application, but I don't know how to pass the parameter from Hosting Application to Web Service.
What is the best way to add this parameter to Web Service ?
Upvotes: 0
Views: 43
Reputation: 3693
Yes, you pass the connection string to the ServiceHost class without too much fuss. When your console program starts, get the connect string from your app.config file in the usual fashion, then store this value in a public static string variable in the public class of your console program. Once you've done that, from within your ServiceHost class you can retrieve it from the console public class.
Something like:
//your console program
public class WebHostConsole
{
private static string sConnectString;
sConnectString = System.Configuration.ConfigurationManager.AppSettings("dbconn");
public static string ConnectionString
{
get { return sConnectString; }
}
//rest of the code
ServiceHost host = new ServiceHost(typeof(MyService));
host.Open();
}
Then, in the constructor of your web service class, reference the ConnectString property of your WebHostConsole class, and you're web service will have a copy of the connect string. Something like:
//your service host class
public class MyService : iMyService
{
public MyService()
{
String _ConnectString = WebHostConsole.ConnectionString;
//rest of your constructor
}
}
Update:
The way this works in my code is that I don't reference the web service functional class, I reference it's interface class.
ServiceHost host = new ServiceHost(typeof(iMyService));
As you probably know, build the WCF interface class with the opreration and data contract definitions, then implement that interface in your working code. when you create your ServiceHost, reference in typeOf the interface, not the working class. That will eliminate the circular reference.
Upvotes: 1