1

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

Community
  • 1
  • 1
  • what is the problem you're facing here? – Amit Kumar Ghosh Jun 06 '15 at 16:17
  • use this:`AddServiceEndpoint(Type, Binding, String)`. you have the contract,endpint address. What else do you need? – Amit Kumar Ghosh Jun 06 '15 at 16:19
  • I am not able to guess how I should I make this process dynamic. If you see, in the method RunWcfServices,I have hardcoded the values to host my services. I want to remove these hardcodings and instead read all the values from app.config. This is what i am attempting to do in method ReadServiceConfig. But I am not sure should I proceed with this was or use ServiceHostFactory. I dont know how i can read values from config and then dynamically initialize servicehost object. – Rootkit Affected Jun 06 '15 at 16:30
  • 1
    Really unclear what the actual problem is. The WCF ServiceHost will, by default, read the ` ` from App.config. – H H Jun 06 '15 at 16:33
  • I am using self hosting, so I have to setup the ServiceHost class. I want to read settings from app.config and use those values to setup servicehost. So if I have setup 10 wcf services in my app.config, my code to dynamically read these during runtime and host those 10 services. If I reduce it to 5,it should host only 5. All these without any hardcoding and manual codings. – Rootkit Affected Jun 06 '15 at 16:38

1 Answers1

1
            var clientSection = config.GetSectionGroup("system.serviceModel").Sections[2].ElementInformation;
            PropertyInformationCollection endpoints = clientSection.Properties;
            foreach (PropertyInformation endpoint in endpoints)
            {
                foreach (ServiceElement key in (ServiceElementCollection)endpoint.Value)
                {
                    var j = key.Endpoints[0];
                    ServiceHost host = new ServiceHost(j.Contract.GetType(), new Uri(key.Host.BaseAddresses[0].BaseAddress));
                    host.AddServiceEndpoint(j.Contract.GetType(), new BasicHttpBinding(), j.Address);
                }
            }
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24