Web Services and C# Enums
Web Service Transparency
.NET support for web services is excellent in creating illusion of transparency. General process is quite straightforward:
- On the server we create a web service, decorating publicly visible methods with [WebMethod] attribute.
- On the client we add a web reference to the service, and proxy code is automatically generated for us by VIsual Studio. We can then call web service methods almost as if they were local methods.
All arguments and return values get magically XML-serialized, transmitted to the peer, and de-serialized to something very close to the original form. Ideally, this whole process should be automatic and seamless.
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:
- If server-side code declares an enum and assigns specific numeric values to its members, these values will not be visible to the client.
- 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.
- 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.
Numeric Values Are Not Preserved
If we have server-side definition like this:
enum StatusCode { Success = 200, NotFound = 404, Denied = 401 }
it is translated to client-side definition like this:
enum StatusCode { Success = 1, NotFound = 2 Denied = 3 }
Corresponding WSDL looks as follows:
<s:simpleType name="StatusCode"> <s:restriction base="s:string"> <s:enumeration value="Success"/> <s:enumeration value="NotFound"/> <s:enumeration value="Denied"/> </s:restriction> </s:simpleType>
As one can see, there is no mension of numeric values in the WSDL. Proxy code generator on the client side does not have access to anything but WSDL. Therefore, it does not have a chance to get numeric values of enum members.
An important side effect of this phenomenon is that the relative order of enum members may not be preserved. For instance, in the example above, expression
(StatusCode.NotFound > StatusCode.Denied)
is true on the server, and false on the client.
Relationships Between [Flags] Masks May Be Broken
Server-side declaration:
[Flags] enum UserRights { Read = 16, Write = 256, Delete = 1024, AllAccess = Read | Write | Delete }
Client-side declaration (some insignificant decorations removed):
[Flags] enum UserRights { Read = 1, Write = 2, Delete = 4, AllAccess = 8 }
Corresponding WSDL:
<s:simpleType name="UserRights"> <s:restriction base="s:string"> <s:enumeration value="Read"/> <s:enumeration value="Write"/> <s:enumeration value="Delete"/> <s:enumeration value="AllAccess"/> </s:restriction> </s:simpleType>
Therefore, on the client UserRights.AllAccess lost its relationship to other user right values. On the server expression(UserRights.AllAccess & UserRights.Read) != 0
is true, while on the client it is false.
This can lead to disastrous consequences.
Out-Of-Range Values Cause Exception on Receiving End
The following server-side code:
enum Test { SomeValue = 1 } [WebMethod] public Test GetValue() { return (Test)512; }
will cause "invalid XML document" exception on the client side when reading GetValue() response. This exception is related to how XML serializer works with enums. "Legal" values are transmitted as text. E.g. value of 1 would be transmitted as <Test>SomeValue</Test>. For [Flags] enums multiple text strings are transmitted to indicate concatenation of several masks, e.g. <UserRights>Read Write</UserRights>. However, out-of-range values are transmitted as stringified integers, e.g. <Test>512</Test>. These integers are not understood at the receiving end and cause exception.
Controlling Generated XML
It seems that .NET framework does not give you much leeway in controlling XML generated for enums. In particular, constructs like :
[XmlElement(typeof(int))] enum MyEnum { ... }
or
[XmlElement(DataType="integer")] enum MyEnum { ... }
do compile, but fail miserably at run-time.
One useful attribute is [XmlEnum], which allows to change names of enum members. As we mentioned before, enum members are transmitted as literal strings. Therefore, if one has flags enum like this:
[Flags] enum MyMask { VeryVeryLongFlagNameWillBeTransmittedToClient, AnotherQuiteLongFlagNameAlongTheFirstOne, EtCeteraEtCetera }
generated XML can get quite verbose. To prevent this, transmitted literal strings may be changed using [XmlEnum]:
[Flags] enum MyMask { [XmlEnum("VeryLong")] VeryVeryLongFlagNameWillBeTransmittedToClient, [XmlEnum("Another")] AnotherQuiteLongFlagNameAlongTheFirstOne, [XmlEnum("EtCetera")] EtCeteraEtCetera }
Conclusion
Extra caution must be excercised when transmitting enums over web service boundary. Behavior of XML serializer is not always straightforward for enums, and sometimes can cause serious incompatibilities between client and server. To ensure correct operation of software, one must keep in mind its limitations. If numeric values need to be preserved across the wire, they must be transferred as integers, not enums. In this case, however, client must have ad-hoc knowledge regarding what value means what.
.NET framework provides limited means of manipulating XML generated for enums. In particular, we were unable to find an attribute that would allow to automatically transmit enum as int.
转载地址:http://www.ikriv.com/dev/dotnet/WebServices_and_Enums.html
Web Services and C# Enums的更多相关文章
- Web Services and C# Enums -摘自网络
Web Service Transparency .NET support for web services is excellent in creating illusion of transpar ...
- .NET RESTful Web Services入门
很早之前看到过RESTful Web Services,并未在意,也没找相关资料进行学习.今天偶尔有一机会,就找了点资料进行研究,发现RESTful真是“简约而不简单”.下面用示例来说明: 1 项目结 ...
- 分分钟带你玩转 Web Services
当大型需求被数个公司分割开来,各公司系统相互交换数据的问题就会接踵而来. 毕竟是多家不同的公司的产品,研发开发语言.采用技术框架基本上是百花齐放. 怎样让自家系统提供的服务具有跨平台.跨语言.跨各种防 ...
- BizTalk发布WS-Security的web services
最近做个项目,biztalk跟OTM(Oracle Transportation Management)系统做对接,双方通过web services通讯,这部分是BizTalk发布WS-Securit ...
- BizTalk调用WS-Security的web services
最近做个项目,biztalk跟OTM(Oracle Transportation Management)系统做对接,双方通过web services通讯,这部分是BizTalk调用OTM的web se ...
- 【整理】动态加载Web Services
WebClient client = new WebClient(); String url = "http://localhost/MESAPIWebService/MESAPI.asmx ...
- RESTful Web Services初探
RESTful Web Services初探 作者:杜刚 近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTf ...
- asp.net Ajax和web services
新建一个web服务 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- SpringSide 部署showcase项目出现 JAX-RS (REST Web Services) 2.0 can not be installed错误!
maven+springmvc错误 JAX-RS (REST Web Services) 2.0 can not be installed 项目problem提示错误 JAX-RS (REST Web ...
随机推荐
- 对ajax中数据的得到以及绑定的认识
1.将后台得到的数据绑定到datagrid为例: 第一种是: 后台得到的数据直接绑定到datagrid上面,如下图: 这样操作的好处在于可以使界面比较简洁. 第二种是将得到的数据作为参数的形式绑定到d ...
- JAVA基础部分复习(三、泛型)
JAVA泛型的基本使用: /** * JAVA泛型的使用 * 定义:泛型的本质是参数化类型,就是说所操作的数据类型被指定为一个参数. * * 定义泛型方法的规则 * 1.所有泛型方法声明都有一个类型参 ...
- flask中利用from来进行对修改修改时旧密码的验证
在flask中,肯定是post提交个from进行密码验证.还有一定就是修改密码肯定是登录之后才能进行对密码的修改,这么说,在浏览器中的session中一定会有用户的信息,可以通过相对应的信息去获取到相 ...
- [Data Structure] Stack Implementation in Python
We can realize a Stack as an adaptation of a Python List. S.push(e)=L.append(e) S.pop()=L.pop() S.to ...
- 牛客G-指纹锁【一题三解】
链接:https://www.nowcoder.com/acm/contest/136/G来源:牛客网 题目描述 HA实验有一套非常严密的安全保障体系,在HA实验基地的大门,有一个指纹锁. ...
- Codeforces 978E:Bus Video System
题目链接:http://codeforces.com/problemset/problem/978/E 题意 一辆公交车,在每站会上一些人或下一些人,车的最大容量为w,问初始车上可能有的乘客的情况数. ...
- mybatis下的分页,支持所有的数据库
大家都知道,mybatis的自带分页方法只是逻辑分 页,如果数据量很大,内存一定会溢出,不知道为什么开源组织不在里面集成hibernate的物理分页处理方法!在不修改mybatis源代码的情况下, 应 ...
- 将button或者input角变为圆弧
style="border-radius:5px;" input框时,不能用type属性
- rest-framework之视图
rest-framework之视图 本文目录 一 基本视图 二 mixin类和generice类编写视图 三 使用generics 下ListCreateAPIView,RetrieveUpdateD ...
- hdu4565 So Easy! 矩阵快速幂
A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...