I am writing a simple Console application which reads all wcf services configured in application.config and then host those services using self hosting.
I came across a similar article here, but I am not getting how the solution fits in my case.
I have created a simple WCF service in a seperate project.
I now create a new console project. I have the proxy for the service correctly setup here.
I now want to read the service settings from the app.config file. Use those settings to dynamically initialize service host object. Open the service and then perform my other tasks.
I have the following code to read the app.config.
private static void ReadServiceConfig()
{
IList<ServiceHost> hosts = new List<ServiceHost>();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ClientSection clientSection = (ClientSection)config.GetSection("system.serviceModel/client");
if (clientSection != null)
{
ChannelEndpointElementCollection endpoints = clientSection.Endpoints;
foreach (ChannelEndpointElement endpoint in endpoints)
{
ServiceHost host = new ServiceHost(endpoint.Contract.GetType(), endpoint.Address);
//Lost at this point. Not sure how to add service endpoints here
}
}
}
The hardcoded version here,
private static void RunWcfServices()
{
try
{
Uri studentAddress = new Uri("http://localhost:8733/SchoolStudentService");
ServiceHost studentHost = new ServiceHost(typeof(SchoolStudentService), studentAddress);
studentHost.AddServiceEndpoint(typeof(TestWcfServices.IStudentService), new WSHttpBinding(), studentAddress);
ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();
behaviour.HttpGetEnabled = true;
studentHost.Description.Behaviors.Add(behaviour);
Uri employeeAddress = new Uri("http://localhost:8734/CompanyEmployeeService");
ServiceHost employeeHost = new ServiceHost(typeof(CompanyEmployeeService), employeeAddress);
employeeHost.AddServiceEndpoint(typeof(TestWcfServices.IEmployeeService), new WSHttpBinding(), employeeAddress);
employeeHost.Description.Behaviors.Add(behaviour);
studentHost.Open();
employeeHost.Open();
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}
I have the endpoint details, but those are strings. I don't want to hard code anything in my code since I want to drive everything through configs.
After going through articles on Internet, I feel this can be implemented using ServiceHostFactory class. But the problem still remains, how will I get the end point details required by ServiceHostFactory class.
Any suggestions how can i take this forward???
Please Note::I do not have .svc files and not want to use IIS or other techniques for hosting