在 WCF 中传递复杂类型作为结果有什么问题?

发布于 2024-12-29 19:38:52 字数 1713 浏览 0 评论 0原文

我有以下 WCF 方法:

[OperationContract]
array<Object^>^ GetResult(UInt64 taskId);

[OperationContract]
array<UrlInfo^>^ GetResultAsUriInfo(UInt64 taskId);

当我通过 GetResult 返回字符串数组时,它工作正常。另外,当我通过 GetResultAsUriInfo 返回 UrlInfo 数组时,它可以正常工作。但是,当我尝试通过 GetResult 返回 UrlInfo 的 appay 时,我收到以下客户端异常:

 The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be  
 used for communication because it is in the Faulted state.

内部异常为 null。

以下是 UrlInfo 的定义:

[Serializable]
[DataContract]
public class UrlInfo
{
    Uri uri;

    [DataMember]
    public Uri Uri
    {
        get { return uri; }
        set { uri = value; }
    }
    string title;

    [DataMember]
    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    string description;
    [DataMember]
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    List<string> tags = new List<string>();
    [DataMember]
    public List<string> Tags
    {
        get { return tags; }
        set { tags = value; }
    }

    Dictionary<string, string> allMetadata = new Dictionary<string, string>();
    [DataMember]
    public Dictionary<string, string> AllMetadata
    {
        get { return allMetadata; }
        set { allMetadata = value; }
    }


    string[] categoryPreferences = new string[0];

    [DataMember]
    public string[] CategoryPreferences
    {
        get { return categoryPreferences; }
        set { categoryPreferences = value; }
    }

为什么我不能将 UrlInfo 数组作为对象数组返回?

I've got following WCF methods:

[OperationContract]
array<Object^>^ GetResult(UInt64 taskId);

[OperationContract]
array<UrlInfo^>^ GetResultAsUriInfo(UInt64 taskId);

when I do return array of strings through GetResult, it works fine. Also, when I return array of UrlInfo through GetResultAsUriInfo it works without any problems. However, when I'm trying to return appay of UrlInfo through GetResult, I'm getting following client side exception:

 The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be  
 used for communication because it is in the Faulted state.

the inner exception is null.

Here's the definition for UrlInfo:

[Serializable]
[DataContract]
public class UrlInfo
{
    Uri uri;

    [DataMember]
    public Uri Uri
    {
        get { return uri; }
        set { uri = value; }
    }
    string title;

    [DataMember]
    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    string description;
    [DataMember]
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    List<string> tags = new List<string>();
    [DataMember]
    public List<string> Tags
    {
        get { return tags; }
        set { tags = value; }
    }

    Dictionary<string, string> allMetadata = new Dictionary<string, string>();
    [DataMember]
    public Dictionary<string, string> AllMetadata
    {
        get { return allMetadata; }
        set { allMetadata = value; }
    }


    string[] categoryPreferences = new string[0];

    [DataMember]
    public string[] CategoryPreferences
    {
        get { return categoryPreferences; }
        set { categoryPreferences = value; }
    }

Why can't I return an array of UrlInfo as array of objects?

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

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

发布评论

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

评论(1

你的他你的她 2025-01-05 19:38:52

wcf 只能传递已知类型而不是泛型类型 - 但我不熟悉 C++ 语法。

但如果需要,您可以为您的类型编写自己的序列化器。

编辑:我真的应该更仔细地阅读这个问题。问题是

 array<Object^>^ GetResult(UInt64 taskId);

对象类型中的对象类型无法在 WCF 中序列化。你应该使用你期望的类型。

wcf can just pass knowntypes and not generic types - but i m not familiar with c++ syntax.

but you can write your own serializer for your types if you need.

EDIT: i really should read the question more carefully. the problem is your object type in

 array<Object^>^ GetResult(UInt64 taskId);

object type can't be serialized in WCF. you should ues the type you expect there.

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