使用单一的控制台应用程序举办两场WCF服务 [英] Hosting two WCF services using a single console app

查看:140
本文介绍了使用单一的控制台应用程序举办两场WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个单一的控制台应用程序托管两种服务。然而,当我试图这样做,只有一个服务被托管,而另一个没有。

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

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

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>

由于在code以上,我想主办的8080端口一个服务,另一个端口8090。当我运行应用程序,第一个服务启动,然后自动关闭,而第二服务仍然启动。我怎么能同时承载两种服务?

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 ?

我已经通过链接了:<一href="http://stackoverflow.com/questions/9187308/two-wcf-services-hosted-in-one-console-application">Two WCF服务,托管在一个控制台应用程序

我通过其他线程了,因为他们不解决我的问题well.But。

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.

在此先感谢。

推荐答案

您的第一个服务跳出使用块等被处理得太早。试试这个...

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

看看这个: http://msdn.microsoft.com /en-us//library/yh598w02.aspx

这篇关于使用单一的控制台应用程序举办两场WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆