我创建了一个net.tcp WCF服务,如下所示:
const string tcpUri = "net.tcp://localhost:9038";
var netTcpHost = new WebServiceHost(typeof(DashboardService), new Uri(tcpUri));
netTcpHost.AddServiceEndpoint(typeof(IDashboardService), new NetTcpBinding(),
"/dashboard");
netTcpHost.Open();
Console.WriteLine("Hosted at {0}. Hit any key to shut down", tcpUri);
Console.ReadKey();
netTcpHost.Close();
以下是我的IDashboardService
和DashboardSerivce
定义:
[ServiceContract]
public interface IDashboardService
{
[OperationContract]
PositionDashboard Loader();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DashboardService : IDashboardService
{
public PositionDashboard Loader()
{
...
}
}
但是,当我尝试使用WCF测试客户端连接到服务时,出现以下错误:
Error: Cannot obtain Metadata from net.tcp://localhost:9038/dashboard
The socket connection was aborted. This could be caused by an error processing your
message or a receive timeout being exceeded by the remote host, or an underlying
network resource issue. Local socket timeout was '00:04:59.9890000'. An existing
connection was forcibly closed by the remote host
答案 0 :(得分:1)
错误明确提到“无法获取元数据......”因此值得检查您是否拥有TCP Mex端点。它应该在config中看起来像这样:
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
或者,如果您要执行此操作,则需要引用 System.ServiceModel.Discovery ,然后您的代码应如下所示:
const string tcpUri = "net.tcp://localhost:9038";
using (var netTcpHost = new WebServiceHost(
typeof(DashboardService),
new Uri(tcpUri)))
{
netTcpHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
netTcpHost.AddServiceEndpoint(
typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(),
"mex");
netTcpHost.AddServiceEndpoint(
typeof(IDashboardService),
new NetTcpBinding(),
"dashboard");
netTcpHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
netTcpHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
netTcpHost.Open();
Console.WriteLine("Hosted at {0}. Hit any key to shut down", tcpUri);
Console.ReadLine();
netTcpHost.Close();
}