我在将我的公共(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
{
}
}
我已经在互联网上搜索过这个问题,看来上面的方法应该可以解决问题。 但是我无法在我的客户端程序中访问它们。它不会显示在智能感知中。
有什么意见吗?
最佳答案
我认为您在这里混淆了一些事情。
- IService 中没有任何操作。一个 ServiceContract 应该有几个 OperationContracts,你在你的服务类中实现。
- 您的 IService 的实现,服务类,不应该是 DataContract!它是 IService 接口(interface)的实现。
- 正如 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/