2012-07-29 94 views
3

我有两个独立的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> 
+0

您可以阅读文章* [在单个Windows服务下托管多个WCF服务](http://blog.thijssen.ch/2009/08/hosting-multiple-wcf-services-under.html)*。这非常有趣,基于ServiceManager。 – 2012-07-29 16:32:37

回答

0

这样做的原因错误

The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.

TraceUtility尝试在名为SetEtwProviderId()的内部方法中初始化其事件跟踪时触发。

的内部异常的检查表明:

Configuration system failed to initialize

及其内部异常显示:

节只必须按照配置文件中出现一次。

所以它不是你的代码,但你的配置文件是错误的。

我可以看到哪里可能会出现混淆。当您添加一个WCF库项目的解决方案,你会在它的app.config发现这一点:

<!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="WcfServiceLibrary2.Service1"> 
     <!-- rest omitted for brevity --> 

因此指令是添加配置到你的主机的一个应用的的app.config,但ISN”非常明确地知道要拷贝什么。

您需要以一个有效的app.config结束。您将<services>元素复制到应用程序主机的app.config。现在你有2 <services>这是一个无效的配置部分。而是将<services>的子元素复制到app.config中的单个<services>元素。

所以要明确:

<!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <!-- COPY FROM HERE ... --> 
     <service name="WcfServiceLibrary2.Service1"> 
     <host> 
      <baseAddresses> 
      <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary2/Service1/" /> 
      </baseAddresses> 
     </host> 
     <!-- Service Endpoints --> 
     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary2.IService1"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <!-- Metadata Endpoints --> 
     <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
     <!-- This endpoint does not use a secure binding and should be secured or removed before deployment --> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
     <!-- 
    .... TO HERE to the app.config of your application hosts config file 
     --> 
    </services> 

假设你已经有了一个基本的system.serviceModel配置存在。

如果Visual Studio不是在抱怨你的配置文件,你总是可以启动WCF服务配置编辑器(在VS的工具菜单下或在配置文件的上下文菜单中)来检查WCF配置。如果它被打破,它会对你咆哮。

相关问题