使用单个控制台应用程序托管两个WCF服务

Ank*_*tal 4 .net c# wcf web-services wcf-binding

我试图使用一个控制台应用程序托管两个服务.但是,当我尝试这样做时,只有一个服务被托管,而另一个服务没有.

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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我试图在端口8080上托管一个服务,而另一个在端口8090上托管.当我运行应用程序时,第一个服务启动然后自动关闭,第二个服务仍然启动.如何同时托管这两项服务?

我已经完成了链接:两个WCF服务,托管在一个控制台应用程序中

我也经历过其他线索.但他们没有解决我的问题.

如有需要,将很乐意提供任何进一步的细节.

Ree*_*sey 5

你正在关闭第一个,因为它正在使用中.您需要对其进行设置,以便在ReadLine()调用之后第一个使用范围不会结束.

尝试:

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();
     }
}
Run Code Online (Sandbox Code Playgroud)

  • @CamBruce话虽如此,如果你有*数百*的自托管服务,我会重新考虑你的整体设计,并真的质疑为什么他们在同一个控制台应用程序... (3认同)

sim*_*lds 5

你的第一个服务跳出了using街区,所以处理得太早了.试试这个...

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();
 }
Run Code Online (Sandbox Code Playgroud)

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