Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ContractNamespace for namespacing enums in WCF

I am struggling to generate correct WSDL namespaces for my C# enums using ContractNamespace instead of decorating each type with attributes.

The following code correctly generates the Person type in "http://www.mynamespace.co.za/", but for some reason Gender is in a different WSDL namespace, "http://schemas.datacontract.org/2004/07/SomeOtherNamespace".

What am I missing? Do enums require special treatment?

[assembly: ContractNamespace("http://www.mynamespace.co.za/", ClrNamespace = "SomeOtherNamespace")]

namespace SomeOtherNamespace
{
    public class Person
    {
        public int Age { get; set; }
        public Gender Gender { get; set; }
    }

    public enum Gender
    { 
        Male,
        Female,
        Other
    }
}

In my actual code, the types live in an external, generated assembly. The types cannot easily be decorated with custom attributes. ContractNamespace would be perfect if it can work for enums too...

In other words, the following works, but would be extremely painful to get into the code generation process.

[DataContract(Namespace = "http://www.mynamespace.co.za/")]
public enum Gender
{ 
    [EnumMember]
    Male,
    [EnumMember]
    Female,
    [EnumMember]
    Other
}
like image 965
carlmon Avatar asked Mar 19 '23 14:03

carlmon


2 Answers

Enums are a pain. You must decorate the enum for the ContractNamespace attribute to take effect.

[DataContract]
public enum Gender
{ 
    [EnumMember]
    Male,
    [EnumMember]
    Female,
    [EnumMember]
    Other
}

Should see that your type appears in the WSDL namespace you want.

like image 157
batwad Avatar answered Apr 01 '23 06:04

batwad


These links may help you

  • How to: Import Custom WSDL
  • WCF Extensibility – WSDL Import (and Code Generation) Extensions
like image 32
Sercan Avatar answered Apr 01 '23 04:04

Sercan