开发者

An existing connection was forcibly closed by the remote host in WCF

I have an abstract class called 'Template' defined as:

[DataContract]
public abstract class Template
{
    [DataMember]
    public virtual int? Id { get; set; }
    [DataMember]
    public virtual string Title { get; set; }
    [DataMember]
    public virtual byte[] TemplateDoc { get; set; }
    [DataMember]
    public virtual bool IsSystemTemplate { get; set; }        
}

Two derived classes: UserTemplate and SystemTemplate implements above abstract class, which are defined as:

public class UserTemplate : Template
{
    [DataMember]
    public virtual Int32? OfficeId { get; set; }

    [DataMember]
    public virtual Int32? UserId { get; set; }

    protected UserTemplate() { }

    public UserTemplate(string title, byte[] templateDoc, string templateDocName, TemplateType templateType, int officeId, int? userId)
    {
        this.Title = title;
        this.TemplateDoc = templateDoc;
        this.IsSystemTemplate = false;
        this.OfficeId = officeId;
        this.UserId = userId;
    }
}

public class SystemTemplate : Template
{
    [DataMember]
    public virtual Int32? MultiListGroupId { get; set; }

    protected SystemTemplate() { }

    public SystemTemplate(string title, byte[] templateDoc, string templateDocName, TemplateType templateType, int multiListGroupId)
    {
        this.Title = title;
        this.TemplateDoc = templateDoc;
        this.IsSy开发者_高级运维stemTemplate = true;
        this.MultiListGroupId = multiListGroupId;
    }
}

Now, when I try to call following service method:

List<Template> GetTemplatesByTemplateType(int officeId, int? userId, TemplateType templateType)

I get this error:

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

Is it because of the reason that I am trying to return an abstract class?

It runs fine if I try to call this method using unit test.


Yes, the problem is your abstract base class which needs to be decorated with the KnownType and XmlInclude attributes. See here: http://geekswithblogs.net/ugandadotnet/archive/2008/05/27/serializing-an-abstract-data-contract.aspx


In addition to grenade's answer about making those descendant classes known to WCF usign the KnownType (or ServiceKnownType) attribute, you'll also have to decorate the descendant classes with a [DataContract] attribute themselves.

[DataContract]
public class UserTemplate : Template
{
    ......
}

[DataContract]
public class SystemTemplate : Template
{
    ......
}

These attributes are almost never inherited from parent to child in WCF - you need to be very explicit and clear about your intent at every level.

Check this blog post All About KnownTypes for more information on the KnownTypes and ServiceKnownTypes attributes.


Add this datacontractserializer line in your web config file

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior >
        **<dataContractSerializer maxItemsInObjectGraph="2147483646"/>**
     </behavior >
 </serviceBehaviors>
</behaviors>
</system.serviceModel>


I got this error once, and it was quite confusing. My problem was that the Service Reference was for some reason not up to date, so updating the Service Reference helped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜