How can I expose a 3rd party's enum through my asmx?
Given:
- An asmx web service.
- A 3rd party dll that contains a useful enum.
Question:
开发者_开发百科How can I expose this enum through my web service without having to repeat myself and re-type the enum's members in my webservice's public class?
You can create a method which either takes or returns a value with their enum in it. The namespace on the enum will change to your service namespace when they generate the proxy class, but you'll have the values they input on your end. This shouldn't be a problem though as long as they aren't trying to use the same enum in your service and the third party.
This question is asking about how to expose a programming language enumeration over a SOAP web service. This cannot be done. Here's why not:
By "enum", the OP means a programming language construct that produces a type consisting of named integer values. For instance:
public enum MyEnum
{
Value1 = 10,
Value2 = 20,
Value3 = 30
}
This produces an association between the names, such as Value
, and the corresponding values, such as 10
. There is nothing like this in SOAP web services.
SOAP web services describe themselves to clients via WSDL (Web Services Description Language). WSDL uses XML Schema (XSD) to describe the shape of the data to be interchanged between the clients and the service.
XML schema has a concept of "enumeration", which describes that a certain element or attribute can have one of several values. Those values are listed (enumerated) in the XML Schema. For instance,
<xs:simpleType name="MyEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="Value1"/>
<xs:enumeration value="Value2"/>
<xs:enumeration value="Value3"/>
</xs:restriction>
</xs:simpleType>
This defines a type which can take on one of three string values: "Value1", "Value2" or "Value3". These values are not associated in any way with integer values. Just to hammer it home, consider the following:
<xs:simpleType name="MyDateEnumeration">
<xs:restriction base="xs:date">
<xs:enumeration value="2013-06-01"/>
<xs:enumeration value="2013-06-05"/>
<xs:enumeration value="2014-06-01"/>
</xs:restriction>
</xs:simpleType>
This defines a type which can take on one of three date
values: June 1, 2013, June 5, 2013 or June 1, 2014. Note that there is neither an association with any integer values, nor even any association with any string name.
The bottom line is that XML Schema cannot describe the programming language concept of an enum
. As a result, WSDL cannot use the programming language concept of an enum
, which means that SOAP web services cannot expose an enum
.
The best that can be done is to expose the names, or the values, but not both, as in MyEnum
above. When a client consumes that service, it may translate MyEnum
into an enum
in a programming language. However, since there is no integer value associated with any of the names, each client could reasonably be expected to use a different integer value. One client might use 1
for Value1
, while another might use 0
.
精彩评论