Sharing Enum with WCF Service

后端 未结 4 2211
你的背包
你的背包 2020-12-13 08:53

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 WC

4条回答
  •  春和景丽
    2020-12-13 08:57

    I must have had some issues with an outdated service reference or something. I went back and created a common library containing the enum, and everything works fine. I simply added a using reference to the service interface's file.

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

    and I dropped the following:

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

    I guess since the enum is referenced via ServiceKnownType, it didn't need to be marked up in the external library with [DataContract] or [Enumerator]

提交回复
热议问题