gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:19:15 24 4
gpt4 key购买 nike

我正在尝试使用单个控制台应用程序托管两项服务。但是,当我尝试这样做时,只有一项服务得到托管,而另一项则没有。

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

应用程序配置

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

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

我已经浏览了链接:Two WCF services, hosted in one console application

我也经历过其他话题。但他们没有解决我的问题。

如果需要,我们很乐意提供任何进一步的详细信息。

最佳答案

您的第一个服务跳出了 using block ,因此处理得太早了。试试这个...

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

关于c# - 使用单个控制台应用程序托管两个 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com