Share via


ContractNamespaceAttribute

Back when I did an overview of custom namespaces, I omitted any namespace declarations that wouldn't appear in the final metadata. One of those declarations is the default namespace for data contracts. I had two example data contracts, one for faults and one for normal messages, although they actually work exactly the same in practice.

 [DataContract(Namespace = "example.com/faultcontract/datacontract")]
public class MyFault
{
   [DataMember]
   public string detail;
}

[DataContract(Namespace = "example.com/datacontract")]
public class MyData
{
   [DataMember]
   public string data;
}

These data contracts include an explicitly declared XML namespace. However, you can modify the test program in the earlier article to see what happens when those namespace declarations are removed. What you end up with is an ugly looking namespace based on a fixed prefix schemas.datacontract.org/2004/07/ and a suffix that is the CLR namespace.

If you want to get rid of that default namespace for every data contract you define, then you can add a ContractNamespaceAttribute to the assembly. This attribute defines the default XML namespace for data contracts that are located in a particular CLR namespace. You can retrieve a ContractNamespaceAttribute in the standard way for assembly attributes by calling GetCustomAttributes on the assembly with the ContractNamespaceAttribute type.

Next time: Configuring SSL Certificates for Windows Vista