Search code examples
c#.netweb-serviceswcfwcf-binding

Hosting two WCF services using a single console app


I am trying to host two services using a single console app. However, when I am trying to do so, only one service gets hosted, while the other does not.

Program.cs:

namespace WWWCFHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(WWWCF.Login)))
            {
                host.Open();
                Console.WriteLine("Service1 Started");
            }
            using (ServiceHost host1 = new ServiceHost(typeof(WWWCF.UserRegistration)))
            {
                host1.Open();
                Console.WriteLine("Service2 Started");
                Console.ReadLine();
            }
        }
    }
}

App.config

<configuration>
  <system.serviceModel>

    <services>
      <service name="WWWCF.Login" behaviorConfiguration="WWWCF.mexBehaviour1">
        <endpoint address="Login" binding="basicHttpBinding" contract="WWWCF.ILogin">
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080"/>
          </baseAddresses>
        </host>
      </service>

      <service name="WWWCF.UserRegistration" behaviorConfiguration="WWWCF.mexBehaviour2">
        <endpoint address="UserRegistration" binding="basicHttpBinding" contract="WWWCF.IUserRegistration">
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8090"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WWWCF.mexBehaviour1">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
        <behavior name="WWWCF.mexBehaviour2">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

As in the code above, I am trying to host one service on port 8080 and the other on port 8090. When I run the application, the first service starts and then closed automatically and the second service remains started. How can I host both the services simultaneously ?

I have gone through the link : Two WCF services, hosted in one console application

I have gone through other threads as well.But they do not solve my issue.

Will be happy to provide any further details if required.


Solution

  • Your first service jumps out of the using block and so is disposing too early. Try this...

    using (ServiceHost host = new ServiceHost(typeof(WWWCF.Login)))
    using (ServiceHost host1 = new ServiceHost(typeof(WWWCF.UserRegistration)))
    {
         host.Open();
         Console.WriteLine("Service1 Started");
    
         host1.Open();
         Console.WriteLine("Service2 Started");
         Console.ReadLine();
     }
    

    Take a look at this: http://msdn.microsoft.com/en-us//library/yh598w02.aspx