[英]WCF service Exception:The formatter threw an exception while trying to deserialize the message
[英]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
不派生自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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.