Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.IsSystemTemplate = 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.

like image 456
inutan Avatar asked Jan 18 '10 16:01

inutan


2 Answers

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

like image 174
grenade Avatar answered Sep 23 '22 21:09

grenade


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.

like image 23
marc_s Avatar answered Sep 20 '22 21:09

marc_s