Web Service export enum to consumer

B

bsma1

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.
 
M

Mariano Omar Rodriguez

Apply the Attribute XmlEnumAttribute for each option of the enumearion.
 
J

John Saunders

Mariano Omar Rodriguez said:
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
 
B

bsma1

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.
 
B

bsma1

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.
 
G

Green Mellon

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.

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.
 
J

John Saunders

John Saunders said:
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.types.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top