Monday, March 26, 2012

Web Service export enum to consumer

I building a web service that has an enum I want the consuming
application to be able to use.
I have the enum declared in the web service as:
public enum myEnum
{
ONE = 1,
TWO = 2,
};
I want to be able to access these from the consuming application such
as: myConsumer.localhost.myEnum.ONE. However, I can't seem to get it
to work. How do I make this visible/export this enum to the consumer?
I would rather not duplicate the code. Thanks for any and all help.Apply the Attribute XmlEnumAttribute for each option of the enumearion.
<bsma1@.hotmail.com> wrote in message
news:1173457522.866567.128760@.q40g2000cwq.googlegroups.com...
>I building a web service that has an enum I want the consuming
> application to be able to use.
> I have the enum declared in the web service as:
> public enum myEnum
> {
> ONE = 1,
> TWO = 2,
> };
> I want to be able to access these from the consuming application such
> as: myConsumer.localhost.myEnum.ONE. However, I can't seem to get it
> to work. How do I make this visible/export this enum to the consumer?
> I would rather not duplicate the code. Thanks for any and all help.
>
"Mariano Omar Rodriguez" <mrodrige@.yahoo.com> wrote in message
news:euSfOtmYHHA.3848@.TK2MSFTNGP02.phx.gbl...
> Apply the Attribute XmlEnumAttribute for each option of the enumearion.
>
I don't believe that this will permit the client to treat the type as an
"enum". The reason is that there is no such thing as an enum in XML Schema,
and XML Schema is what is used to communicate type information from server
to client via the WSDL.
There is an xs:enumeration facet in XML schema, but it doesn't quite produce
the same thing as an enum in C#. Instead, it "enumerates" the set of
possible values for a type. It does not then associate a name with each
possible value.
One might manage some magic with schema importer extensions, but this would
only work for .NET clients. Note in particular, that not all languages have
the concept of an "enum".
John
Thanks for the reply. I'm having trouble figuring out how to apply the
XmlEnumAttribute. Here's what I tried, but neither worked:
public enum myEnum
{
[System.Xml.Serialization.XmlEnumAttribute]
ONE = 1,
[System.Xml.Serialization.XmlEnumAttribute]
TWO = 2,
}
I also tried:
[System.Xml.Serialization.XmlEnumAttribute]
public enum myEnum
{
ONE = 1,
TWO = 2,
}
Thanks for any and all help.
On Mar 9, 9:47 am, "Mariano Omar Rodriguez" <mrodr...@.yahoo.com>
wrote:
> Apply the Attribute XmlEnumAttribute for each option of the enumearion.
> <b...@.hotmail.com> wrote in message
> news:1173457522.866567.128760@.q40g2000cwq.googlegroups.com...
>
>
>
What would be the best way to share a bunch on constants (these are
preprocessor macros for a native DLL) between a web service and it's
consumer? Please include some C# code samples because I'm a newbie.
Thanks for the help.
On Mar 9, 10:17 am, "John Saunders" <john.saunders at trizetto.com>
wrote:
> "Mariano Omar Rodriguez" <mrodr...@.yahoo.com> wrote in messagenews:euSfOtm
YHHA.3848@.TK2MSFTNGP02.phx.gbl...
>
> I don't believe that this will permit the client to treat the type as an
> "enum". The reason is that there is no such thing as an enum in XML Schema
,
> and XML Schema is what is used to communicate type information from server
> to client via the WSDL.
> There is an xs:enumeration facet in XML schema, but it doesn't quite produ
ce
> the same thing as an enum in C#. Instead, it "enumerates" the set of
> possible values for a type. It does not then associate a name with each
> possible value.
> One might manage some magic with schema importer extensions, but this woul
d
> only work for .NET clients. Note in particular, that not all languages hav
e
> the concept of an "enum".
> John
On Mar 9, 2:23 pm, b...@.hotmail.com wrote:
> What would be the best way to share a bunch on constants (these are
> preprocessor macros for a native DLL) between a web service and it's
> consumer? Please include some C# code samples because I'm a newbie.
> Thanks for the help.
> On Mar 9, 10:17 am, "John Saunders" <john.saunders at trizetto.com>
> wrote:
>
>
>
>
>
>
First of all, if I understand your problem, you need the client to
know *the values* of the enum members. The *list* of enum members is
exported to the client just declaring a parameter of this type in any
of the methods in the WebService.
Now, to the point...
I've had problems like this in an application that I was developing a
while ago. After unsuccessfully trying to export those values to the
client, I finally took the decision to review my objectives. The
point is: Why do you want to export those values to the client as an
enum?
If the values are hardly related: They must be in separated constants.
If you want the client to use those values when it invokes some of
your methods, change the type of the arguments of those methods to the
enum type, so you get rid of values (enums are correctly published to
the client, then, but without the associated values).
If you want the client to use those values for itself you can export
an enum and a method who translates enums to values. Here is a sample
code:
...
public enum myEnum
{
ONE = 1,
TWO = 2,
}
[WebMethod]
public int getEnumValue(myEnum e)
{
return (int)e;
}
...
I know the performance issues that this approach has but I couldn't
figure out a better way to do it yet.
"John Saunders" <john.saunders at trizetto.com> wrote in message
news:%23VmHM7mYHHA.4000@.TK2MSFTNGP02.phx.gbl...
> "Mariano Omar Rodriguez" <mrodrige@.yahoo.com> wrote in message
> news:euSfOtmYHHA.3848@.TK2MSFTNGP02.phx.gbl...
> I don't believe that this will permit the client to treat the type as an
> "enum". The reason is that there is no such thing as an enum in XML
> Schema, and XML Schema is what is used to communicate type information
> from server to client via the WSDL.
Ok, I take back part of this.
If you have an enum type like this:
public enum MyEnum
{
One = 1,
Two = 2
}
Then this will be translated into the following XML Schema type:
<s:simpleType name="MyEnum">
<s:restriction base="s:string">
<s:enumeration value="One" />
<s:enumeration value="Two" />
</s:restriction>
</s:simpleType>
This will cause the following client proxy to be created:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public enum MyEnum {
/// <remarks/>
One,
/// <remarks/>
Two,
}
Note that the integer value of "One" on the client is zero.
The reason that I didn't think this would work is that the web services I've
recently created couldn't simply use s:restriction base="string". They had
to use s:restriction base="s:int".
Note that you can get some interesting effects when your <s:enumeration>
values are not valid .NET identifiers. For instance, an enum member with
value "a b c" gets the name "abc" on the client:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customerSpecific.t
ypes.umcase.um.cca.webservices.trizetto.com")]
public enum aaTesting {
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("a b c")]
abc,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("d e f")]
def,
}
John

0 comments:

Post a Comment