我有两个独立的DLL文件中的两个WCF服务。我想在一个自己托管的项目(控制台主机)中托管这些项目。如何在单个控制台主机中托管多个WCF服务?
我想用下面的代码来做到这一点,但我得到这个异常:
的类型初始为 “System.ServiceModel.Diagnostics.TraceUtility”引发了异常。
C#代码:
public static void Main(string[] args)
{
new Program().inital();
}
private void inital()
{
ServiceHost myService = new ServiceHost(typeof(ClientNotificationService));
ServiceHost myService2 = new ServiceHost(typeof(ServerNotificationService));
try
{
myService.Open();
myService2.Open();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
在App.config
:
<system.serviceModel>
<services>
<service name="ClientNotification.ClientNotificationService">
<endpoint address="net.tcp://localhost:7081/CVClientNotificationService"
binding="netTcpBinding" contract="ClientNotification.IClientNotification">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/ClientNotification/ClientNotificationService/" />
</baseAddresses>
</host>
</service>
</services>
<services>
<service name="ServerNotification.ServerNotificationService">
<endpoint address="net.pipe://localhost/ServerNotificationService" binding="netNamedPipeBinding"
contract="ServerNotification.IServerNotification">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/ServerNotification/ServerNotificationService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
您可以阅读文章* [在单个Windows服务下托管多个WCF服务](http://blog.thijssen.ch/2009/08/hosting-multiple-wcf-services-under.html)*。这非常有趣,基于ServiceManager。 – 2012-07-29 16:32:37