开发者

Autogenerated enum definition causing problems on client side

I have a situation like this, where i have

  • WCF Service (VS2008) Hosted somewhere
  • Main Solution (VS2005) which has a Consumer Project of type 'class Library' with the Service reference to this WCF Service
  • In the data Contract i have a Data Member as follows...

    [DataContract]
    public class Cmd
    {
       [DataMember]
       public string CommandText;
       [DataMember]
       public CommandType CommandType;
    }
    

The proxy service.cs is generated in my consumer project's service reference Folder (might be because i am using vs 2005), which contains all the declarations of the service where enum CommandType has got a new definition

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.30")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/System.Da开发者_如何学Cta")]
public enum CommandType
{
    Text,
    StoredProcedure,
    TableDirect,
}

instead of this original enum from System.Data

public enum CommandType
{    
    Text = 1,    
    StoredProcedure = 4,    
    TableDirect = 512,
}

which causes incorrect assignment of CommandType values from client to server,

  1. what should i do to overcome this.
  2. can we override this CommandType enum on the WCF service to get same enum definition throughout.

Sorry for such a long problem statement...


Try defining your own enum which explicitly inherits from int and then casting from your custom enum to the System.Data enum when the custom enum arrives on the server:

public enum MyEnum : int {    
    Text = 1,    
    StoredProcedure = 4,    
    TableDirect = 512,
}

server side:

CommandType t = (CommandType)myEnumVariable;


Enums in WCF are just strings. Your CommandType values will exist in the SOAP payload as "Text", "StoredProcedure", etc. Only on the server side can you cast them as ints. If you want to use the int values in your contract then you have to define Cmd.CommandType as int and expose your enum via the "KnownTypes" directive. That sort of defeats the purpose of it being an enum though. Alternatively, just avoid casting on the client side altogether.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜