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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2087680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:24:44  来源:igfitidea点击:

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

c#.netwcfexception-handling

提问by inutan

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:

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

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

System.Net.Sockets.SocketException: 一个现有的连接被远程主机强行关闭

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.

是因为我试图返回一个抽象类的原因吗?
如果我尝试使用单元测试调用此方法,它运行良好。

采纳答案by grenade

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

是的,问题在于您的抽象基类需要使用 knownType 和 XmlInclude 属性进行修饰。见这里:http: //geekswithblogs.net/ugandadotnet/archive/2008/05/27/serializing-an-abstract-data-contract.aspx

回答by marc_s

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.

除了手榴弹关于使 WCF 知道这些后代类使用KnownType(or ServiceKnownType) 属性的答案之外,您还必须使用[DataContract]属性本身来装饰后代类。

[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.

在 WCF 中,这些属性几乎从不从父级继承到子级——您需要在每个级别都非常明确和清楚地说明您的意图。

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

查看此博客文章All About KnownTypes以获取有关 KnownTypes 和 ServiceKnownTypes 属性的更多信息。

回答by Chitta

Add this datacontractserializer line in your web config file

在您的 Web 配置文件中添加此 datacontractserializer 行

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

回答by David d C e Freitas

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.

我曾经遇到过这个错误,这很令人困惑。我的问题是服务参考由于某种原因不是最新的,所以更新服务参考有帮助。