Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can WCF self hosted applications create ServiceHosts automatically using the app.config?

Tags:

wcf

When I create a self hosted wcf application, I create ServiceHost objects for each service I want to expose. It then looks in the app.config (matching up the server name), and then pulls the associated endpoint address and contract.

Is there a way to automatically create ServiceHosts for every service that is listed in the app.config. I would like to add new services to the app.config and have them loaded automatically without recompilng my program and using my manually coded process to create ServiceHost objects.

Is there a factory or a tutorial someone could link me that shows me how to do this? Thanks

like image 928
djmc Avatar asked Sep 18 '10 02:09

djmc


People also ask

What is self hosting in WCF?

This is referred to as a self hosting WCF service, the exact meaning of Self Hosted is that it hosts the service in an application that could be a Console Application or Windows Forms and so on. Earlier we saw what a WCF Service is in the . Net environment. We can host a WCF service in IIS and a Windows service also.

How to Test self hosted WCF service?

Test the servicePress Ctrl + F5 to run the service. Open WCF Test Client. To open WCF Test Client, open Developer Command Prompt for Visual Studio and execute WcfTestClient.exe. Select Add Service from the File menu.

How to Host a WCF service in console application?

config file so for that go to "Tools" > "WCF Service Configuration Editor". Then go to "File" > "Open" > "Config File" and open the config file of this project where it is present and then click on Create a New Service. And then open the DLL of the service and then open the service reference.


1 Answers

I'm not sure what do you mean by pulling associated addresses and contracts from config - this is done automatically. Service section in configuration file is automatically paired with type of service hosted in ServiceHost:

Service hosting:

using (var host = new ServiceHost(typeof(MyNamespace.Service))
{
  // no endpoint setting needed if configuration is correctly paired by the type name
  host.Open() 
}

Service configuration:

<services>
  <service name="MyNamespace.Service">
    ...
  </service>
</service>

Now the only thing you need is to handle ServiceHost creation automatically. Here is my sample code to do it:

   class Program
   {
       static void Main(string[] args)
       {
           List<ServiceHost> hosts = new List<ServiceHost>();

           try
           {
               var section = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
               if (section != null)
               {
                   foreach (ServiceElement element in section.Services)
                   {
                       var serviceType = Type.GetType(element.Name);
                       var host = new ServiceHost(serviceType);
                       hosts.Add(host);
                       host.Open();
                   }
               }

               Console.ReadLine();
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
               Console.ReadLine();
           }
           finally
           {
               foreach (ServiceHost host in hosts)
               {
                   if (host.State == CommunicationState.Opened)
                   {
                       host.Close();
                   }
                   else
                   {
                       host.Abort();
                   }
               }
           }
       }
   } 
like image 135
Ladislav Mrnka Avatar answered Oct 27 '22 01:10

Ladislav Mrnka