WCF Contract and Terminologies with Examples

Datetime:2016-08-23 04:33:35         Topic: WCF          Share        Original >>
Here to See The Original Article!!!

Before starting the series on the WCF services, I want to discuss the differences between WCF contract which include Service contract, data contract and Operation contract. Apart from this other basic WCF terminologies which we should be aware of with C# code examples.

WCF Contract

Operation Contract

  • Methods are decorated with OperationContract attribute
  • Operation are exposed to clients.
  • Clients are not aware of any of these operations internals.
  • Details of these operations can change which client need not to be botherd abou.t
  • Removes application configuration from a client’s computer like connection string

Service Contract

  • Client needs to know what operations are available from the service as well as what data to send and receive.
  • Service contracts define the list of the API’s or operations of a service.
  • It is an interface with member functions only which has [ServiceContract] applied to it.
  • It is only WCF specific unlike DataContract which can be used outside WCF for serialization.

Data Contract

    • Data contracts provides the shape of the data
    • Represents the data that goes to the service and comes from the service
    • They are the only piece that both the client and server share.
    • Data contracts are the classes that are sent as request or received as response. Sending and receiving data can be done using multiple primitive parameters for request or encapsulating all of them in single class.
    • DataContract is used to serialize the contracts which is an opt in way to serialize the data.
    • Name of the class should end with suffix Data for DataContract e.g. PersonData which is a good coding practice
    • DataContract is present in the System.Runtime.Serialization namespace.
    public class PersonData
        public int Name { get; set; }

What is a service?

  • Service is the implementation of the service contract for the service side of wire.
  • Entry point for the client and manages all down level calls
  • Expose the business logic to the outside world

What is a service proxy ?

To access the services and for hiding the complexities of the services from the client we put a layer between Client and service and this layer is known as Service proxies.

This is the layer that extract all the low level communication that is necessary to establish a connection with the service layer which is on another machine and doing hand shaking and taking care of security , doing serialization and deserialization. Client only needs to know about certain operation.

In my next article I will develop a WCF service project using C# by using all the concepts which I have discussed here in this article.


Put your ads here, just $200 per month.