gpt4 book ai didi

c# - WCF 方法返回集合时出现 ServiceKnownType 异常

转载 作者:行者123 更新时间:2023-11-30 23:08:57 25 4
gpt4 key购买 nike

我有一个基类 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 之前得到的原始异常。属性...

"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返回类型的各种组合,包括 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>>();
}

关于c# - WCF 方法返回集合时出现 ServiceKnownType 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com