我有一个使用 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();
}
我发现该问题的解决方案主要是将“serviceMetaData”添加到服务配置或提供 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();