gpt4 book ai didi

c# - WCF 客户端异常 : error while trying to deserialize parameter

转载 作者:行者123 更新时间:2023-11-30 12:27:58 24 4
gpt4 key购买 nike

当我尝试调用 WCF 服务时出现此错误:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:ResultValue. The InnerException message was 'Error in line 1 position 1741. Element 'htp://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'htp://schemas.datacontract.org/2004/07/DataAccess:Person'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'Person' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.

我有一个具有以下定义的接口(interface)项目:

public interface IPerson
{
string Name { get; set; }
}

public interface IPersonExtended : IPerson
{
// If I remove the List of IPerson property, it works fine
List<IPerson> Contacts { get; set; }
}

我有一个实现接口(interface)的 DataAccess 项目:

public class Person : IPerson
{
public string Name { get; set; }
}

public class PersonExtended : IPersonExtended
{
public string Name { get; set; }
private List<IPerson> mContacts = new List<IPerson>();

// If I remove the List of IPerson property, it works fine
public List<IPerson> Contacts
{
get { return mContacts; }
set { mContacts = value; }
}
}

我的服务契约(Contract)如下所示:

[ServiceContract]
[ServiceKnownType(typeof(Person))]
[ServiceKnownType(typeof(PersonExtended))]
public interface IMyService
{
[OperationContract]
ServiceCallResult<GetPeopleResponse> GetPeople(GetPeopleRequest request);
}

我的服务看起来像:

public class MyService : IMyService
{
public ServiceCallResult<GetPeopleResponse> GetPeople(GetPeopleRequest request)
{
GetPeopleResponse response = new GetPeopleResponse();
// Get Some people that have contacts
response.People = GetPeopleFromSomewhere();

ServiceCallResult<GetPeopleResponse> result =
new ServiceCallResponse<GetPeopleResponse> { ResultValue = response };

return result;
}
}

我的响应对象看起来像:

[DataContract]
[KnownType(typeof(PersonExtended))]
[KnownType(typeof(Person))]
[KnownType(List<Person>))]
[KnownType(List<PersonExtended))]
public class GetPeopleResponse
{
[DataMember]
public List<PersonExtended> People { get; set; }
}

Response对象只是包裹在一个包含状态信息等的MessageContract对象中

编辑如果我在整个工作流程中删除 Contact (List) 属性,它就可以正常工作。我想知道这是否与尝试将带有列表的属性用于接口(interface)而不是具体对象有关,但我不确定如何在不添加循环引用的情况下使用我的项目结构来解决这个问题。

最佳答案

Person 类需要[DataContract][DataMember]

[DataContract]
public class Person : IPerson
{
[DataMember]
public string Name { get; set; }
}

KnownTypeAttribute 应该使您能够为给定的 DataContract 指定可接受的派生类。它指定在序列化反序列化给定类型时应由 DataContractSerializer 识别的类型。

GetPeopleResponse 不派生自 PersonPersonExtended...

此外,您的代码中有很多内容对我来说根本没有意义...这对我来说很有意义......

public interface IPerson {
string Name { get; set; }
}

public interface IPersonExtended : IPerson {
List<IPerson> Contacts { get; set; }
}

[DataContract]
public class Person : IPerson {
[DataMember]
public string Name { get; set; }
}

[DataContract]
public class PersonExtended : Person, IPersonExtended {
[DataMember]
public List<Person> Contacts { get; set; }

public PersonExtended() {
Contacts = new List<Person>();
}
}

[ServiceContract]
public interface IMyService {
[OperationContract]
IList<PersonExtended> GetAllPeople();
}

public class MyService : IMyService
{
private IList<PersonExtended> _people;

public MyService() {
_people = new IList<PersonExtended>();
}

public IList<PersonExtended> GetAllPeople() {
return _people
}
}

关于c# - WCF 客户端异常 : error while trying to deserialize parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23703755/

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