【问题标题】:WCF Connection closed with complex dataWCF 连接因复杂数据而关闭
【发布时间】:2012-08-08 14:21:20
【问题描述】:

我有一个 WCF 服务,在这个服务中,我返回一个包含许多属性的类,其中一些属性本身就是类,它有点复杂但不是很大。我用另一个 WCF 服务在同一个项目上做了类似的事情,一切都很好。但是这个在我使用的时候给了我这个错误。

[System.ServiceModel.CommunicationException]    {"An error occurred while receiving the HTTP response to http://xxx/Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."}  System.ServiceModel.CommunicationException

内部异常是

[System.Net.WebException]   {"The underlying connection was closed: An unexpected error occurred on a receive."}    System.Net.WebException

内部的例外是说

[System.IO.IOException] {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}   System.IO.IOException

内部的例外是说

[System.Net.Sockets.SocketException]    {"An existing connection was forcibly closed by the remote host"}   System.Net.Sockets.SocketException

所以,这就是我尝试过的。我认为我的 .svc 文件中可能有一些错误,或者我的 .config 文件的配置中可能有一些错误,但据我所知没有。然后,我想我应该尝试看看我是否可以从服务器向客户端发送一个简单的类型。因此,我创建了一个名为 GetInt() 的方法,它返回 7。我在客户端上调用了它,它运行良好。因此,我认为这是我从服务器发回的数据不受支持。我不明白为什么,因为正如我所说,我之前在同一个项目上(就在昨天)发送了复杂类型,并且一切正常。无论如何,这是我要发送的课程。也许,有人可以指出可能不支持的内容。或者也许有人知道还有什么可能导致这种情况。

    public class Hotels
    {
        public string Language { get; set; }
        public string Datum { get; set; }
        public  List<HotelListing> HotelListing { get; set; }
    }

    public class HotelListing
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
        public string Category { get; set; }
        public List<PhoneNumber> PhoneNumbers { get; set; }
        public Address Address { get; set; }
        public Content Content { get; set; }
    }

    public class Content
    {
        public Description Description { get; set; }
        public List<Review> Reviews { get; set; }
        public Image Image { get; set; }
        public AtrttributeData AtrttributeData { get; set; }
    }

    public class AtrttributeData
    {
        public string Link { get; set; }
        public string Title { get; set; }
        public NameValueCollection Attributes { get; set; }
    }

    public class Image
    {
        public string Type { get; set; }
        public string ImageURL { get; set; }
        public string HotelURL { get; set; }
    }

    public class Review
    {
        public string Type { get; set; }
        public string Link { get; set; }
        public string Author { get; set; }
        public string Body { get; set; }
        public NameValueCollection Ratings { get; set; } 
    }

    public class Description
    {
        public string Link { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
    }

    public class Address
    {
        public string Format { get; set; }
        public string Addr1 { get; set; }
        public string Addr2 { get; set; }
        public string City { get; set; }
        public string PostCode { get; set; }
    }

    public class PhoneNumber
    {
        public string Type { get; set; }
        public string Number { get; set; }
    }

有问题的方法是这样的

[OperationContract]
Hotels GetHotels();

任何帮助将不胜感激。

这是我的配置部分

<binding name="BasicHttpBinding_ITablet" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>

<snip>

<endpoint address="http://xxx/Service.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITablet"
                contract="SupplierInterfaceTabletService.ITablet" name="BasicHttpBinding_ITablet" />

谢谢,

萨钦

【问题讨论】:

  • 请从您的应用程序配置文件中添加您的 system.serviceModel 部分。可能是您没有将最大消息大小设置得足够大,或者其他与消息大小相关的配置设置。
  • 我会开启追踪功能,看看您能否获得更多诊断信息。配置跟踪描述here。
  • 服务器端还是客户端?

标签: .net wcf datacontract operationcontract


【解决方案1】:

尝试返回 DataContract(不是 Class)。这是一个带有另一个 DataContract 列表和继承示例的 DataContract。

[DataContract]
public class sDoc
{
    [DataMember]
    public int sID;
    [DataMember]
    public int sParID;
    [DataMember]
    public List<sDocProp> Props;
    [DataMember]
    public string SessionID;

    public string NotDataMember;
}

[DataContract]
[KnownType(typeof(sDocPropStringSV))]
public class sDocProp
{
    [DataMember]
    public int ID;
    [DataMember]
    public string Name;
    [DataMember]
    public ArrivalStatus ArrivalStatus;
}

[DataContract]
public class sDocPropStringSV : sDocProp
{
    [DataMember]
    public string Value;
}

【讨论】:

  • 这不是真的:DataContract 和 DataMember 属性是可选的。如果不存在,则该类的所有公共属性都将被序列化。
  • 是的,我昨天没有对我的另一个示例执行此操作,并且一切正常。我的客户端有这种东西。
  • @CodeCaster OK 我改了试试。
  • 该问题并未表明使用DataContracts。我会分解它并返回一些非常简单的东西并从那里构建。
【解决方案2】:

我喜欢 .NET,它有时太垃圾了。问题是 NameValueCollections。由于某种原因,WCF 在将这些从服务器传递到客户端时遇到了问题。算了!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
相关资源
最近更新 更多