【问题标题】:WCF This could be due to the service endpoint binding not using the HTTP protocolWCF 这可能是由于服务端点绑定未使用 HTTP 协议
【发布时间】:2013-10-08 00:51:33
【问题描述】:

Default.aspx.cs

WCFService.Service1Client client = new WCFService.Service1Client();
string stream = client.JsonSerializeFromDatabase();
client.Close();
WCFService.Service1Client client2 = new WCFService.Service1Client();
foreach (WCFService.Person in client2.JsonDeserializeFromDatabase(stream)) 

Service1.svc.cs

public IList<Person> JsonDeserializeFromDatabase(string value)
{
    MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(value));
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Person>));
    IList<Person> tableData = (IList<Person>)ser.ReadObject(ms);
    ms.Close();
    ms.Dispose();

    return tableData;
}

IService1.cs

[OperationContract]
IList<Person> JsonDeserializeFromDatabase(string value);

服务器 Web.config

    <httpRuntime maxRequestLength="8192"/>
</system.web>
...
<system.serviceModel>
<services>
    <service name="TestWCF.Service1" behaviorConfiguration="TestWCF.Service1Behavior">
        <endpoint address="" binding="wsHttpBinding" contract="TestWCF.IService1">
            <identity>
                <dns value="localhost"/>
            </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
</services>
<behaviors>
<serviceBehaviors>
    <behavior name="TestWCF.Service1Behavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true"/>
    <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
</serviceBehaviors>

客户端 Web.config

    <httpRuntime maxRequestLength="8192"/>
</system.web>
<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="debuggingBehaviour">
                <dataContractSerializer maxItemsInObjectGraph="2147483646" />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService1" closeTimeout="00:50:00" openTimeout="00:50:00" receiveTimeout="00:50:00" sendTimeout="00:50:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
            <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
            <reliableSession ordered="true" inactivityTimeout="00:50:00" enabled="false"/>
            <security mode="Message">
            <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>
            </security>
        </binding>
    </wsHttpBinding>
</bindings>
<client>
    <endpoint address="~~~~~/Service1.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="WCFService.IService1" name="WSHttpBinding_IService1" behaviorConfiguration="debuggingBehaviour">

异常信息
- 类型:System.ServiceModel.CommunicationException,System.ServiceModel,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089
- 消息:接收到 ~~~~~/Service1.svc 的 HTTP 响应时发生错误。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。

我从服务器跟踪查看器获得了这个异常信息,所以请不要建议我放置 标签。

如您所见,我增加了所有尺寸的东西。

就像.. 我不知道为什么我在调用 JsonDeserializeFromDatabase(stream) 时会出错。
“接收到 ~~~~~/Service1.svc 的 HTTP 响应时出错。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。”

【问题讨论】:

  • 尝试使用byte[]作为流参数(序列化/反序列化)

标签: wcf wcf-data-services wcf-binding httpresponse json-deserialization


【解决方案1】:

从 WCF 服务中的数据库返回记录时,我也遇到过此错误消息。除了在客户端配置 (App.config) 中的绑定中增加 maxReceivedMessageSize 之外,另一个问题似乎是 WCF 在序列化 Entity Framework 对象时存在问题,如果它们的关系会导致其对象图中的循环。

我通过返回伙伴类对象(它们是原始数据库记录的副本,但没有任何关系链接)而不是原始数据库类本身来解决这个问题。

希望这会有所帮助 - 为什么微软不产生更好的错误信息?在这里,与许多其他情况一样,错误消息没有提供真正问题的线索(WCF 调用的返回值的序列化)!

【讨论】:

    【解决方案2】:

    re:WCF 和序列化 Entity Framework 对象的问题,如果它们的关系会导致其对象图中的循环。我遇到了同样的错误和 user1956642 提供的答案,它确实为我指明了正确的方向,但后来意识到我可以通过配置 DbContext 来序列化这些实体

    context.Configuration.ProxyCreationEnabled = false;
    

    仍然启用延迟加载,但我相信动态代理用于更改跟踪和延迟加载。所以是的......只是我的 5c

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多