标志使用Web服务 [英] Flags with web services

查看:132
本文介绍了标志使用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标志属性枚举的背后是一个Web服务,如下所示:

  [序列化,标志]
公共枚举AccessLevels
{
    无= 0,
    阅读= 1,
    写= 2,
    全部=阅读|写
}
 

我的问题是我的web服务的消费者不具备枚举的原常数值。由此产生的代理类的客户端有一些达这样的:

  {
   无= 1,
   阅读= 2,
   写= 4,
   全部= 8
}
 

就这样,当消费者正在检查读访问,这将是错误的,即使testItem是全部

 ((testItem&安培; Svc.Read)== Svc.Read)
 

我怎样才能正确地提供旗Web服务?

编辑:

<据一文也未必能够做什么,我期待的事情。伊万Krivyakov状态

  

枚举的不完善透明度

     

事实证明,枚举不一样   透明的,我们希望他们可以。   有三个粘的问题:

     
      
  1. 如果服务器端code声明一个枚举,并指定具体的数字   值的成员,这些值   将不可见给客户端。
  2.   
  3. 如果服务器端code声明了[标志]枚举与复合的面具   值(如白色=红|绿|蓝)   它没有正确反映在   客户端。
  4.   
  5. 如果服务器或客户端发送一个非法的值,它的外   枚举的范围,它会导致   例外在对XML解串   另一面。
  6.   

所以我想如果这仅仅是一个限制,是不可能的。

解决方案

我对这个做了广泛的调研,发现这是不可能通过Web服务序列化枚举常量。请注意,以实现自己的目标,你不需要枚举无或完整。这两个枚举可以读/写组合隐含的:

您可以全权访问,如果你的AccessLevels =阅读|写    并没有,如果你的AccessLevels = 0没什么]

您枚举是这样的:

  [序列化,标志]
公共枚举AccessLevels
{
    阅读= 1,
    写= 2
}
 

I have a flag attribute enumeration that is behind a web service as follows:

[Serializable,Flags]
public enum AccessLevels
{
    None = 0,
    Read = 1,
    Write = 2,
    Full = Read | Write
}

My problem is the consumer of my web service does not have the original constant values of the enum. The resulting proxy class client side has something that amounts to this:

{
   None = 1,
   Read = 2,
   Write = 4,
   Full = 8
}

And thus when the consumer is checking for "Read" access this will be false even when "testItem" is "Full"

((testItem & Svc.Read) == Svc.Read)

How can I properly provide flags over a web service?

EDIT:

According to this article it may not be possible to do what I am looking to do. Ivan Krivyakov states

Imperfect Transparency of Enums

It turns out that enums are not as transparent as we'd like them to be. There are three sticky issues:

  1. If server-side code declares an enum and assigns specific numeric values to its members, these values will not be visible to the client.
  2. If server-side code declares a [Flags] enum with "compound" mask values (as in White = Red|Green|Blue), it is not properly reflected on the client side.
  3. If server or client transmits an "illegal" value which is outside of the scope of the enum, it causes an exception in XML de-serializer on the other side.

So I wonder if this is just a limitation and is not possible.

解决方案

I have done extensive research on this and found that it is not possible to serialize enumeration constants through a web service. Note that to accomplish your goal you don't need the enumerations None or Full. These two enumerations can be implied with read / write combination:

You could assume full access if your AccessLevels = Read | Write and none if your AccessLevels = 0 [nothing]

Your enumerations would look like this:

[Serializable,Flags]
public enum AccessLevels
{
    Read = 1,
    Write = 2
}

这篇关于标志使用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆