当 WCF 方法 returns a collection 时出现 ServiceKnownType 异常
Exception with ServiceKnownType when the WCF method returns a collection
我有一个基础 class Fallible<T>
和几个派生的 classes Success<T>
, Failure<T>
和 BadIdea<T>
将被使用在 WCF 服务调用的 return 值中。
因为我 ,为了让它工作,我需要用 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"});
}
这很好用。但是,我现在想要一个 return 是 collection...
的 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
属性之前遇到的原始异常...
"An error occurred while receiving the HTTP response to http://localhost:5448/PatientsService.svc.
This could be due to the service endpoint binding not using the HTTP protocol. This could also be
due to an HTTP request context being aborted by the server (possibly due to the service shutting
down). See server logs for more details."
内部异常:
"The underlying connection was closed: An unexpected error occurred on a receive."
内部异常:
"Unable to read data from the transport connection: An existing connection was forcibly closed by
the remote host."
内部异常:
"An existing connection was forcibly closed by the remote host"
WCF 确实没有给我任何关于这里出了什么问题的信息。我尝试将 ServiceKnownType
与 return 类型的各种组合一起使用,包括 Fallible<Patient[]>
和 Fallible<List<Patient>>
但它没有帮助。
有人知道我需要做什么才能 return collection 吗?
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(CommandServiceHelper))]
public interface IPatientService
{
//Your interface methdos
}
这是每次 WCF 需要知道类型时都会调用的助手 class。
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 的 this blog post。
所以我尝试用你的代码的精简版本来复制你的问题,结果是这个
[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
returns Fallible<List<PatientOverview>>
但“ServiceKnownTypes”的 none 属于这种类型。你试过了吗:
ServiceKnownType[Fallible<List<PatientOverview>>]
ServiceKnownType[Success<List<PatientOverview>>]
...
public Fallible<List<PatientOverview>> GetDischargedPatients() {
return new Success<List<PatientOverview>>();
}
我有一个基础 class Fallible<T>
和几个派生的 classes Success<T>
, Failure<T>
和 BadIdea<T>
将被使用在 WCF 服务调用的 return 值中。
因为我 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"});
}
这很好用。但是,我现在想要一个 return 是 collection...
的 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
属性之前遇到的原始异常...
"An error occurred while receiving the HTTP response to http://localhost:5448/PatientsService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."
内部异常:
"The underlying connection was closed: An unexpected error occurred on a receive."
内部异常:
"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
内部异常:
"An existing connection was forcibly closed by the remote host"
WCF 确实没有给我任何关于这里出了什么问题的信息。我尝试将 ServiceKnownType
与 return 类型的各种组合一起使用,包括 Fallible<Patient[]>
和 Fallible<List<Patient>>
但它没有帮助。
有人知道我需要做什么才能 return collection 吗?
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(CommandServiceHelper))]
public interface IPatientService
{
//Your interface methdos
}
这是每次 WCF 需要知道类型时都会调用的助手 class。
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 的 this blog post。
所以我尝试用你的代码的精简版本来复制你的问题,结果是这个
[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
returns Fallible<List<PatientOverview>>
但“ServiceKnownTypes”的 none 属于这种类型。你试过了吗:
ServiceKnownType[Fallible<List<PatientOverview>>]
ServiceKnownType[Success<List<PatientOverview>>]
...
public Fallible<List<PatientOverview>> GetDischargedPatients() {
return new Success<List<PatientOverview>>();
}