WCF 方法返回集合时出现服务已知类型的异常



我有一个基类Fallible<T>和几个派生类Success<T>Failure<T>BadIdea<T>,它们将在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"});
}

这工作正常。但是,我现在想要一个返回集合的 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>>();
}
[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 
}
}

如果您需要更多详细信息和解释,可以查看Mebyon Kernow的这篇博客文章。

最新更新