views:

217

answers:

3

I have declared an enum in my server code, and would like my client code to be able to use it. Unfortunately, it's not being auto-generated for some reson. My enum is declared similar to the following example:

[DataContract]
public enum MyEnum {
    [EnumMember]
    First = 1,
    [EnumMember]
    Second = 2
}

It's not declared inside a class, but alongside several classes that are auto-generated as well (all in the same namespace). I have no problem using these classes in my client code, but this enum isn't being generated, and therefore is not usable. Help!

Thanks!!

EDIT:

As of now, the service neither takes a "MyEnum" as a parameter anywhere, or returns it from a function. That's my problem. It's used several places in my server code, and I'd like to use it in a few places in my client code as well (without having to copy/paste an existing construct).

A: 

The easiest solution I came up for this problem was to create a simple WCF function that takes a MyEnum parameter and returns it. As a result, "MyEnum" will be exposed to my client.

//Declaration
[DataContract]
MyEnum GetMyEnum(MyEnum value);

//Definition
public MyEnum GetMyEnum(MyEnum value){
    return value;
}

Any alternatives that don't require this and/or are more graceful would be appreciated!

Pwninstein
The enum apparently has nothing to do with the service. If you want the client to use any code that is not related to the service, then you should put that code into an assembly that you share with the client. You then have versioning issues, but then, you asked for that.
John Saunders
If you'll read the edit of my question, you'll see that this enumeration is used in several places throughout both the client and server code. Its purpose is to make dealing with static id values in SQL Server much easier. For example, if I have a row in a Types table: [Id = 7, Type = "The Seventh Type"], I could refer to it as "Types.Seventh" instead of the int value "7".
Pwninstein
A: 

In the kind of separation concerns client server model I assume you're using, you need some shared assembly between your client and server to store items used in both, like this enum and other classes they will both need to understand. A good way to do this might be using interfaces (won't help with the enum, but that can be thrown into the shared assembly).

marr75
A: 

Seems to me there are a couple options. If the service interface (ServiceContract) exposes a parameter or return value with a typeof equal to the enum, then the client will get the enum via the service definition. In practical terms, the client-side code generated from the WSDL by svcutil will include an enum type that corresponds to the thing used in the interface.

If the enum type is not used anywhere in the public interface of the service, then you can rely on an assembly of shared types.

Cheeso
Not always an option though. If you are working on a Windows Mobile app for instance, Standard .NET Dlls cannot be referenced by the WinMo6 framework
xximjasonxx