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

在我previously discovered时,为了使其正常工作,我需要使用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"});
}


这很好。但是,我现在想要返回集合的WCF服务方法。

public List<Patient> 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属性之前,我仍然得到了原始的异常...


  “收到对http://localhost:5448/PatientsService.svc的HTTP响应时发生错误。
  这可能是由于服务端点绑定未使用HTTP协议。这也可能是
  由于服务器终止了HTTP请求上下文(可能是由于服务关闭)
  下)。请参阅服务器日志以获取更多详细信息。”


内部例外:


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


内部例外:


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


内部例外:


  “现有连接被远程主机强行关闭”


WCF确实没有给我任何有关这里出了什么问题的信息。我尝试将ServiceKnownType与返回类型的各种组合一起使用,包括Fallible<Patient[]>Fallible<List<Patient>>,但这没有帮助。

任何人有什么想法我需要做些什么来退还收藏吗?

最佳答案

因此,我尝试使用精简后的代码版本来复制您的问题,并最终得出结论

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


以及服务的实施:

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


但是我没有得到错误。

查看您的代码,我可以看到您的GetDiscardedPatients返回Fallible<List<PatientOverview>>,但是'ServiceKnownTypes'都不属于这种类型。你有没有尝试过:

ServiceKnownType[Fallible<List<PatientOverview>>]
ServiceKnownType[Success<List<PatientOverview>>]
...
public Fallible<List<PatientOverview>> GetDischargedPatients() {
  return new Success<List<PatientOverview>>();
}

08-06 04:16