c# - 在客户端和 WCF 服务之间共享枚举?

标签 c# wcf enums

我在将我的公共(public)枚举类从我的 WCF 服务共享到我的客户端程序时遇到问题。 (我希望能够从我的客户端程序访问每个枚举属性)。 (我已将我的服务添加为我的服务引用)。 (为了测试,我只有两个 EnumMemer - 我知道..)

我的 Service.svc.cs 文件中有这个:

namespace ITHelperService
{
[DataContract]
public class Service : IService
{
    [DataMember]
    public CommandsEnums comands;

    [DataContract(Name="CommandsEnums")]
    public enum CommandsEnums
    {
        [EnumMember]
        Get_IPConfig,
        [EnumMember]
        Get_IPConfig_all,
        Get_BIOSVersion,
        Get_JavaVersion,
        Get_RecentInstalledPrograms,
        Get_RecentEvents,
        Get_WEIScore,
        Do_Ping,
        Do_NSLookup
    }
}
}

这是我的 IService.cs 文件:

namespace ITHelperService
{
[ServiceContract]
[ServiceKnownType(typeof(ITHelperService.Service.CommandsEnums))]
public interface IService
{


}
}

我已经在互联网上搜索过这个问题,看来上面的方法应该可以解决问题。 但是我无法在我的客户端程序中访问它们。它不会显示在智能感知中。

有什么意见吗?

最佳答案

我认为您在这里混淆了一些事情。

  1. IService 中没有任何操作。一个 ServiceContract 应该有几个 OperationContracts,你在你的服务类中实现。
  2. 您的 IService 的实现,服务类,不应该是 DataContract!它是 IService 接口(interface)的实现。
  3. 正如 Simon 所指出的,Enum CommandsEnums 不应该在 Service 类的实现中。

我会这样建议: IService.cs 文件:

namespace ITHelperService
{
 [ServiceContract]
 [ServiceKnownType(typeof(ITHelperService.Service.CommandsEnums))]
 public interface IService
 {
  [OperationContract]
  void Test();
 }
}

Service.svc.cs 文件:

namespace ITHelperService
{
[DataContract]
public class Service : IService
{
    public void Test()
    {
     // This is the method that you can call from your client
    }

}

 [DataContract(Name="CommandsEnums")]
    public enum CommandsEnums
    {
        [EnumMember]
        Get_IPConfig,
        [EnumMember]
        Get_IPConfig_all,
        Get_BIOSVersion,
        Get_JavaVersion,
        Get_RecentInstalledPrograms,
        Get_RecentEvents,
        Get_WEIScore,
        Do_Ping,
        Do_NSLookup
    }
}

关于c# - 在客户端和 WCF 服务之间共享枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17455539/

相关文章:

c# - 用 TDD 思考 OO——从哪里开始?

c# - 子窗口显示时,父窗口最小化

c# - 使用 msmq 队列对服务进行负载平衡?

WCF + SSL,获取方法调用的空结果

java - 如何使用JPA将多个​​枚举值保存到数据库?

c# - 从字符串中删除特定的特殊字符

c# - ICommand 实现问题

c# - 如何强制 Unity 创建新实例?

c# - 防止 ServiceStack 序列化 ENUM

mvvm - RelayCommand <enum>上的RaiseCanExecuteChanged()不会导致CanExecute()被调用