显示 wcf 服务的枚举客户端
我不知道这是否可能,但我希望能够在客户端引用我的 WCF 服务中的枚举。我有一个核心项目,在该项目中枚举是:
public enum StatusType
{
Ok = 1,
Error = 2,
Unknown = 0
}
public enum DirectionType
{
None = 0,
ToSystem = 1,
FromSystem = 2
}
我有一个使用核心项目的服务项目,它正在设置核心项目中的枚举类型,如下所示:
[DataContract()]
static class EnumHelper
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
List<Type> knownTypes = new List<Type>();
// Add any types to include here.
knownTypes.Add(typeof(StatusType));
knownTypes.Add(typeof(DirectionType));
return knownTypes;
}
}
在界面中:
[ServiceKnownType( typeof(EnumHelper))]
[ServiceContract( SessionMode = SessionMode.Allowed)]
public interface HandlerService
当我调用一个方法时,无论是谁接受或返回一个枚举,它工作正常,但是我必须引用客户端项目中的核心项目才能使用枚举客户端,如果可能的话,我希望从服务中执行此操作。
我尝试将核心项目中的枚举设置为
[DataContract]
public enum StatusType
{
[EnumMember]
Ok = 1, /*!<Done with no error */
[EnumMember]
Error = 2, /*!<Done with error */
[EnumMember]
Unknown = 0, /*!<No data registered, default value */
}
无效。
我想在我的客户端项目中像这样使用它:
像 client.StatusType.Ok 或 Servicereference1.StatusType.Ok 或类似的东西, 像 Core.StatusType.Ok 这样的注释
我想要这个的原因是因为该服务应该在不同的项目中使用,并且我们不希望每个人都依赖于一个公共的 dll 库,如果可以跳过它的话。我使用 net.tcp 绑定该服务。希望这是可以理解的,感谢您的帮助:)
I don’t know if it’s possible, but I want to be able to refer to enums from my WCF service on the client side. I have one core project, and in that project the enums are:
public enum StatusType
{
Ok = 1,
Error = 2,
Unknown = 0
}
public enum DirectionType
{
None = 0,
ToSystem = 1,
FromSystem = 2
}
I have one Service project using the core project and it is setting the enum types from the core project likes this:
[DataContract()]
static class EnumHelper
{
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
List<Type> knownTypes = new List<Type>();
// Add any types to include here.
knownTypes.Add(typeof(StatusType));
knownTypes.Add(typeof(DirectionType));
return knownTypes;
}
}
And in the interface :
[ServiceKnownType( typeof(EnumHelper))]
[ServiceContract( SessionMode = SessionMode.Allowed)]
public interface HandlerService
when I call a method either who takes or returns an enum, it works fine, but I then have to refer to the core project in the client project to use the enums client side, I would want to do that from the Service if it’s possible.
I have tried to set the enums in the core project to
[DataContract]
public enum StatusType
{
[EnumMember]
Ok = 1, /*!<Done with no error */
[EnumMember]
Error = 2, /*!<Done with error */
[EnumMember]
Unknown = 0, /*!<No data registered, default value */
}
with no effect.
I want to use it like this in my client project:
Either like client.StatusType.Ok or Servicereference1.StatusType.Ok or something like that,
note like Core.StatusType.Ok
The reason I want this, is because the Service should be used in different projects, and we don’t want everyone to be dependent on a common dll liberary, if it’s possible to skip it. I use net.tcp binding for the service. Hope it was understandable, thanks for any help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在服务器和客户端之间共享类型和类,则必须将它们放入单独的程序集中,并在服务器和客户端上使用它。仅当您控制线路的两端时,这才有效,例如同时编写代码的服务器端和客户端(我相信您是)。
如果您在服务器端创建单独的
MyWCFTypes
程序集,则也可以在客户端项目中引用该程序集,并且在导入服务定义时,WCF 应重用现有的类,例如应重用您的>MyWCFTypes
类,无需为相同的枚举创建新类。If you want to share types and classes between server and client, you have to put them into a separate assembly, and use that on both the server and the client side. This only works if you control both ends of the wire, e.g. write both the server and the client side of the code (which I believe you are).
If you create a separate
MyWCFTypes
assembly on the server side, you can reference that assembly in your client projects as well, and when importing the service definition, WCF should reuse existing classes, e.g. should reuse yourMyWCFTypes
classes without creating new classes for the same enums.