如何在单个控制台主机中托管多个WCF服务?

时间:2012-07-29 16:28:42

标签: c# .net visual-studio-2010 wcf wcf-hosting

我在两个单独的DLL文件中有两个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>

1 个答案:

答案 0 :(得分: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中,但是对于要复制的内容并不是非常明确。

您需要使用有效的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配置。如果它坏了它会吠叫你。