一个现有的连接被强行WCF中远程主机关闭

人气:1,029 发布:2022-09-10 标签: .net c# wcf exception-handling

问题描述

我有一个抽象类,称为模板定义为:

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; }        
}

两个派生类:UserTemplate和SystemTemplate实现上述抽象类,其定义为:

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:一个现有的连接被强行关闭远程主机

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.

推荐答案

是的,问题是它需要与KnownType装饰和XmlInclude属性的抽象基类。在这里看到:http://geekswithblogs.net/ugandadotnet/archive/2008/05/27/serializing-an-abstract-data-contract.aspx

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

736