Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF客户端异常:尝试反序列化参数时出错_C#_Wcf - Fatal编程技术网

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

C# WCF客户端异常:尝试反序列化参数时出错,c#,wcf,C#,Wcf,我尝试调用WCF服务时遇到此错误: 格式化程序在尝试反序列化 消息:尝试反序列化参数时出错 . InnerException消息为“错误” 在第1行1741位置。元素 'htp://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' 包含映射到名称的类型中的数据 'htp://schemas.datacontract.org/2004/07/DataAccess:Person'. 反序列化程序不知道映射到此名称的任何类型。

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

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

我有一个接口项目,定义如下:

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; }
}
公共接口IPerson
{
字符串名称{get;set;}
}
公共接口IPersonExtended:IPerson
{
//如果我删除IPerson属性的列表,它可以正常工作
列出联系人{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; }
    }
}
公共类人员:IPerson
{
公共字符串名称{get;set;}
}
公共类人员扩展:IPersonExtended
{
公共字符串名称{get;set;}
私有列表mContacts=新列表();
//如果我删除IPerson属性的列表,它可以正常工作
公开名单联系人
{
获取{return mContacts;}
设置{mContacts=value;}
}
}
我的服务合同如下所示:

[ServiceContract]
[ServiceKnownType(typeof(Person))]
[ServiceKnownType(typeof(PersonExtended))]
public interface IMyService
{
    [OperationContract]
    ServiceCallResult<GetPeopleResponse> GetPeople(GetPeopleRequest request);
}
[服务合同]
[ServiceKnownType(类型(人员))]
[ServiceKnownType(类型(人员扩展))]
公共接口IMyService
{
[经营合同]
ServiceCallResult GetPeople(GetPeopleRequest请求);
}
我的服务看起来像:

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; }
}
公共类MyService:IMyService
{
公共服务调用结果GetPeople(GetPeopleRequest请求)
{
GetPeopleResponse=新建GetPeopleResponse();
//找一些有联系人的人
response.People=getpeoplefromwhere();
ServiceCallResult结果=
新ServiceCallResponse{ResultValue=response};
返回结果;
}
}
我的响应对象看起来像:

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; }
}
[DataContract]
[知识类型(类型(人员扩展))]
[知识类型(类型(人))]
[知识类型(列表))]

[KnownType(列表您需要
[DataContract]
[DataMember]
个人
类上

[DataContract]
public class Person : IPerson
{
    [DataMember]
    public string Name { get; set; }
}
KnownTypeAttribute
应该允许您为给定的
DataContract
指定可接受的派生类。它指定了在对给定类型进行序列化或反序列化时,
DataContractSerializer
应该识别的类型

GetPeopleResponse
不是从
Person
PersonExtended
派生的

你的代码中有很多东西对我来说毫无意义。。。 这对我来说是有意义的

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
    }
}
公共接口IPerson{
字符串名称{get;set;}
}
公共接口IPersonExtended:IPerson{
列出联系人{get;set;}
}
[数据合同]
公共类人士:IPerson{
[数据成员]
公共字符串名称{get;set;}
}
[数据合同]
公共类PersonExtended:Person,IPersonExtended{
[数据成员]
公共列表联系人{get;set;}
公共人员扩展(){
联系人=新列表();
}
}
[服务合同]
公共接口IMyService{
[经营合同]
IList GetAllPeople();
}
公共类MyService:IMyService
{
私人伊利斯特人;
公共MyService(){
_人=新IList();
}
公共IList GetAllPeople(){
归人
}
}

我认为这还不够。我已经更新了我的问题。这代表了我的专有对象的简化版本。
ServiceCallResult
对象只是一个包含状态等信息的包装器。您的服务类末尾有一个return语句,它告诉我您不知道自己在做什么Service类也没有实现接口声明的函数。事实上,这并不是因为我不知道我在做什么。这只是一个简单的剪切/粘贴问题。我显然只是遗漏了函数def的行,函数def需要在那里满足接口。当它不用于任何用途时,为什么要将请求作为参数发送?你可以我们只需在界面中定义这样的函数即可满足您的需求
ServiceCallResult GetPeople();
,看在上帝的份上,尝试添加
[DataContract]
[DataMember]
People
PeopleExtended
类上的属性。同样,这是一个简化版本-请求参数通常包含筛选信息等。我确实将
DataContract
DataMember
属性添加到DataAccess类中,但没有任何效果。如上面的编辑所示,如果我从扩展对象中移动
联系人
属性,该属性是
列表
,即可正常工作。