2009-07-16 1 views
2

REST WCF 서비스에서 일반 ICollection을 반환하려고합니다. 다음이 가능해야합니까?WCF WebGet 및 ICollection <>

[ServiceContract] 
public class WebConfigurationManager { 

    [WebGet] 
    [OperationContract] 
    public ICollection<string> GetStrings() { 
     return new string[] { "A", "B", "C" }; 
    } 

} 

웹 브라우저에서이 작업을 실행하려고하면 오류가 발생합니다.

이 정확한 유형이 아니기 때문에 유형의 매개 변수 '[] 선택 System.String'(작업 'GetStrings'에 대한 계약 'WebConfigurationManager')를 직렬화 할 수 없습니다 '은 System.Collections : 내 WCF 추적을 통해 찾고 나에게 이것을 보여줍니다 .Generic.ICollection`1 [System.String] '이 (가) 메서드 서명에 있고 알려진 형식 컬렉션에 없습니다. 매개 변수를 serialize하려면 ServiceKnownTypeAttribute를 사용하여 작업의 알려진 형식 컬렉션에 형식을 추가합니다.

답변

2

이 작동합니다 :

[ServiceKnownType(typeof(string[]))] 
[ServiceContract] 
public class WebConfigurationManager { 
    [WebGet] 
    [OperationContract] 
    public ICollection<string> GetStrings() { 
     return new string[] { "A", "B", "C" }; 
    } 
} 
0

앤드류는 올바른 방향으로 절 지적했다. 해답은 [의 ServiceContract] 특성 위에

[ServiceKnownType(typeof(string[]))] 

를 추가하는 것이다.