我有一个使用NetTcpBinding的WCF服务,我想在WPF应用程序中托管它。该服务似乎正确启动,但是当我尝试使用“添加服务参考”来获取它的元数据时。在visual studio中我得到了这个例外:
The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/Mandrake/mex'.
我的服务项目的App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="Mandrake.Service.OTAwareService">
<endpoint address="OTService" binding="netTcpBinding" contract="Mandrake.Service.IOTAwareService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="MEX" address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8000/Mandrake/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
托管应用程序中的代码:
Uri baseAddress = new Uri("net.tcp://localhost:8000/Mandrake");
ServiceHost host = new ServiceHost(typeof(OTAwareService), baseAddress);
try
{
host.AddServiceEndpoint(typeof(IOTAwareService), new NetTcpBinding(), "OTService");
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);
host.Abort();
}
我发现问题的解决方案主要是添加&#39; serviceMetaData&#39;到服务配置或提供mex端点。你能提出一些建议吗?
修改
最终配置:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Mandrake.Service.OTAwareService" behaviorConfiguration="NewBehavior0">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8036/OTService"/>
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" name="TcpEndpoint" contract="Mandrake.Service.IOTAwareService" />
<endpoint address="mex" binding="mexTcpBinding" name="MetadataEndpoint" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
主机应用程序:
host = new ServiceHost(typeof(OTAwareService));
host.Open();
答案 0 :(得分:2)
我已经成功解决了这个问题,在启用了serviceDebug的includeExceptionDetailInFaults后,它非常清楚。
Mandrake.Service.IOTCallback.Send operation references a message element [http://tempuri.org/:Send] that has already been exported from the Mandrake.Service.IOTAwareService.Send operation
因此在服务合同和回调接口中也有一个Send(OTMessage)操作。一个相当丑陋的错误,但我想我会留下解决方案,以防它帮助任何人。