使用svcutil解决WCF(net.tcp)端点发现问题

时间:2014-08-29 14:44:35

标签: c# .net wcf tcp

我有一个项目,我正在尝试通过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-Metadata Exchange错误     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

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

1 个答案:

答案 0 :(得分:0)

<serviceMetadata httpGetUrl="false" httpGetEnabled="false"/>

您需要将这两个设置为true才能让svcutil.exe能够查询您的服务。

来自svcutil.exe上的MSDN:

  

使用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。

修改:还要检查<serviceMetadata/>的MSDN,您需要将其更改为以下内容:

<serviceMetadata httpGetEnabled="false"/>

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

链接:MSDN <serviceMetadata> Element Documentation