WCF WebGet 和 ICollection<>

发布于 2024-07-27 21:46:00 字数 595 浏览 5 评论 0 原文

我正在尝试从 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 将类型添加到操作的已知类型集合中。

I'm attempting to return a generic ICollection from a REST WCF service. Should the following be possible?

[ServiceContract]
public class WebConfigurationManager {

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

}

When I attempt to execute this operation from my web browser, I get an error. Looking through my WCF trace shows me this:

Cannot serialize parameter of type 'System.String[]' (for operation 'GetStrings', contract 'WebConfigurationManager') because it is not the exact type 'System.Collections.Generic.ICollection`1[System.String]' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-08-03 21:46:00

这应该有效:

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

This should work:

[ServiceKnownType(typeof(string[]))]
[ServiceContract]
public class WebConfigurationManager {
    [WebGet]
    [OperationContract]
    public ICollection<string> GetStrings() {
        return new string[] { "A", "B", "C" };
    }
}
清晰传感 2024-08-03 21:46:00

安德鲁为我指明了正确的方向。 答案是

[ServiceKnownType(typeof(string[]))]

在[ServiceContract]属性上方添加。

Andrew pointed me in the right direction. The answer is to add

[ServiceKnownType(typeof(string[]))]

above the [ServiceContract] attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文