C# 如何在单个控制台主机中承载多个WCF服务?
我在两个单独的DLL文件中有两个WCF服务。我想在一个单独的自托管项目(控制台主机)中托管它们 我尝试使用以下代码执行此操作,但我遇到了此异常: 的类型初始值设定项 “System.ServiceModel.Diagnostics.TraceUtibility”引发异常 C#代码: 在C# 如何在单个控制台主机中承载多个WCF服务?,c#,.net,visual-studio-2010,wcf,wcf-hosting,C#,.net,Visual Studio 2010,Wcf,Wcf Hosting,我在两个单独的DLL文件中有两个WCF服务。我想在一个单独的自托管项目(控制台主机)中托管它们 我尝试使用以下代码执行此操作,但我遇到了此异常: 的类型初始值设定项 “System.ServiceModel.Diagnostics.TraceUtibility”引发异常 C#代码: 在App.config中: <system.serviceModel> <services> <service name="ClientNotification.ClientNoti
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>
此错误的原因
System.ServiceModel.Diagnostics.TraceUtility'的类型初始值设定项引发异常。
当TraceUtility
尝试在名为SetEtwProviderId()的内部方法中初始化其事件跟踪时触发
对内部异常的检查显示:
配置系统初始化失败
其内部异常显示:
每个配置文件只能显示一次节
因此,错误的不是您的代码,而是您的配置文件
我可以看出这方面的混乱可能从哪里开始。将Wcf库项目添加到解决方案时,可以在其app.config中找到:
因此,说明是将该配置添加到宿主应用程序的app.config中,但没有明确说明要复制什么
你需要得到一个有效的app.config。您已将
元素复制到应用程序主机的app.config中。现在有2个
元素,它们是无效的配置部分。而是将
的子元素复制到app.config中的单个
元素
因此,要明确的是:
假设您已经有了一个基本的system.serviceModel
config
如果Visual Studio没有抱怨配置文件,则始终可以启动WCF服务配置编辑器(在VS中的“工具”菜单下或配置文件的上下文菜单中)来检查WCF配置。如果它坏了,它会朝你吠叫 您可以阅读文章[在单个Windows服务下托管多个WCF服务]()。它非常有趣,基于ServiceManager。
<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>