Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...