【问题标题】:How do send a Collection of Enums to a WCF Service如何将枚举集合发送到 WCF 服务
【发布时间】:2009-01-13 16:14:40
【问题描述】:

我在将枚举集合发送到 WCF 服务方法时遇到问题。我以这篇文章为指导:Sharing Enum with WCF Service

[ServiceContract]
[ServiceKnownType(typeof(MyEnum))]
[ServiceKnownType(typeof(List<MyEnum>))]
public interface IMyService{    
  [OperationContract]    
  MyEnum ServiceMethod1( );

  [OperationContract]    
  IList<MyEnum> ServiceMethod2( );

  [OperationContract]    
  IList<MyEnum> ServiceMethod3( MyEnum e );

  [OperationContract]    
  IList<MyEnum> ServiceMethod4( IList<MyEnum> e );
}

[Serializable]
[DataContract]
public enum MyEnum{ 
  [EnumMember] red, 
  [EnumMember] green, 
  [EnumMember] blue 
};

ServiceMethod1、ServiceMethod2 和 ServiceMethod3 工作正常。尝试将枚举列表发送到 ServiceMethod4 时出现以下错误。

Operation 'ServiceMethod4' in contract 'IMyService' has a query variable named 'e' of type 'System.Collections.Generic.IList`1[MyEnum]', but type 'System.Collections.Generic.IList`1[MyEnum]' is not convertible by 'QueryStringConverter'.  Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

我需要创建自定义 QueryStringConverter 吗?

谢谢!

【问题讨论】:

    标签: c# wcf collections enums


    【解决方案1】:

    您的配置文件是什么样的?听起来您可能正在使用不支持 IList&lt;MyEnum&gt;webHttpBinding 元素,因为它无法在 URL 上表示。

    您应该考虑使用 basicHttpBinding,因为它使用 SOAP。使用 SOAP 将允许您序列化 IList&lt;MyEnum&gt; 并将其发送到您的 OperationContract

    【讨论】:

    • 我正在使用 webHttpBinding 元素并使用简单的 Get 请求调用我的服务,并传递查询字符串上的参数。 server/MyService.svc/ServiceMethod4?e=0&e=1&e=2 为什么webHttpBinding 不序列化枚举列表? webHttpBinding 将序列化一个对象列表。
    【解决方案2】:

    是否可以将该参数键入为 MyEnum 的数组?然后在您的实现中使用 var eList = new List(e);或者,您可以尝试使用 KnownType 助手类,如下所述: http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceknowntypeattribute.aspx

    【讨论】:

    • 我之前尝试将参数的类型更改为 MyEnums 数组,但参数仍然没有序列化。我也在使用上面示例中所示的 ServiceKnownType 属性,但它也无济于事。
    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2014-11-30
    相关资源
    最近更新 更多