【问题标题】:How can I control the name of generic WCF return types?如何控制通用 WCF 返回类型的名称?
【发布时间】:2008-10-05 16:50:58
【问题描述】:

我有一个 WCF Web 服务方法,其原型是:

[OperationContract]
Response<List<Customer>> GetCustomers();

当我将服务引用添加到客户端时,Visual Studio (2005) 创建一个名为“ResponseOfArrayOfCustomerrleXg3IC”的类型,它是“Response>”的包装器。有什么办法可以控制包装名称? ResponseOfArrayOfCustomerrleXg3IC 听起来不是很吸引人……

【问题讨论】:

    标签: wcf web-services generics


    【解决方案1】:

    您可以像这样在DataContract 属性中定义自己的名称:

    [DataContract(Name = "ResponseOf{0}")]
    public class Response<T>
    

    请注意,在您的示例中,{0} 将被替换,您的代理引用类型将为 ResponseOfArrayOfCustomer。

    更多信息在这里:WCF: Serialization and Generics

    【讨论】:

      【解决方案2】:

      请试试这个:

      [OperationContract]
      [return: MessageParameter(Name="YOURNAME")]
      Response<List<Customer>> GetCustomers();
      

      【讨论】:

        【解决方案3】:

        是的。 OperationContractAttribute 采用一个名为 Name 的参数。你可以这样指定:

        [OperationContract(Name = "NameGoesHere")]
        Response<List<Customer>> GetCustomers();
        

        【讨论】:

        • 我不想指定操作的名称,我想指定DataContract的名称。问题是我使用的 DataContract 是通用的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-04
        • 2010-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多