Overriding Namespaces from Serialization
I’ve defined my own XML namespaces for the members of a data contract and now when I generate the client proxies I get some ugly CLR namespaces for the types. How do I fix this?
The mapping from an XML or WSDL namespace to a CLR namespace is a mechanical transformation of breaking apart the authority and path of the XML namespace, doing minimal fix-ups to avoid illegal CLR type names, and spitting out a type in an equivalently delimited CLR namespace. The generated CLR namespaces might be neither pretty nor support CLS compliance needed for maximum portability.
You can fix those generated CLR namespaces though by overriding the mapping for an XML namespace. The svcutil program has a /namespace option that takes as parameters the target XML namespace and the CLR namespace that you would like to use instead. You can also use * for the target namespace to set the CLR namespace for all target namespaces without an explicit mapping. For example, you could say:
svcutil /namespace:*,Example.Contracts
If you’re using Visual Studio instead of svcutil, then you instead need to set the namespace mappings in the generated svcmap file in your solution. Inside the svcmap file is a NamespaceMappings collection to which you can add NamespaceMapping elements with attributes for the TargetNamespace and ClrNamespace. You aren’t able to use * for the target namespace though in an svcmap file because Visual Studio already has a wildcard definition and you’re not allowed to have conflicting definitions.