首页 文章

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

提问于
浏览
3

我尝试调用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; }
    }
}

我的服务 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 对象中 .

EDIT 如果我通过整个工作流程删除了联系人(列表)属性,它可以正常工作 . 我不知道如何在没有添加循环引用的情况下使用我的项目结构来解决这个问题 .

1 回答

  • 2

    你需要在 Person 课上 [DataContract][DataMember]

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

    KnownTypeAttribute 应该允许您为给定的 DataContract 指定可接受的派生类 . 它指定当 SerializingDeserializing 给定类型时应由 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
        }
    }
    

相关问题