Search code examples
wcfattributesdatacontract

Passing Attributes on DataContracts in WCF


I have a a datacontract which has important attributes on. For example, in the following code:

[DataMember]
[Description("My Description")]
public string Name { get; set; }

I want the Description attribute to be auto generated on the client proxy.

Is there any solution, or workarounds besides massive duplication?

Many thanks!


Solution

  • You don't, not really. Remember that you're not passing instances of objects, but rather textual messages.

    If it's really important then you can abandon the generated proxy classes and share implementation of the data objects and contracts instead, however this is a bunch more work and of course you're running the risk of the client and server becoming out of sync.

    If you want to try that then put your contracts and operation interface into a separate assembly, with public modifiers, then try the following

    Binding binding = new BasicHttpBinding(); // or which one you 
    EndpointAddress endpoint = 
        new EndpointAddress("endpointUrl");
    ChannelFactory<IServiceInterface> channelFactory = 
        new ChannelFactory<IServiceInterface>(binding, endpoint);
    
    IServiceInterface client = channelFactory.CreateChannel();
    MyDataType result = client.Operation(myOtherDataType);
    
    ((IClientChannel)client).Close();