2012-09-18 6 views
0

나는, 부울, 오브젝트가 int와 같은 기본 유형을 포함 할 수 dictionary <string, object>의 배열을 반환하는 등 노력하고 있어요 아니면 내가 그것을 얻을 수 있지만 그것은 dictionary<string, object>.net4 WCF 사전에 포함 된 사전

의 또 다른 배열을 포함 할 수 사전에 사전이 존재한다면 비 직렬화하지 않을 것입니다.

Error in line 1 position 543. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data from a type that maps to the name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfArrayOfKeyValueOfstringanyType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'ArrayOfArrayOfKeyValueOfstringanyType' 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. 

클래스 :

[DataContract(Namespace = "CISICPD")] 
[KnownType(typeof(Dictionary<string,object>))] 
public class TestResponse 
{ 
    [DataMember] 
    public Dictionary<string,object>[] Results; 
} 

기능 :

public TestResponse test(string test1, string test2) 
    { 
     TestResponse r = new TestResponse(); 
     r.Results = new Dictionary<string, object>[1]; 
     r.Results[0] = new Dictionary<string, object>(); 
     r.Results[0].Add("field1", 26); 
     Dictionary<string, object>[] d = new Dictionary<string, object>[1]; 
     d[0] = new Dictionary<string, object>(); 
     d[0].Add("inner", 28); 
     r.Results[0].Add("dictionary", d); 
     return r; 
    } 

실행이 오류 메시지를 제공하지만 난 살아야 올바른 knowntype 거 같아요

는 나는 다음과 같은 오류가 발생합니다 ?

CISICPD.CPDClient t = new CISICPD.CPDClient(); 
CISICPD.TestResponse response = t.test("dgdf", "dfsdfd"); 

답변

0

TestResponse에 알려진 유형이 추가 : "D"는 시험 방법에서 사전 객체의 배열이

[KnownType(typeof(Dictionary<string, object>[]))] 

때문에 당신은 저장하고 그 결과 값으로는 유형을 알려진 유형에 추가해야합니다.

체크 아웃 자세한 내용은이 링크의 "컬렉션 및 알려진 유형"섹션 : http://msdn.microsoft.com/en-us/library/aa347850.aspx

기본적으로 당신이 객체가 KnownType 추가가 필요합니다 같은 결과에 저장 될 겁니다 모든 유형.

+0

나는 그 변경을하고 서비스 참조를 업데이트 한 것과 똑같은 메시지를 받고 있습니다. – user1677973

+0

ok ive는 문제가 무엇인지 발견했습니다. - knowntype 속성이 무시되었습니다. 서비스 참조를 추가/업데이트하여 생성 된 reference.cs 파일은 testresponse 클래스에 대한 유형을 가지고 있지 않습니다. 코드에 추가하면 제대로 작동합니다. 그렇다면 reference.cs 파일을 생성 할 때 knowntype을 무시하는 이유는 무엇입니까? – user1677973

+0

저는 일반적으로 서비스 참조를 사용하지 않으므로 KnownType 특성이 생성되지 않는 이유가 확실하지 않지만 DataContractSerializer를 사용할 때만 발생하는 것으로 보입니다. 명령 줄에서 svcutil을 수동으로 실행하고 XmlSerializer를 지정하면 "svcutil YourServiceURL/serializer : XmlSerializer/noConfig" –

1

datacontract 클래스에 다음 속성을 추가하기 만하면됩니다.

[DataMember] 
public object UsedForKnownTypeSerializationObject; 

이제 생성 된 프록시에는 datacontract에 설정 한 Knowtypes가 포함됩니다. 나는 똑같은 문제가 있었는데 이것이 내가 생각해 낸 유일한 해결책이다.

[DataContract] 
[KnownType(typeof(List<String>))] 
public class Foo 
{ 
    [DataMember] 
    public String FooName { get; set; } 

    [DataMember] 
    public IDictionary<String, Object> Inputs { get; set; } 

    [DataMember] 
    private Object UsedForKnownTypeSerializationObject{ get; set; } 

} 

그것은 같은 꽤 당신이 결국 때문이 아니다 : 당신에 Object 유형의 속성에 DataContract 수업을하지 않을 경우, 생성 된 프록시는 선언 knowtypes 예를 들어

가 포함되어 있지 않습니다 기능적 구현이없는 더미 속성. 하지만 또 다른 해결책이 없습니다.

관련 문제