c# - 由于服务操作量大,添加服务引用时出错

标签 c# .net wcf

我有一份包含足够操作量(约 40 次操作)的服务契约(Contract)。当我尝试从我的客户端项目(或什至从 WCF Test Client )添加服务引用时,出现错误:

Error: Cannot obtain Metadata from net.tcp://localhost:12345/DataProvider/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: net.tcp://localhost:12345/DataProvider/mex Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:12345/DataProvider/mex'. There is an error in the XML document. The value for the 'type' attribute is invalid - 'q2:PersonFilter' is an invalid value for the 'type' attribute.

奇怪的行为从这里开始:什么都不做,只是删除一个的服务操作,一切都非常顺利 - 添加服务引用效果非常好。当然,我并没有尝试删除这 40 种方法中的所有方法,但我已经尝试了 5 种方法。 另一件重要的事情 - 我没有删除使用 PersonFilter(错误消息中显示的类型)操作的方法。

我不知道如何解决这个问题。如果需要更多信息,请告诉我。

更新#1

所以,我刚刚尝试设置 mex 自定义绑定(bind)以增加默认配额:

  <customBinding>
    <binding name="mexBinding">
      <binaryMessageEncoding>
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
                      maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                      maxNameTableCharCount="2147483647" />
      </binaryMessageEncoding>
      <tcpTransport transferMode="Buffered" maxReceivedMessageSize="2147483647" 
                    maxBufferSize="2147483647" />
    </binding>
  </customBinding>

  <service name="MyNamespace.DataService" behaviorConfiguration="myServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:12345/DataProvider/" />
      </baseAddresses>
    </host>

    <endpoint binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
              contract="MyNamespace.IDataService" />
    <endpoint address="mex" binding="customBinding" bindingConfiguration="mexBinding"
              name="tcp" contract="IMetadataExchange" />
  </service>

但这无济于事 - 仍然出现同样的错误。

更新#2

使用 ServiceModel Metadata Utility Tool (Svcutil.exe)我尝试从以下 3 个来源获取元数据:

  1. HTTP 主机
  2. TCP 主机
  3. 直接组装

至于来源 1. 和 3. - 一切顺利。我能够获取元数据并生成客户端代码,但是当我通过 TCP 主机调用 svcutil 时,我得到:

Microsoft (R) Service Model Metadata Tool [Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1] Copyright (c) Microsoft Corporation. All rights reserved.

Attempting to download metadata from 'net.tcp://localhost:12345/DataProvider/mex' using WS-Metadata Exchange. This URL does not support DISCO.

Microsoft (R) Service Model Metadata Tool [Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1] Copyright (c) Microsoft Corporation. All rights reserved.

Error: Cannot obtain Metadata from net.tcp://localhost:12345/DataProvider/mex

If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.

WS-Metadata Exchange Error URI: net.tcp://localhost:12345/DataProvider/mex

Metadata contains a reference that cannot be resolved: 'net.tcp://12345/DataProvider/Management/mex'.

There is an error in the XML document.

The value for the 'type' attribute is invalid - 'q2:PersonFilter' is an invalid value for the 'type' attribute.

If you would like more help, type "svcutil /?"

我仍然不知道我的 wcf 服务或其配置有什么问题。

最佳答案

通常先让服务使用基本的 http 绑定(bind)然后再将其更改为 net tcp 会更容易。

除了定义 mex 端点之外,您还需要启用元数据交换:

<behaviors> 
  <serviceBehaviors> 
    <behavior name="myServiceBehavior"> 
        <serviceDebug includeExceptionDetailInFaults="True" /> 
        <serviceMetadata /> 
    </behavior> 
   </serviceBehaviors> 
 </behaviors> 

没有 <serviceMetadata />元素 mex 端点将不起作用。

关于c# - 由于服务操作量大,添加服务引用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739499/

相关文章:

c# - SOAP 是简单对象访问协议(protocol)还是面向服务的应用程序平台?

c# - Revit API - 错误的完整类名

c# - 确定主题 ID 以在 CHM 文件中打开它

C# 控制台应用程序点效果

c# - 我应该为 64 位 Windows 操作系统构建 x86 还是 x64 应用程序?

c# - NativeApplicationClient 不受任何支持

c# - 蓝牙串行端口 (SPP) 传入端口创建

c# - 查找属于两个事件目录组的用户

c# - ASP.Net Web API 与 WCF,我应该在我的项目中选择哪一个

c# - WCF 服务 API 的哈希