Bug record1: The underlying connection was closed: The connection was closed unexpectedly.Then

本文介绍了一种在使用WebService进行数据交互过程中遇到的“The underlying connection was closed”错误的解决方法。通过调整WSHttpBinding设置及增加KnownType属性,解决了因类型传递不明确导致的数据接收失败问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description:

Setting up a c# console form for testing another web service called api.svc.

After running the web service and make sure it is running correctly, we create the service reference inside tester program.


It connect the two program successfully, but when do a request, at the receiving data point, keep on meeting this error:


The underlying connection was closed: The connection was closed unexpectedly.


First I thought it would be time out or receiving data too big. So I make following changes:

  <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
          <security mode="Message">
            <!--<transport clientCredentialType="None" proxyCredentialType="None"/>-->
            <message clientCredentialType="UserName" negotiateServiceCredential="false"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>


in both project and 

 <sessionState mode="InProc" cookieless="false" timeout="40" sessionIDManagerType="BotDetect.Web.CustomSessionIdManager, BotDetect" />

on the server end.


Nothing fixed.


After test piece by piece, found here is the point that causing this error:


        [System.Xml.Serialization.XmlElementAttribute("xxxxxxxxxxCOU", typeof(xxxxxxxCOUType))]
        [System.Xml.Serialization.XmlElementAttribute("xxxxxxxxxxNew", typeof(xxxxxxxNewType))]
        [DataMember]
        public object Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }


this is a field inside a class.


Note: in order to import all the classes and its fields, methods, need to add [DataContract] at the top of the class and [DataMember] at top of all public fields.


It is not passing the possible types to the test program.

In order to inform test program about it, we need to add the following to the top of the class (Notice:not the field, but the class)

[KnownType(typeof(xxxxxxxCOUType))]
[KnownType(typeof(xxxxxxxNewType))]


Then, the test program would recognize object item which type it is.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值