Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an enum type used in WCF service and Windows Phone 7 app

I'm developing a Windows Phone 7 app that uses a WCF service.

I need to use on both projects the following code:

public enum GameType
{
    MonoPlayer = 1,
    MultiPlayer = 2
}

I'm sure I must not define this enum on both projects, so I figure out that I need to find another solution.

I think I need to use a third project where I have to put enum.

Do you have a better approach?

like image 691
VansFannel Avatar asked Feb 04 '26 21:02

VansFannel


1 Answers

WCF uses contracts, so the enum must be decorated as a contract.

For instance, you can have:

[DataContract]
public enum GameType
{
    [EnumMember]
    MonoPlayer = 0,

    [EnumMember]
    MultiPlayer = 1
}

You put this enum file in a separate project, so that it can be shared by client and WCF service.

Then, in the service contract (i.e., the interface of your WCF service), you must declare the enum as a "known type", like so:

[ServiceContract]
[ServiceKnownType(typeof(GameType))]
public interface IMyService {...}

That should do it!

like image 180
Roy Dictus Avatar answered Feb 06 '26 11:02

Roy Dictus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!