远程主机强制关闭现有连接 - WCF数据服务错误

时间:2013-01-21 10:00:15

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

我目前正在使用带有Entity Framework的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属性成为解决方案。这些绑定看起来是否正确?我错过了一些可以让我将更多数据发布到服务器的属性吗?我是否还需要在客户端上配置此服务行为?

3 个答案:

答案 0 :(得分:18)

好的,这就是我为解决问题所做的工作。首先,我通过在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 ,这表明我的绑定没有被提取。这归结为两件事,首先name配置的service部分不正确 - 它需要包含命名空间信息。我最终得到了这个(我不得不把它放在客户端的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。< / p>

我现在的头发比起初时少,但这是我付出的代价。

答案 1 :(得分:4)

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

[DataMember]
public Name { get; } 

会导致此错误。要解决问题,你应该

[DataMember]
public Name {get; set;}

答案 2 :(得分:0)

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

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

修复非常简单:只需使用IDictionary

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

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