Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
311 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're instantly closing the first, since it's in the using. You need to set it up so the first using scope doesn't end until after the ReadLine() call.

Try:

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();
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...