开发者

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 so开发者_Python百科lution.

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

Do you have a better approach?


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!


What I normally do is have a separate project that has all the classes and enumerations that have the kind of domain logic you are refering to (like GameType) that is shared across other projects and call it: Xpto.Common

I then reference that common project in both my projects. That makes the classes and enumerations reusable and keeps things organized.


Obviously, reusable classes must be put into a separate project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜