【问题标题】:Unable to add net.tcp as service reference无法将 net.tcp 添加为服务参考
【发布时间】:2014-07-24 15:03:53
【问题描述】:

我已经像这样创建了一个 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();

这是我的IDashboardServiceDashboardSerivce 定义:

[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

【问题讨论】:

    标签: c# wcf tcp metadata nettcpbinding


    【解决方案1】:

    错误明确提及 “无法获取元数据...”,因此值得检查您是否拥有 TCP Mex 端点。它在配置中应该是这样的:

    <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();
    }
    

    【讨论】:

    • 我正在以编程方式创建它(即没有 app/web.config)。我将如何以编程方式做到这一点?
    • 我尝试这样做时遇到错误:在服务 Services.DashboardService 实施的合同列表中找不到合同名称“IMetadataExchange”。将 serivceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost 以启用对此合同的支持
    • 谢谢,我明天试试,告诉你
    猜你喜欢
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    相关资源
    最近更新 更多