与WCF服务共享枚举 [英] Sharing Enum with WCF Service

查看:112
本文介绍了与WCF服务共享枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个不同的应用程序,我想分享一个C#枚举。我不清楚如何在常规应用程序和WCF服务之间共享枚举声明。



这是情况。我有2个轻量级C#destop应用程序和一个WCF Web服务,都需要共享枚举值。



客户端1具有

  Method1(MyEnum e,string sUserId) ; 

客户2有

  Method2(MyEnum e,string sUserId); 

Webservice有

 code> ServiceMethod1(MyEnum e,string sUserId,string sSomeData); 

我最初是创建一个名为Common.dll的库来封装枚举,然后只是引用所有需要枚举的项目中的图书馆。但是,WCF使事情变得困难,因为您需要将枚举作为该服务的组成部分进行标记。像这样:

  [ServiceContract] 
[ServiceKnownType(typeof(MyEnum))]
public interface IMyService
{
[OperationContract]
ServiceMethod1(MyEnum e,string sUserId,string sSomeData);
}

[DataContract]
public enum MyEnum {[EnumMember] red,[EnumMember] green,[EnumMember] blue};

所以....有没有办法在WCF服务和其他应用程序之间共享一个枚举?

解决方案

使用Common库应该很好。枚举是可序列化的,不需要DataContract属性。



请参阅:
http://msdn.microsoft.com/en-us/library/ms731923.aspx


枚举类型。枚举,包括标志枚举,是
可序列化。可选地,枚举类型可以用
DataContractAttribute属性标记,在这种情况下,
参与序列化的每个成员都必须标记
EnumMemberAttribute属性


< blockquote>

编辑:即使如此,将枚举标记为DataContract并使客户端库使用它也不会有问题。


I have few different applications among which I'd like to share a C# enum. I can't quite figure out how to share an enum declaration between a regular application and a WCF service.

Here's the situation. I have 2 lightweight C# destop apps and a WCF webservice that all need to share enum values.

Client 1 has

 Method1( MyEnum e, string sUserId );

Client 2 has

Method2( MyEnum e, string sUserId );

Webservice has

ServiceMethod1( MyEnum e, string sUserId, string sSomeData);

My initial though was to create a library called Common.dll to encapsulate the enum and then just reference that library in all of the projects where the enum is needed. However, WCF makes things difficult because you need to markup the enum for it to be an integral part of the service. Like this:

[ServiceContract]
[ServiceKnownType(typeof(MyEnum))]
public interface IMyService
{
    [OperationContract]
    ServiceMethod1( MyEnum e, string sUserId, string sSomeData);
}

[DataContract]
public enum MyEnum{ [EnumMember] red, [EnumMember] green, [EnumMember] blue };  

So .... Is there a way to share an enum among a WCF service and other applictions?

解决方案

Using the Common library should be fine. Enumerations are serializable and the DataContract attributes are not needed.

See: http://msdn.microsoft.com/en-us/library/ms731923.aspx

Enumeration types. Enumerations, including flag enumerations, are serializable. Optionally, enumeration types can be marked with the DataContractAttribute attribute, in which case every member that participates in serialization must be marked with the EnumMemberAttribute attribute

EDIT: Even so, there should be no issue with having the enum marked as a DataContract and having client libraries using it.

这篇关于与WCF服务共享枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆