I have two WCF services in two separate DLL files. I want to host those in a single self-hosted project (console host).
我在两个单独的DLL文件中有两个WCF服务。我想在一个自托管项目(控制台主机)中托管它们。
I am trying to do this with following code, but I am getting this exception:
我试图用以下代码执行此操作,但我得到此异常:
The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.
'System.ServiceModel.Diagnostics.TraceUtility'的类型初始值设定项引发了异常。
C# code:
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();
}
}
In 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 个解决方案
#1
0
The reason for this error
出现此错误的原因
The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.
'System.ServiceModel.Diagnostics.TraceUtility'的类型初始值设定项引发了异常。
is triggered when the TraceUtility
tries to initialize its Event Tracing in an internal method called SetEtwProviderId()
.
当TraceUtility尝试在名为SetEtwProviderId()的内部方法中初始化其事件跟踪时触发。
Inspection of the inner exception shows:
检查内部异常显示:
Configuration system failed to initialize
配置系统无法初始化
and its inner exception shows:
它的内部异常显示:
Sections must only appear once per config file.
每个配置文件只能出现一次。
So it is not your code but your config file that is wrong.
所以这不是你的代码,而是你的配置文件错了。
I can see where the confusion on this might start. When you add an Wcf Library project to the solution you'll find this in its app.config:
我可以看到对此的困惑可能从哪里开始。当您向解决方案添加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 -->
So the instruction is to add that config to the app.config of your hosting applicaton, but is isn't very explicit about what exactly to copy.
因此,该指令是将该配置添加到您的托管应用程序的app.config,但是对于要复制的内容并不是非常明确。
You need to end-up with an app.config that is valid. You copied the <services>
elements to the app.config of the application host. Now you have 2 <services>
elements which is an invalid config section. Instead copy the child elements of <services>
to the single <services>
element in the app.config.
您需要使用有效的app.config结束。您将
So to be clear:
所以要明确:
<!-- 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>
Assuming you already have a basic system.serviceModel
config present.
假设您已经有一个基本的system.serviceModel配置。
If Visual Studio isn't complaining about your config file you can always start the WCF Service Configuration Editor (under the Tools menu in VS or in the context menu of the config file) to inspect the WCF config. If it is broken it will bark at you.
如果Visual Studio没有抱怨您的配置文件,您可以随时启动WCF服务配置编辑器(在VS的“工具”菜单下或配置文件的上下文菜单中)以检查WCF配置。如果它坏了它会吠叫你。
#1
0
The reason for this error
出现此错误的原因
The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.
'System.ServiceModel.Diagnostics.TraceUtility'的类型初始值设定项引发了异常。
is triggered when the TraceUtility
tries to initialize its Event Tracing in an internal method called SetEtwProviderId()
.
当TraceUtility尝试在名为SetEtwProviderId()的内部方法中初始化其事件跟踪时触发。
Inspection of the inner exception shows:
检查内部异常显示:
Configuration system failed to initialize
配置系统无法初始化
and its inner exception shows:
它的内部异常显示:
Sections must only appear once per config file.
每个配置文件只能出现一次。
So it is not your code but your config file that is wrong.
所以这不是你的代码,而是你的配置文件错了。
I can see where the confusion on this might start. When you add an Wcf Library project to the solution you'll find this in its app.config:
我可以看到对此的困惑可能从哪里开始。当您向解决方案添加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 -->
So the instruction is to add that config to the app.config of your hosting applicaton, but is isn't very explicit about what exactly to copy.
因此,该指令是将该配置添加到您的托管应用程序的app.config,但是对于要复制的内容并不是非常明确。
You need to end-up with an app.config that is valid. You copied the <services>
elements to the app.config of the application host. Now you have 2 <services>
elements which is an invalid config section. Instead copy the child elements of <services>
to the single <services>
element in the app.config.
您需要使用有效的app.config结束。您将
So to be clear:
所以要明确:
<!-- 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>
Assuming you already have a basic system.serviceModel
config present.
假设您已经有一个基本的system.serviceModel配置。
If Visual Studio isn't complaining about your config file you can always start the WCF Service Configuration Editor (under the Tools menu in VS or in the context menu of the config file) to inspect the WCF config. If it is broken it will bark at you.
如果Visual Studio没有抱怨您的配置文件,您可以随时启动WCF服务配置编辑器(在VS的“工具”菜单下或配置文件的上下文菜单中)以检查WCF配置。如果它坏了它会吠叫你。