WCF中的远程主机强制关闭现有连接 [英] An existing connection was forcibly closed by the remote host in WCF
问题描述
我有一个名为Template的抽象类,定义为:
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)
我得到这个错误:
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
这篇关于WCF中的远程主机强制关闭现有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!