Click here to Skip to main content
12,549,492 members (44,425 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

WCF Contracts Simplified

, 17 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
WCF contracts simplified

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.

Service Contract and Operation Contract

Service Contract basically describes the operations a service exposes 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 its implementation.WCF Service Contract

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
             string SimpleOperation();

class SimpleService  :  ISimpleService
            public string SimpleOperation()
                    return “Simple Operation Result”;

In the 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 the above example, we use ServiceContract attribute without any parameter but we can pass parameters also like Name, Namespace, ConfigurationName, ProtectionLevel, SessionMode, etc. as follows:

[ServiceContract(Name = "MySimpleService")]
interface ISimpleService
             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

The post WCF Contracts Simplified appeared first on WCF Tutorial.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Imran Abdul Ghani
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 17 Jul 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid