Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF方法返回集合时ServiceKnownType异常_C#_.net_Wcf_Serialization_Datacontractserializer - Fatal编程技术网

C# WCF方法返回集合时ServiceKnownType异常

C# WCF方法返回集合时ServiceKnownType异常,c#,.net,wcf,serialization,datacontractserializer,C#,.net,Wcf,Serialization,Datacontractserializer,我有一个基类Fallible和几个派生类Success、Failure和baddea,它们将在WCF服务调用的返回值中使用 在我看来,为了让它正常工作,我需要用ServiceKnownType属性修饰WCF服务方法,如下所示 [OperationContract] [ServiceKnownType(typeof(Fallible<Patient>)] [ServiceKnownType(typeof(Success<Patient>)] [ServiceKnownTyp

我有一个基类
Fallible
和几个派生类
Success
Failure
baddea
,它们将在WCF服务调用的返回值中使用

在我看来,为了让它正常工作,我需要用
ServiceKnownType
属性修饰WCF服务方法,如下所示

[OperationContract]
[ServiceKnownType(typeof(Fallible<Patient>)]
[ServiceKnownType(typeof(Success<Patient>)]
[ServiceKnownType(typeof(BadIdea<Patient>)]
[ServiceKnownType(typeof(Failure<Patient>)]
public Fallible<Patient> GetPatient(int id) {
  return new Success<Patient>(new Patient {ID = 1,FirstName = "Jim",Surname = "Spriggs"});
}
[运营合同]
[ServiceKnownType(类型)(可出错)]
[ServiceKnownType(类型)(成功)]
[ServiceKnownType(类型)(Baddea)]
[ServiceKnownType(故障类型)]
公共易出错的GetPatient(内部id){
返回新的成功(新患者{ID=1,FirstName=“Jim”,姓氏=“Spriggs”});
}
这很好。但是,我现在想要一个WCF服务方法,它返回一个集合

public List<Patient> GetDischargedPatients()
公共列表GetDischargedPatients()
按照我以前做的,我尝试装饰这个,但无论我尝试什么组合,我都会有例外。下面是我尝试的全部组合

[OperationContract]
[ServiceKnownType(typeof(Fallible<PatientOverview>))]
[ServiceKnownType(typeof(Success<PatientOverview>))]
[ServiceKnownType(typeof(BadIdea<PatientOverview>))]
[ServiceKnownType(typeof(Failure<PatientOverview>))]
[ServiceKnownType(typeof(Fallible<PatientOverview[]>))]
[ServiceKnownType(typeof(Success<PatientOverview[]>))]
[ServiceKnownType(typeof(BadIdea<PatientOverview[]>))]
[ServiceKnownType(typeof(Failure<PatientOverview[]>))]
[ServiceKnownType(typeof(List<Fallible<PatientOverview>>))]
[ServiceKnownType(typeof(List<Success<PatientOverview>>))]
[ServiceKnownType(typeof(List<BadIdea<PatientOverview>>))]
[ServiceKnownType(typeof(List<Failure<PatientOverview>>))]
public Fallible<List<PatientOverview>> GetDischargedPatients() {
  return new Success<List<PatientOverview>>();
}
[运营合同]
[ServiceKnownType(typeof(Fallible))]
[ServiceKnownType(类型(成功))]
[ServiceKnownType(typeof(baddea))]
[ServiceKnownType(类型(失败))]
[ServiceKnownType(typeof(Fallible))]
[ServiceKnownType(类型(成功))]
[ServiceKnownType(typeof(baddea))]
[ServiceKnownType(类型(失败))]
[ServiceKnownType(类型(列表))]
[ServiceKnownType(类型(列表))]
[ServiceKnownType(类型(列表))]
[ServiceKnownType(类型(列表))]
公众容易犯错的出院病人(){
返回新的成功();
}
正如您所看到的,我已经在那里抛出了所有东西(除了实际有效的东西!),但我仍然得到了在发现
ServiceKnownType
属性之前得到的原始异常

“接收对的HTTP响应时出错。 这可能是由于服务端点绑定未使用HTTP协议所致 由于HTTP请求上下文被服务器中止(可能是由于服务关闭) 已关闭)。有关详细信息,请参阅服务器日志。“

内部异常:

“基础连接已关闭:接收时发生意外错误。”

内部异常:

“无法从传输连接读取数据:现有连接被 远程主机。”

内部异常:

“远程主机已强制关闭现有连接”

WCF并没有告诉我这里出了什么问题。我尝试使用
ServiceKnownType
和各种返回类型组合,包括
Fallible
Fallible
,但没有任何帮助

有人知道我需要做些什么来归还一个收藏品吗

[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(CommandServiceHelper))]
public interface IPatientService
{
     //Your interface methdos
}
这是每次WCF需要知道类型时将调用的帮助器类

public static class CommandServiceHelper
{
    public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
    {
        //Return a list of type that your service will need to know 
    }
}
公共静态类CommandServiceHelper
{
公共静态IEnumerable GetKnownTypes(ICustomAttributeProvider提供程序)
{
//返回服务需要知道的类型列表
}
}

如果你需要更多的细节和解释,你可以通过Mebyon Kernow查看。

所以我尝试用你的代码的精简版本复制你的问题,结果是这样的

[ServiceContract]
public interface IService1
{
     //Get a patient's data
    [OperationContract]
    [ServiceKnownType(typeof(Fallible<Patient>))]
    [ServiceKnownType(typeof(Success<Patient>))]
    Fallible<Patient> GetPatient(int id);

     //Get a list of Patients
    [OperationContract]
    List<Patient> GetPatients();

    //Get a list of patients
    [OperationContract]
    [ServiceKnownType(typeof(Fallible<List<Patient>>))]
    [ServiceKnownType(typeof(Success<List<Patient>>))]
    Fallible<List<Patient>> GetSpecificPatients(string type);
}

您返回了多少名患者?可能是大小问题!您如何处理您的病例?@Esperadoce我尝试了一个空列表,一名患者,等等。基于上次我这样做时发生的情况(参见链接问题),问题不是数据,而是serialiser无法序列化数据。这是一个了解类型的问题,这就是为什么我添加了
ServiceKnownType
属性。你能试着在接口中设置ServiceKnownType吗?我想你在多态序列化方面有问题,我想我有一个链接给你,这将显示你的路径做这件事,你应该做什么avoid@Esperadoce不确定您在界面中使用
ServiceKnownType
是什么意思。您使用该属性标记服务调用本身,正如我在工作示例中所做的那样。这里唯一的区别是我希望返回一个集合,而不是单个项。我希望有兴趣查看该链接。感谢感谢感谢,但我已经在使用该方法。我只使用
ServiceKnownType
发布了该版本,以使发布的代码更易于阅读。我的问题是知道是哪种类型要包括在列表中。只是为了澄清,我显示的代码最初可以用于返回单个项目,我很难确定在返回集合时要包含哪些类型。好吧,我很困惑!我尝试了
易出错的
等(不是因为我在编辑后的答案中输入了列表`)也有同样的问题。但是,我刚刚复制了你的代码,它工作了。不确定区别是什么,但它现在工作了。非常感谢。哈哈哈-很高兴你让它工作了。如果你添加
ServiceKnownType[Fallible],你可以破坏我的代码
反序列化不喜欢同时拥有这两个属性。唯一的问题是,我最初没有同时拥有这两个属性。我最初使用了
列表
,但在
ArrayOfPatient
上出现了一个错误,这就是为什么我切换到
Patient[]
。我在问题中添加的代码是最后一次尝试通过投入所有东西来让它正常工作!仍然不知道我做错了什么,但我不会太担心。再次感谢您的帮助
public class Service : IService1
{
    public Fallible<Patient> GetPatient(int id)
    {
        return new Success<Patient>() { Value = new Patient() { Name = "Scott Robinson" } };
    }

    public List<Patient> GetPatients()
    {
        List<Patient> patients = new List<Patient>();
        patients.Add(new Patient() { Name = "Scott Robinson" });
        patients.Add(new Patient() { Name = "Darryl Robinson" });
        return patients;
    }

    public Fallible<List<Patient>> GetSpecificPatients(string type)
    {
        switch (type)
        {
            case "Fallible":
                return new Fallible<List<Patient>>() { Value = new List<Patient>() { new Patient() { Name = "Scott" }, new Patient() { Name = "Darryl" } } };              
            default:
                return new Success<List<Patient>>() { Value = new List<Patient>() { new Patient() { Name = "Scott" }, new Patient() { Name = "Darryl" } } };
        }
    }
}
ServiceKnownType[Fallible<List<PatientOverview>>]
ServiceKnownType[Success<List<PatientOverview>>]
...
public Fallible<List<PatientOverview>> GetDischargedPatients() {
  return new Success<List<PatientOverview>>();
}