【问题标题】:Trouble with WCF (net.tcp) endpoint discovery using svcutil使用 svcutil 的 WCF (net.tcp) 端点发现问题
【发布时间】:2014-08-29 14:44:35
【问题描述】:

我有一个项目,我正在尝试通过 TCP 传输实现一个简单的 WCF 服务。这就是下面共享的服务端代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace HwWcfService
{
    class Program
    {
        static void Main(string[] args)
        {


            string Uri = string.Format("net.tcp://localhost:8000/TelemetryProviderService");
            ServiceHost host = new ServiceHost(
                    typeof(Calculator),
                    new Uri(Uri)
                    );


            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);                
            host.AddServiceEndpoint(
                typeof(ICalc),
                binding,
                "CalcService");

            host.Open();

            Console.WriteLine("Done starting the new endpoint...");
            Console.ReadKey();

        }
    }



    class Calculator :ICalc
    {

        public int Add(int x, int y)
        {
            Console.WriteLine(x + " " + y);
            return x + y;
        }
    }
}





namespace HwWcfService
{
    [ServiceContract(
        Namespace="HwWcfService"
    )]
    interface ICalc
    {

        [OperationContract]
        int Add(int x, int y);
    }
}

App.Config 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <system.serviceModel>
      <services>
        <service behaviorConfiguration="ServiceBehaviour" name="HwWcfService.Calculator">
          <endpoint address="net.tcp://localhost:8000/TelemetryProviderService" 
                    name="TelemetryProviderService"
                    contract="HwWcfService.ICalc"
                    binding="netTcpBinding"  
                    bindingConfiguration="calcTcpBindingConfiguration"
                    behaviorConfiguration="epBehaviourConfig"
                    />

          <endpoint address="net.tcp://localhost:8000/TelemetryProviderServiceMex"
                    name ="TelemetryProviderServiceMex"
                    binding="mexTcpBinding"
                    contract="HwWcfService.ICalc"
                    listenUriMode="Explicit"
                    />
        </service>
      </services>

      <bindings>
        <netTcpBinding>
          <binding name="calcTcpBindingConfiguration" closeTimeout="00:02:00"/>
        </netTcpBinding>
      </bindings>


      <behaviors>

        <endpointBehaviors>
          <behavior name="epBehaviourConfig">
          </behavior>
        </endpointBehaviors>

        <serviceBehaviors>
          <behavior name="ServiceBehaviour">
            <serviceMetadata httpGetUrl="false" httpGetEnabled="false"/>
          </behavior>
        </serviceBehaviors>

      </behaviors>

    </system.serviceModel>
</configuration>

当我运行 svcutil.exe 来查询所有参数时,我得到了错误: 错误:无法从 net.tcp://localhost:8000/TelemetryProviderService 获取元数据

如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在 spe 启用元数据发布 化地址。如需启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455 的 MSDN 文档。

WS-元数据交换错误 URI:net.tcp://localhost:8000/TelemetryProviderService

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/TelemetryProviderService'.

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote hos

,或底层网络资源问题。本地套接字超时为 '00:04:59.9929977'。

An existing connection was forcibly closed by the remote host

有人知道我为什么会收到这个错误吗?

【问题讨论】:

    标签: c# .net wcf tcp


    【解决方案1】:
    <serviceMetadata httpGetUrl="false" httpGetEnabled="false"/>
    

    您需要将这两个都设置为 true,以便 svcutil.exe 能够查询您的服务。

    来自 MSDN 上的 svcutil.exe:

    当您使用 Svcutil 访问一个 WSDL 文档时,该文档引用了 安全令牌服务 (STS),Svcutil 生成 WS-MetadataExchange 致电 STS。但是,该服务可以公开其 WSDL 文档 使用 WS-MetadataExchange 或 HTTP GET。因此,如果 STS 仅使用 HTTP GET 公开了 WSDL 文档,客户端编写在 WinFX 将失败。对于使用 .NET Framework 3.5 编写的客户端,Svcutil 将尝试使用 WS-MetadataExchange 和 HTTP GET 来获取 STS WSDL。

    编辑:此外,在检查 MSDN 以获取 &lt;serviceMetadata/&gt; 时,您需要将其更改为以下内容:

    <serviceMetadata httpGetEnabled="false"/>
    

    默认情况下,您应该无法通过端点地址访问 Web 服务的 WSDL,但在 URL 中包含“?wsdl”。

    链接:MSDN <serviceMetadata> Element Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多