Basically a WCF Contract is an agreement between the two parties i.e. a Service and a Client. In Windows Communication Foundation, Contracts are categorized as behavioral or structural.
-
Behavioral Contracts
- ServiceContract attribute is used to mark a type as Service Contract that contains operations.
- OperationContract attribute is used to mark the operations that will be exposed.
- FaultContract defines what errors are raised by the service being exposed.
-
Structural Contracts
- DataContract attribute defines types that will be moved between the parties.
- MessageContract attribute define the structure of the SOAP message.
In this WCF Tutorial, we will discuss and implement Contracts in Windows Communication Foundation using a step by step approach with practical examples.
WCF Contracts Related:
Fault Contract in WCF **
Data Contract Vs Message Contract **
Data Contract Known Types **
Service Contract and Operation Contract
Service Contract basically describe the operations a service expose to other party( i.e. a client). We can map a WCF Service Contract to a WSDL (Web Service Description Language).
It’s recommended to apply ServiceContract attribute to an interface, although it can be applied to a class as well. Applying it to an interface give us clear separation of contract and it’s implementation.
It describes:
- what operations are exposed by the service?
- platform independent description of interface as well as methods of our service.
- MEP (Message Exchange Pattern) between the parties i.e. Request/Response, One-Way or Duplex . Please follow here for detailed description of MEPs.
In order to define a Service Contract, we will apply ServiceContract attribute to a .NET Interface and OperationContract attribute to methods as follows:
interface ISimpleService
{
[OperationContract]
string SimpleOperation();
}
{
public string SimpleOperation()
{
return “Simple Operation Result”;
}
}
In above code, we used ServiceContract attribute to mark ISimpleService (an interface) as a Service Contract and OperationContract attribute to method “SimpleOperation“. Further provided an implementation class “SimpleService“.
Note: ServiceContract attribute is not inherited, means if we are defining another interface/class as a Service Contract inheriting from ISimpleContract, we still have to explicitly mark it with ServiceContract attribute.
In above example, we use ServiceContract attribute without any parameter but we can pass parameters also like Name, Namespace, ConfigurationName, ProtectionLevel, SessionMode etc as follows:
interface ISimpleService
{
[OperationContract]
string SimpleOperation();
}
In this WCF Tutorial, we learnt about two behavioral contracts i.e. Service Contract and Operation contracts. To learn about Structural Contracts (Data Contract and Message Contract).
Other Related Articles:
- WCF Hosting (Console | Windows Service | IIS | WAS )
- WCF Tutorial Step by Step
- 5 simple steps to create your first WCF RESTful Service
- WCF Vs ASMX Service
- MVC3 Vs MVC4 Vs MVC5
- Difference betweeen ASP.NET WebForms and ASP.NET MVC
- 3 ways to generate proxy for WCF Service
- Post JSON data to WCF RESTful Service using jQuery
- Consuming a WCF RESTful Service using jQuery
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions