在WCF中传递复杂类型有什么问题?

时间:2012-01-29 23:14:54

标签: c# wcf c++-cli managed-c++

我有以下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.

内部异常为空。

以下是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数组作为对象数组返回?

1 个答案:

答案 0 :(得分:2)

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

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

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

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

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