nicholas
nicholas

Reputation: 125

Contracts in WCF - Differences

I could see that Operation Contract in WCF is inside the Service Contract

[ServiceContract]
public interface IService1
{

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

How can i explain the difference between the two.

Upvotes: 0

Views: 157

Answers (1)

Hamdi Baligh
Hamdi Baligh

Reputation: 892

The difference between the two of them is that an operation contract is a part of a service contract. WCF provides a manner for exposing web services. The contract of a service ([ServiceContract]) is a set of operational actions ([OperationContract]) that can be consumed by a client. The service is something that is shared between the client and the service provider. In WCF, service contracts are seperated into interfaces in order to provide a level of abstraction between the inernal mechanisme with which the service is working and the definition of a service. So the Service Contract is the definition of the whole service. Once the client gets a remote reference of the service (also called a proxy of the service), he can call one of the operation contracts which are encapsulated into it. For Example, if you want a service for remotely managing a set of students into the database, you will create an interface (that will be your service contract) that you will call IStudentManager (you can call it what you like). And this interface has the operations that will define the set of possibilities given to the client of the web service.

[ServiceContract]
public interface IStudentManager
{

    [OperationContract]
    void AddStudent(Student s);

    [OperationContract]
    void DeleteStudent(int studentId);

    [OperationContract]
    void UpdateStudent(int studentId);

    [OperationContract]
    Student GetStudent(int studentId);

    // TODO: You can add other services operations here
}

In this case, the client will request a remote reference of the service, which will be communicated to him over the network. From this reference (of type IStudentManager): the ServiceContract, will be used to trigger actions on the service. These actions in our case are: Add/Update/Delete/Get Students: The OperationContracts.

Upvotes: 1

Related Questions