2009-07-16 77 views
2

我试图从REST WCF服务返回一个通用的ICollection。以下是可能的吗?WCF WebGet和ICollection <>

[ServiceContract] 
public class WebConfigurationManager { 

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

} 

当我尝试从我的Web浏览器执行此操作时,出现错误。翻翻我的WCF跟踪显示我:

无法序列类型的参数“System.String []”(操作“GetStrings”,合同“WebConfigurationManager”),因为它不是确切类型“System.Collections中.Generic.ICollection`1 [System.String]'在方法签名中,不在已知类型集合中。为了序列化参数,请将类型添加到使用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[]))] 

相关问题