Search code examples
c#.netwcfdatacontractservicecontract

Can I expose a data member in a WCF ServiceContract?


In a WCF service, is it possible to include a data member inside a ServiceContract definition? Doing something like this:

namespace My.Service.Contracts
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        ResultObject[] Search(SearchParams searchParams);

        [DataMember]
        MyCustomClass MyDataMember { get; }
    }
}

Can I expose MyDataMember from inside the ServiceContract? The scenario would go like this: The following class, which implements the service contract has member data that I would like to expose using a public field/property. Something that looks like this: I'm trying to expose a field/property in a class which implements a service contract. For example:

public class MyService : IMyService
{
    private MyCustomClass _datafield;

    ResultObject[] Search(SearchParams searchParams){
        //Do the search
    }

    MyCustomClass MyDataMember {
      get: { return _dataField; }
    }
}

Solution

  • is it possible to include a data member inside a ServiceContract definition?

    Although the compiler will quite happily let you "add" a property decorated with [DataMember] to the service interface, any WCF client will not see the property.

    So if your service interface was defined as:

    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        ResultObject[] Search(SearchParams searchParams);
    
        [DataMember]
        MyCustomClass MyDataMember { get; }
    }
    

    ...and say you generated the client proxy by Add Service Reference, you would see no mention of MyDataMember:

    enter image description here

    Note, at the point of adding the service reference you won't see any properties either.

    enter image description here

    It does not make sense to add properties to a service interface, nor does it make sense to add [DataMember]. You add [DataMember] to a class decorated with [DataContract] and referenced in your service's interface.

    MSDN has this to say on data contracts:

    A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged. - Golly, tell me more...

    WCF essentially is all about invoking methods (actually it is more about creating a unified communications API giving you RPC as the free set of steak knives). Methods are generally invoked by sending SOAP messages to the service (though it could be REST too). Messages have properties which are decorated with [DataMember] to indicate that the property should be serialised and included in the message stream. There's also [MessageContract] but we won't go there.

    Anyway, one does not access "a property" on a WCF service, instead you invoke a method.

    Tell me more

    To learn more about the epic-ness that is WCF, why not check out the link below. There's even a rather nice example at the bottom: