简体   繁体   中英

Share enum between client and WCF service?

I am having problems sharing my public enum class from my WCF service down to my client program. (I want to be able to access every enum attributes from my client program). (I have added my service as i Service Reference). (For testing I only have two EnumMemer - I know..)

I have this in my Service.svc.cs file:

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
    }
}
}

And this is my IService.cs file:

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


}
}

I have searched the Internet about this problem and it seems that the above should do the trick. But I can't access them in my client program. It doesn't show up in the intellisense.

Any input please?

I think you are confusing a few things here.

  1. The IService does not have any Operations in it. A ServiceContract should have a few OperationContracts, that you implement in your Service class.
  2. The Implementation of your IService, the Service class, should NOT be a DataContract! It is your implementation of the IService interface.
  3. The Enum CommandsEnums should maybe not be inside the implementation of the Service class, as Simon pointed out.

I would suggest smth like this: IService.cs file:

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

Service.svc.cs file:

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
    }
}

Your enum shouldn't be included with the server-side code. If you want to share common code, then put it in a common location. That way both the client and server can reference it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM