繁体   English   中英

WCF客户端异常:尝试反序列化参数时出错

[英]WCF Client Exception: error while trying to deserialize parameter

我尝试调用WCF服务时收到此错误:

格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:ResultValue时出错。 InnerException消息是'第1行位置错误1741.元素'htp://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType'包含来自映射到名称'htp:// schemas的类型的数据.datacontract.org / 2004/07 /数据访问:人”。 反序列化器不知道映射到此名称的任何类型。 考虑使用DataContractResolver或将与“Person”对应的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中。

我有一个Interfaces项目,其中包含以下定义:

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

我有一个实现接口的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; }
    }
}

我的服务合同如下:

[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对象中,该对象包含状态信息等。

编辑如果我通过整个工作流删除联系人(列表)属性,它可以正常工作。 我想知道它是否与尝试使用带有列表的属性而不是具体对象有关,但是我不知道如何使用我的项目结构来解决这个问题而不添加循环引用。

你需要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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM