-->

WCF WebGet和ICollection的<>(WCF WebGet and ICo

2019-09-19 06:04发布

我试图从REST WCF服务返回泛型ICollection的。 如果以下是可能的?

[ServiceContract]
public class WebConfigurationManager {

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

}

当我试图从我的浏览器上执行此操作,我得到一个错误。 翻翻我的WCF跟踪显示我:

无法序列类型的参数“System.String []”(操作“GetStrings”,合同“WebConfigurationManager”),因为它不是在方法中的确切类型“System.Collections.Generic.ICollection`1 [System.String]”签名,是不是在已知类型的集合。 为了序列的参数,添加的类型的已知类型的集合使用ServiceKnownTypeAttribute操作。

Answer 1:

这应该工作:

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


Answer 2:

安德鲁指出我在正确的方向。 答案是增加

[ServiceKnownType(typeof(string[]))]

上面的[的ServiceContract]属性。



文章来源: WCF WebGet and ICollection<>
标签: wcf webget