【问题标题】:An existing connection was forcibly closed by the remote host - WCF Data Service error远程主机强制关闭现有连接 - WCF 数据服务错误
【发布时间】:2013-01-21 10:00:15
【问题描述】:

我目前正在将 WCF 数据服务(嗯,ADO.Net 数据服务)与实体框架一起使用,并且在执行 POST 时收到以下错误(省略/更改了一些不相关的信息):

<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code /><m:message xml:lang="en-GB">The URI 'http://localhost:56568/MyWcfDataServices.svc/EntitySetName(pk_id)' is not valid for POST operation. For POST operations, the URI must refer to a service operation or an entity set.</m:message></m:error>

在网上浏览了一下,我似乎找不到关于这条消息的太多信息,所以调试它有点困难。这很可能会发生,因为我发布的属性之一是一个大的 base64 字符串(如果我不发布它就可以正常工作)。我尝试将maxRequestLength 设置为以下:

<httpRuntime maxRequestLength="102400" />

但这似乎没有帮助。虽然我会继续解决这个问题,但我想我会在这里快速发布一个帖子,看看是否有人知道可能有帮助的东西。

我的 WCF 数据服务类如下所示:

public class MyWcfDataServices: DataService<ContextName>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        // set entity access rules etc
    }
}

谢谢

编辑:我似乎对此一无所知。到目前为止,这是我尝试过的。

在服务器端,我设置了以下绑定:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                             minFreeMemoryPercentageToActivateService="0" />
  <services>
    <service name="MyWcfDataServices" 
             behaviorConfiguration="myBehaviorConfig">
      <endpoint address=""
                binding="webHttpBinding"
                bindingConfiguration=""
                contract="System.Data.Services.IRequestHandler" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="myBehaviorConfig">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

希望maxItemsInObjectGraph 属性是解决方案。这些绑定看起来正确吗?我是否遗漏了一些可以让我向服务器发布更多数据的属性?我是否也需要在客户端上配置此服务行为?

【问题讨论】:

    标签: c# entity-framework wcf-data-services


    【解决方案1】:

    好的,这就是我解决问题的方法。首先,我通过将以下内容添加到我的 web.config 来启用服务器上的跟踪:

    <system.diagnostics>
      <sources>
        <source name="System.ServiceModel"
                switchValue="Information, ActivityTracing"
                propagateActivity="true">
          <listeners>
            <add name="traceListener"
                type="System.Diagnostics.XmlWriterTraceListener"
                initializeData="c:\logs\Traces.svclog"  />
          </listeners>
        </source>
      </sources>
    </system.diagnostics>
    

    这为我提供了有关问题的更多信息,特别是在我的情况下,最大请求长度仍然是 65536,这表明我的绑定没有被拾取。这归结为两件事,首先我的service 配置中的name 部分不正确——它需要包含命名空间信息。我最终得到了这个(我也必须把它放在客户端的 web.config 中):

    <services>
      <service name="Solution.Project.MyWcfDataServices">
        <endpoint address=""
                  binding="webHttpBinding"
                  bindingConfiguration="webHttpConfig"
                  contract="System.Data.Services.IRequestHandler" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpConfig" 
                  allowCookies="true"
                  maxReceivedMessageSize="20000000"
                  maxBufferSize="20000000"
                  maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32"
                        maxArrayLength="200000000"
                        maxStringContentLength="200000000" />
        </binding>
      </webHttpBinding>
    </bindings>
    

    最后,我必须将我的 .svc 文件标记中的工厂从生成的模板 System.Data.Services 程序集更改为 Microsoft 的 ODATA 程序集(其中也包含 System.Data.Services 命名空间)System.Data.Services.DataServiceHostFactory, Microsoft.Data.Services, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

    我现在的头发比刚开始时少,但我想这就是你要付出的代价。

    【讨论】:

    • 你应该得到两票,一张是答案,一张是“少发”笑话。唉,我只能投票一次。
    【解决方案2】:

    我遇到了这个问题,因为我的 DataContract 中的一些 DataMembers 只有 get 方法。例如

    [DataMember]
    public Name { get; } 
    

    会导致这个错误。要解决您应该遇到的问题

    [DataMember]
    public Name {get; set;}
    

    【讨论】:

    • 你拯救了我的一天,谢谢,我不得不摆脱 set,因为我设置了无异常的 xml 解析值,所以不需要设置,但是空的 set 块也可以,例如设置 {}
    【解决方案3】:

    此错误的另一个原因是尝试将IReadOnlyDictionary 声明为DataMember

    [DataMember]
    public IReadOnlyDictionary<String, Object> Foo { get; set; }
    

    解决方法很简单:只需改用IDictionary

    [DataMember]
    public IDictionary<String, Object> Foo { get; set; }
    

    我认为这也适用于IReadOnlyCollection 的其他实现,例如IReadOnlyList

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 2011-01-06
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多