【问题标题】:Strange error with WCF REST ServicesWCF REST 服务的奇怪错误
【发布时间】:2014-01-13 14:41:41
【问题描述】:

我突然开始面临以下问题。我的 REST 服务不再有效。它在一段时间前工作,我没有对配置进行任何更改。突然之间,它的行为很奇怪。我使用 WCF 测试客户端检查问题,奇怪的是,服务方法被调用了 6 次,最后抛出以下错误。

错误:无法从http://localhost:49796/Test.svc/GetInformation?memberId=1 获取元数据如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange 错误 URI:http://localhost:49796/Test.svc/GetInformation?memberId=1 元数据包含无法解析的引用的 MSDN 文档:“http://localhost:49796/Test.svc/GetInformation?memberId=1”。远程服务器返回意外响应:(405) 方法不允许。远程服务器返回错误:(405) Method Not Allowed.HTTP GET Error URI:

http://localhost:49796/Test.svc/GetInformation?memberId=1

There was an error downloading 'http://localhost:49796/Test.svc/GetInformation?memberId=1'.    The underlying connection was closed: An unexpected error occurred on a receive.    Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.    An existing connection was forcibly closed by the remote host

最令人惊讶的是,即使我创建一个新服务或测试一个旧服务,它也是一样的。我的电脑在全球范围内出现问题了吗?由于我电脑中的所有 WCF REST 服务的行为都相同,这似乎是某些文件损坏或全局配置更改。我用谷歌搜索,但找不到任何正确的答案。

我尝试删除端点并使用测试客户端再次测试。由于测试客户端自动添加了 basichttpbinding,因此使用测试客户端可以正常工作。

如果有人知道,请提供有关修复的建议。

我的配置如下所示:

<system.serviceModel>
<services>
  <service name="TestService.ServiceLayer.Services.TestService" behaviorConfiguration="TestBehavior">
    <endpoint address="" binding="webHttpBinding" 
              contract="TestService.ServiceLayer.Services.ITestService" behaviorConfiguration="web" />
   </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="TestBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

我的服务合同如下

 [ServiceContract]
public interface ITestService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetInformation?memberId={memberId}")]
    List<MyTestClass> GetInformation(string memberId);
}

我进一步分析发现,如果我将 List 更改为字符串,它可以正常工作。我在服务合同之上还添加了 KnownType,但它不起作用。请就这一行提出建议。

【问题讨论】:

    标签: .net wcf rest


    【解决方案1】:

    在您的配置中,我没有看到 Mex 绑定。没有它,我认为无法从服务中获取元数据。 mex 指 mexHttpBinding

    粗略地说,它看起来像这样:

    <services>
       <service name="MyService" behaviorConfiguration="MEX">
        <endpoint
           address="http://localhost:8000/MEX"
           binding="mexHttpBinding"
           contract="IMetadataExchange"        />
        </service>
    </services>
    
    <behaviors>
       <serviceBehaviors>
          <behavior name="MEX">
            <serviceMetadata/>
          </behavior>
        </serviceBehaviors>
    </behaviors>
    

    【讨论】:

    • 我已经添加了这个并测试了但没有用。我看到的主要问题是它被执行了 6 次。每次我看到正确的数据被返回,所以没有代码问题。唯一的问题是我无法弄清楚的东西被损坏了!
    【解决方案2】:

    启用 wcf 跟踪并查看 SvcTraceViewer.exe 中的跟踪日志。查看以下链接了解如何在 wcf 中启用跟踪。

    4 simple steps to enable tracing in WCF

    如何使用 SvcTraceViewer

    Using Service Trace Viewer for Viewing Correlated Traces and Troubleshooting

    【讨论】:

    • 按照链接中提供的步骤进行操作
    • 在 SvcTraceViewer.exe 中查看服务错误。它将提供详细的错误信息
    • 检查日志文件的位置
    • 我现在可以生成日志并查看某些内容,但无法理解错误 schemas.microsoft.com/2004/06/ServiceModel/Management/…">
    • 仅通过查看错误消息很难判断您的服务有什么问题。我在我的答案中添加了一个链接如何使用 svctraceviewer 实用程序
    【解决方案3】:

    分辨率令人兴奋。我看到其中一个数据协定字段在 DB 中为 NULL,但在 C# 中它不是 Nullable 类型。这导致我的 REST 服务出现奇怪的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2012-08-13
      相关资源
      最近更新 更多