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

9

我目前正在使用带有实体框架的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属性可以解决问题。这些绑定看起来正确吗?我是否缺少某个属性,以便让我向服务器POST更多数据?我是否还需要在客户端上配置此服务行为?

3个回答

19

好的,这是我为解决问题所做的事情。首先,我通过将以下内容添加到我的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,版本=5.2.0.0,文化=中立,PublicKeyToken=31bf3856ad364e35

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


你应该得到两个赞,一个是为了回答,另一个是为了“少头发”的笑话。可惜,我只能点赞一次。 - John Zabroski

9
我遇到这个问题是因为我的DataContract中的一些DataMembers只有get方法。例如:
[DataMember]
public Name { get; } 

会导致这个错误。要解决问题,您应该有

[DataMember]
public Name {get; set;}

你救了我的一天,谢谢。因为我自己解析的XML值是无异常的,所以不需要使用set,但是空的set块也可以起到作用,例如 set {}。 - Shantu

2
另一个导致此错误的原因是尝试将 IReadOnlyDictionary 声明为 DataMember
[DataMember]
public IReadOnlyDictionary<String, Object> Foo { get; set; }

修复很容易:只需使用IDictionary即可。
[DataMember]
public IDictionary<String, Object> Foo { get; set; }

我假设这也适用于其他实现了IReadOnlyCollection的接口,比如IReadOnlyList

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接