C# 底层连接已关闭:连接意外关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/285345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:38:59  来源:igfitidea点击:

The underlying connection was closed: The connection was closed unexpectedly

c#asp.netweb-services.net-3.5

提问by Dave

This exception is consistently thrown on a SOAP Request which takes almost three minutes to receive and is 2.25 megs in size.

此异常始终在 SOAP 请求上抛出,该请求需要将近三分钟才能接收并且大小为 2.25 兆。

When scouring the web I find all sorts of posts which all seem to be about setting headers on the Request, some want me to not send the "Expect:" header, some want me to send the "Keep-Alive:" header, but irregardless of the headers I send I still get this pesky error. I don't believe that setting any headers is my answer, because I can recreate the exact same request using "curl" and a response does eventually come back with no problems what-so-ever.

在网上搜索时,我发现各种帖子似乎都是关于在请求上设置标题,有些人希望我不要发送“Expect:”标题,有些人希望我发送“Keep-Alive:”标题,但是不管我发送的标题如何,我仍然会遇到这个讨厌的错误。我不相信设置任何标题是我的答案,因为我可以使用“curl”重新创建完全相同的请求,并且响应最终会毫无问题地返回

My <httpRuntime maxRequestLength="409600" executionTimeout="900"/>.

我的<httpRuntime maxRequestLength="409600" executionTimeout="900"/>

I feel as if I'm running out of options. If anyone can provide any assistance I would be most grateful. A few other things to note would be that the server I'm Requesting data from is out of my hands, also these requests are over https and other requests with smaller responses work flawlessly.

我觉得我已经没有选择了。如果有人可以提供任何帮助,我将不胜感激。其他一些需要注意的事情是,我从中请求数据的服务器不在我的掌控之中,而且这些请求是通过 https 和其他响应较小的请求完美地工作的。

Thanks

谢谢

回答by mkoeller

Have you tried the sugestion of this Blog Post? The problem will most probably lie in the TCP/HTTP stack implementation of .NET .

你有没有试过这个博客帖子的sugestion ?问题很可能在于 .NET 的 TCP/HTTP 堆栈实现。

回答by Robert Wagner

You tagged the post as .NET35, so are you using WCF?

您将帖子标记为 .NET35,那么您使用的是 WCF 吗?

If so, here is an example of the App.config we use for large data sets:

如果是这样,以下是我们用于大型数据集的 App.config 示例:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:1602/EndPoint.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="IEndPointContract" name="EndPoint" behaviorConfiguration="EndpointBehaviour" />     
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndpointBehaviour">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

回答by Boris Modylevsky

I hope it's not too late for answering this question.

我希望现在回答这个问题还为时不晚。

Try adding the following attribute on the definition of your contract interface:

尝试在合约接口的定义中添加以下属性:

[ServiceKnownType(typeof(ReturnClass))]

For more generic solution that allows returning polymorphic classes please refer to this post: http://www.goeleven.com/blog/entryDetail.aspx?entry=45

有关允许返回多态类的更通用的解决方案,请参阅此帖子:http: //www.goeleven.com/blog/entryDetail.aspx?entry= 45

回答by Ahmad Th

I've got the same issue, and after deep investigations I found this article:

我遇到了同样的问题,经过深入调查,我发现了这篇文章:

Merrick Chaffer's Blog

Merrick Chaffer 的博客

It was all related to setting the "dataContractSerializer" for both client and server. Hope this to be helpful.

这一切都与为客户端和服务器设置“dataContractSerializer”有关。希望这会有所帮助。

回答by Isaac

I have added another field, but didn't have a set on the property. That was my solution for the same error.

我添加了另一个字段,但在该属性上没有设置。那是我对同样错误的解决方案。

[DataMember]
public bool HasValue
{
    get { return true; }
    set { }//adding this line made the solution.
}

回答by sharmaja

If you are using dbml instead of edmx you will get this( The underlying connection was closed: The connection was closed unexpectedly.) as dbml will not return serialisable data it needs datacontract so go to properties of dbml file and change the Serialization mode to unidirectional.

如果您使用的是 dbml 而不是 edmx,您会得到这个(底层连接已关闭:连接意外关闭。)因为 dbml 不会返回它需要数据契约的可序列化数据,所以转到 dbml 文件的属性并将序列化模式更改为单向.

回答by aatdark

i got this error because my datatransfereobjects refered to each other in an recursive manner.

我收到此错误是因为我的 datatransfereobjects 以递归方式相互引用。

For example:

例如:

Customer-> (has) -> Rating Rating-> (belong to) -> Customer

Customer-> (has) -> Rating-> (belong to) -> Customer

so you have to remove cycles.

所以你必须删除循环。

[DataContract]
public class Rating
{
    private Customer _customer;
    //[DataMember] // <-  EITHER HERE 
    public Customer Customer
    {
        get { return _customer; }
        set { _customer = value; }
    }
}


[DataContract]
public class Customer
{
    private long _customerID;
    [DataMember]
    public long CustomerID
    {
        get { return _customerID; }
        set { _customerID = value; }
    }

    [DataMember] // <- OR HERE
    public Rating Rating
    {
        get { return _rating; }
        set { _rating = value; }
    }
}

回答by SyntaxError

Tried several ways to get rid of this error message until I found this solution: http://kiranpatils.wordpress.com/2008/09/22/the-underlying-connection-was-closed-the-connection-was-closed-unexpectedly-while-returning-data-table-from-wcf-service/

尝试了几种方法来摆脱此错误消息,直到找到此解决方案:http: //kiranpatils.wordpress.com/2008/09/22/the-underlying-connection-was-closed-the-connection-was-closed-意外返回数据表来自 wcf 服务/

You may change your List<> to DataSet. I suspect DataSet can handle much amount of data than the List<>.

您可以将 List<> 更改为 DataSet。我怀疑 DataSet 可以处理比 List<> 多得多的数据。

Hope it helps.

希望能帮助到你。

回答by Roath So

For WCF with EF, just add the following code in the context class.

对于带有 EF 的 WCF,只需在上下文类中添加以下代码。

base.Configuration.ProxyCreationEnabled = false;

base.Configuration.ProxyCreationEnabled = false;

回答by Justin Clarke

This is a generic error raised if there is an internal error.

如果存在内部错误,则这是引发的一般错误。

Try adding tracing here: http://msdn.microsoft.com/en-us/library/ms732023(v=vs.110).aspx

尝试在此处添加跟踪:http: //msdn.microsoft.com/en-us/library/ms732023(v=vs.110).aspx

You will see the full log then.

然后你会看到完整的日志。