Hosting two WCF services using a single console app

后端 未结 2 473
说谎
说谎 2020-12-19 16:06

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:<

相关标签:
2条回答
  • 2020-12-19 16:23

    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

    0 讨论(0)
  • 2020-12-19 16:40

    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();
         }
    }
    
    0 讨论(0)
提交回复
热议问题