WCF 这可能是由于服务端点绑定(bind)未使用 HTTP 协议(protocol)

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

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 响应时发生错误。这可能是由于服务端点绑定(bind)未使用 HTTP 协议(protocol)。这也可能是由于 HTTP 请求上下文被服务器中止(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。

我从 Server trace viewer 得到了这个异常信息,所以请不要建议我放置 <-system.diagnostics-> 标签。

正如你所看到的,我增加了所有尺寸的东西。

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

最佳答案

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

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

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

关于WCF 这可能是由于服务端点绑定(bind)未使用 HTTP 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19123350/

相关文章:

WCF 错误仅使用 wsHttpBinding 发送到 SOAP 1.1 时 SOAP 1.2 消息无效

.net - ASP.NET网站->WCF服务->WCF服务,一路冒充?

Azure 云服务上的 WCF 与 Azure 网站客户端

wcf - 如何使用 System.ServiceModel 在关联的配置文件中启用智能感知

.net - 在 wcf 服务调用上设置 CultureInfo?

wcf - 如何从 WCF 服务返回干净的 JSON?

windows-8 - 如何将自定义授权 header 添加到Windows Store App OData客户端?

wcf-data-services - 如何从 WCF 数据服务返回文件?

c# - 调度程序 BeginInvoke 语法

wcf - 在 WCF 服务中传输大量数据