2

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.Data")]
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...

Bravo
  • 3,341
  • 4
  • 28
  • 43

2 Answers2

0

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;
Nathan
  • 6,095
  • 2
  • 35
  • 61
  • Thanks Nathan for that quick reply, but i have already tried it and it still shows the same enum definition at the client (second code block in my problem definition). and still have the same problem. I am getting value of "scObj.myCommandType" as 4 for stored proc at the client side assignment scObj.myCommandType = (Service1.myCommandType)cmd.CommandType; – Bravo Aug 17 '11 at 07:20
0

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.

gordy
  • 9,360
  • 1
  • 31
  • 43