3

我在两个单独的 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>
4

1 回答 1

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 配置。如果它坏了,它会向你吠叫。

于 2016-05-29T12:25:54.090 回答