Hello All,
Hoping for a quick idea of the "best" way to pass simple parameters to services. For instance, I am developing a service that hooks into a SQL database for it's data persistance... anyone know the best way for the service's constructor to pull a connection string out the creating host
Thanks,
Rick

Passing parameters to services
Anonymous1980
just need more info:
you want to read connection information on host side
Everytime client creates the new proxy class,we call service class constructor,inside service constructot,you can read connection string from app.config(self hostcase),or web.config(in case of web host)
in this sample,we are reading base addres from config
http://windowssdk.msdn.microsoft.com/library/default.asp url=/library/en-us/WCF_samples/html/05e68661-1ddf-4abf-a899-9bb1b8272a5b.asp
you can do same thing for connection string
-Thank you
Madhu
funk101
Perhaps I am simplifying what you are trying to accomplish here, but I have a few comments based on what I have read in this thread:
>Your service implementation is like the code behind an ASPX page...it should NOT have business logic in it. Instead, it should invoke the business component that is the service functionality beneath it. So, for every service you will have the following assembly breakdown (at a minimum):
>Your host can include connection string information in its app.config or web.config. Your business logic or data access code can access this using System.Configuration. No need to pass it around from ServiceHost to constructor and couple the hosting and service initialization with something that is clearly related to business functionality.
Just use standard conventions for loading configuration, like you would in any app. And, you can (and should) encrypt those sections of the config as well.
Colonel Hogan
unfortunately constructing the proxy object with cosntructor params won't work in wcf (or may be in any SOA program)
when servicehost got very first request from the client,it will activate the service object and then service constructor will be called (so somebody has to send the message to service,then service object will be created inside host app).
you can use sessions and call setconnstring() method first and then server can remember connstring value till you close the servicehost.
you can manage state by using session objects:
we have sdk sample here
http://windowssdk.msdn.microsoft.com/library/default.asp url=/library/en-us/WCF_samples/html/36e1db50-008c-4b32-8d09-b56e790b8417.asp
(1)Declare interface and enable sessions and decalre 2 methods to set connection string and get connection string.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples", Session=true)]
public interface ICalculatorSession
{
[OperationContract(IsOneWay=true)]
void SetconnString(string connstring);
[OperationContract(IsOneWay=false)]
string GetconnString();
}
(2)implement that ineterface
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class CalculatorService : ICalculatorSession
{
private string m_connstring;
public void SetconnString(string connstring)
{
m_connstring = connstring;
}
public string GetconnString()
{
return m_connstring;
}
}
(3) client side
// Create a proxy with given client endpoint configuration
using (CalculatorSessionProxy proxy = new CalculatorSessionProxy())
{
proxy.SetconnString("hello");
string retval=proxy.GetconnString();
Console.WriteLine(retval);
or if you want share the state between all the clients,my team member wrote one sample,that may help
http://windowscommunication.net/ControlGallery/ControlDetail.aspx Control=2241&tabindex=2
-Thank you
Madhu
DeborahK
You could store your data in an IExtension<ServiceHostBase> object (or at a different level if you like).
See http://windowscommunication.net/ControlGallery/ControlDetail.aspx Control=2241&tabindex=2 for sample.
Or you could subclass ServiceHost with additional properties.
Thanks,
Scott
dince
No, thats easy....
The issue I want to address is how do I pass a name value pair from a ServiceHost<T> to the actual type that hosts the service... or rather
[ServiceContract]
public class ServiceClass
{
public ServiceClass()
{
// This is where I want to pull the connection string from the context
}
[OperationContract]
public void MyOperation()
{
SqlConnectionString sqlConn = SqlConnectionString(_sqlConnString); // Where _sqlConnString is stored earlier
}
}
public void Main()
{
ServiceHost theHost = new ServiceHost(typeof(ServiceClass));
// Pass the connection string to the service
}