Common WCF Exception : Connection Unexpectedly Closed

后端 未结 12 1008
耶瑟儿~
耶瑟儿~ 2021-02-05 17:19

I have three projects. One is a WCF Services Project, one is a WPF Project, and one is a Microsoft Unit Testing Project. I setup the WCF Services project with a data object th

相关标签:
12条回答
  • 2021-02-05 17:49

    As you yourself noted, if you mark the enum as DataContract, you'll have to mark the items, too.

    As an alternative you could just remove the [DataContract] before your enum like this:

    public enum Priority
    {    
      Low,    
      Medium,    
      High
    }
    

    This would work, too, because in this case WCF handles the enum by itself. If you mark it as [DataContract] you have to mark every item as you noticed yourself.

    0 讨论(0)
  • 2021-02-05 17:49

    in my case i was returning a custom class object, one of the members of which was a data table. and if you dont have a name on the datatable it will throw this error.

    Dim oTable As DataTable = New DataTable 'this wont serialize
    Dim oTable As DataTable = New DataTable("MyTable")  'this will serialize
    
    0 讨论(0)
  • 2021-02-05 17:58

    Sometimes this error could be very misleading. Common WCF Exception : Connection Unexpectedly Closed can be occurred when the culture is not set correctly or in string formating as well.

    Following fails:

    new DateTime(adate.Year, adate.Month, firstday).ToString("d", cultureInfo);
    

    while this works:

    CultureInfo culture = new CultureInfo(this.aculture.Name);               
    Convert.ToString(new DateTime(adate.Year, adate.Month, firstday), culture);
    
    0 讨论(0)
  • 2021-02-05 18:01

    In case someone else is also doing this, I returning a List of objects that were generated by linq to sql / dbml file. I just had to enable serialization in the dbml file :

    http://blogs.msdn.com/b/wriju/archive/2007/11/27/linq-to-sql-enabling-dbml-file-for-wcf.aspx

    cheers

    0 讨论(0)
  • 2021-02-05 18:01

    Another reason: This exception comes up if you have DataContract/DataMember attributes on an Interface instead of a concrete type (terrible idea, don't do it) and you are trying to serialize the Concrete type.

    0 讨论(0)
  • 2021-02-05 18:02

    Someone on this thread posted that adding this element to the endpoint behavior fixed the issue.

    <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    

    This worked but it had to be added not only to the endpoint behavior but the service behavior too (which makes sense since that is where the serialization will take place).

    If it was added to the service only I got this error "Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota."

    If added to the endpoint only I still got the connection unexpectedly closed error.

    0 讨论(0)
提交回复
热议问题