C# 使用单个控制台应用程序托管两个WCF服务
我正在尝试使用一个控制台应用程序托管两个服务。但是,当我尝试这样做时,只有一个服务被托管,而另一个没有 Program.cs:C# 使用单个控制台应用程序托管两个WCF服务,c#,.net,web-services,wcf,wcf-binding,C#,.net,Web Services,Wcf,Wcf Binding,我正在尝试使用一个控制台应用程序托管两个服务。但是,当我尝试这样做时,只有一个服务被托管,而另一个没有 Program.cs: namespace WWWCFHost { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(WWWCF.Login))) {
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>
我也经历过其他线程,但它们并不能解决我的问题
如有需要,我们将乐意提供更多细节 您将立即关闭第一个,因为它正在使用中。您需要对其进行设置,以使第一个使用范围在调用ReadLine()
之后才结束
尝试:
您的第一个服务使用
块跳出,因此处理得太早。试试这个
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();
}
看看这个:一旦你离开使用
语句,你的服务范围就消失了。即使我不使用“使用”语句,同样的事情也会发生。谢谢大家的关注。大家好。我又被打动了。编辑帖子。你已经接受了这个问题的答案,如果你创建一个新问题,你会得到更多的回应。这解决了我的问题。谢谢但我想知道,如果我有两个以上的服务(比如说100个),我会怎么做。在另一个内部使用嵌套将使嵌套级别增加到100秒。@simonlchilds提到的解决方案在这种情况下会更好。@AnkitMittal该解决方案与此解决方案相同-唯一的区别是使用的语法(我不是按他的方式做的,因为它在异常情况下会改变行为-如果第一个服务打开,则此服务和您的服务不会实例化第二个服务)。如果您有100个服务,您可以只保留对每个服务的引用集合,然后在Console.ReadLine()
之后循环并处置它们所有的服务。@CamBruce也就是说,如果您有数百个自托管服务,我会重新考虑您的总体设计,真的很怀疑为什么它们在同一个控制台应用程序中…@ReedCopsey关于异常处理。。。我同意您的看法,也许更好的解决方案是避免使用使用
块,并确保在块内的所有非空主机实例上调用Dispose()
。尽管如此,整个设计还是有点时髦!这解决了我的问题。使用此解决方案,我可以轻松地托管任意数量的服务。非常感谢。你可以,但是要注意内存消耗。很高兴我能帮上忙:)记住,如果你通过这个控制台应用程序托管“100个服务”,它们都在一个线程上,并且会相互阻止。@CamBruce也是这样。这将是一个婊子调试!我不会有100次的服务。不过,如果我有100项服务,你有什么特别的建议吗?
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();
}