我如何通过我的 asmx 公开第 3 方的枚举?
给定:
- 一个 asmx Web 服务。
- 包含有用枚举的第 3 方 dll。
问题:
我如何通过我的 网络服务,无需重复 我自己并重新输入枚举的成员 在我的网络服务的公共类中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个方法,该方法接受或返回一个包含枚举的值。当它们生成代理类时,枚举上的命名空间将更改为您的服务命名空间,但您将拥有它们在您端输入的值。不过,只要他们不尝试在您的服务和第三方中使用相同的枚举,这就不应该成为问题。
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.
这个问题询问如何通过 SOAP Web 服务公开编程语言枚举。这是不可能的。原因如下:
通过“enum”,OP 表示一种编程语言构造,它生成由命名整数值组成的类型。例如:
这会在名称(例如
Value
)和相应的值(例如10
)之间产生关联。 SOAP Web 服务中没有这样的东西。SOAP Web 服务通过 WSDL(Web 服务描述语言)向客户端描述自身。 WSDL 使用 XML 架构 (XSD) 来描述要在客户端和服务之间交换的数据的形状。
XML 模式有一个“枚举”的概念,它描述某个元素或属性可以具有多个值之一。这些值在 XML 架构中列出(枚举)。例如,
这定义了一个可以采用三个字符串值之一的类型:“Value1”、“Value2”或“Value3”。这些值不以任何方式与整数值关联。为了便于理解,请考虑以下内容:
这定义了一个可以采用三个
date
值之一的类型:2013 年 6 月 1 日、2013 年 6 月 5 日或 2014 年 6 月 1 日。请注意,有既不与任何整数值关联,也不与任何字符串名称关联。最重要的是,XML 模式无法描述
枚举
的编程语言概念。因此,WSDL 无法使用枚举
的编程语言概念,这意味着 SOAP Web 服务无法公开枚举
。最好的办法是公开名称或值,但不能同时公开两者,如上面的
MyEnum
所示。当客户端使用该服务时,它可能会将MyEnum
转换为编程语言的enum
。但是,由于没有与任何名称关联的整数值,因此可以合理地期望每个客户端使用不同的整数值。一个客户端可能使用1
作为Value1
,而另一个客户端可能使用0
。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:
This produces an association between the names, such as
Value
, and the corresponding values, such as10
. 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,
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:
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 anenum
, which means that SOAP web services cannot expose anenum
.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 translateMyEnum
into anenum
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 use1
forValue1
, while another might use0
.