SvcUtil and Enumeration Types

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

If you try to create the contract with an enumeration type, it could happen that the enum-type is not properly parsed during SVCUTIL import.
To make it clearer, take a look on following example:

namespace Abc
{
[DataContract(Namespace = "http://daenet",
Name = "ProcessState1")]
public enum ProcessState1
{
 
Started,
 
Stopped
}

 public enum ProcessState2
 {
  Started,
  Stopped
 
}
}


When SVCUTIL is started to import the metadata from the service which make usage of these two
types following the ProcessState1 will be imported as a string type and not as enum as you
probably expect. It will look approximately as:

namespace http_daenet
{
   public string ProcessState
}


The type ProcessState2 will be imported in as a type which will be deserialized by using of XmlSerializer
and not DataContract-Serializer as ProcessState1.

namespace Abc
{
  public enum ProcessState2
  {
    Started, Stopped
  }
}

Personally, O would like to have the first type ProcessState1 as enum and not as string. If you start the
client which uses such generated type the invocation of the target operation will fail with some very, very
amassing error, which is so bad and nothing saying, that I do not want to paste it here.

Unfortunately, this cannot be declared as a well feature of SVCUTIL. But, fortunately, there is a way to
workaround this problem. Following example shows hot to force SVCUTIL to generate proper enum type
in the proxy file:

namespace Abc
{
  [DataContract(Namespace = "http://daenet",
  Name = "ProcessState1")]
 
public enum ProcessState1
 
{
   
[System.Runtime.Serialization.EnumMemberAttribute()] 
    Started
   
[System.Runtime.Serialization.EnumMemberAttribute()] 
    Stopped
 
}
}

Now, the generate type looks perfect:

namespace http_daenet
{
  [DataContractAttribute()]|

  public enum ProcessState
  {
    Started, 
    Stopped
  }
}


Posted Jul 03 2007, 11:23 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.