如何将Enums集合发送到WCF服务

时间:2009-01-13 16:14:41

标签: c# wcf collections enums

我无法将一组Enums发送到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正常工作。尝试将Enums列表发送到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?

谢谢!

2 个答案:

答案 0 :(得分:3)

您的配置文件是什么样的?听起来您可能正在使用不支持webHttpBinding的{​​{1}}元素,因为无法在网址上表示。

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

答案 1 :(得分:0)

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

相关问题