Go to DotNetFunda.com
 Online : 1500 |  Welcome, Guest!   Login
 
Home > Articles > WCF > Contracts in WCF : A simple view
  • Nominate yourself for FREE online training by Microsoft MVP on OOPS, ASP.NET, ADO.NET and Sql Server.
    Brought to you by DotNetFunda.Com. You can refer to your friends as well !

  • Now you can recommend your article from any website to be selected as "Article of the Day" on DotNetFunda.Com website. If approved, that article will be featured on our home page.

General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.

Submit Article | Articles Home | Search Articles |

Contracts in WCF : A simple view

 Posted on: 9/9/2009 3:55:30 AM by G_arora | Views: 958 | Category: WCF | Level: Beginner | Print Article
In simple words contract defines the functionality provides/offers by a service and functionaly uses by the client.

Main point to note is : Contract can be completely independent of the implementation of service.

Ask all your .NET related questions/clarifications here to get quicker solution.

Introduction

In simple words contract defines the functionality provides/offers by a service and functionaly uses by the client.

Main point to note is : Contract can be completely independent of the implementation of service.

In WCF[Windows Comunication Foundation] can be grouped in following three different contract types:

 

Data Contract
Describes a data structure.Maps CLR types to XSD. In other words we can say that it defines the Data received by and returned from Service.

Here CLR types are mapped to XML schemas. Data Contract requires explicit marking of the fields that should be serialized with the [DataMember] attribute. [DataMember] attribute can be used regardless wheteher the field is private or public.

Data contract is different from .NET Serialization like :

runtime serialization: all fields are serialized including private fields.This mechanism is used by Remoting.
XML serialization: only public fields and properties are serialized. This mechanism is used by Web Services.

Following is the piece of example:

 

[DataContract] (Namespace="http://www.msdotnetheaven.com/SampleServices/MsDnH/2009")]

public class MsDnHService

{

[DataMember]

public string ServiceId {get; set;}

}

Service Contract

Describes the operations a service can perform. Maps CLR types to WSDL. Also defines as,Service contract is used to define the WSDL that describes the service. This contract is defined with interfaces or classes with [ServiceContract] attributes and [OperationContract] attributes for method offered by the service. Here is an example:

[ServiceContract]

public interface IMsDnHService

{

 [OperationContract]

 bool StartServices(MsDnHService msdnhService);

}

Message Contract

Defines the structure of the message on the wire. Maps CLR types to SOAP messages.

It is used when complete control over the SOAP message is needed. With this one can specify that what part of the message should go into the SOAP header and what belongs in the SOAP body.[MessageContract] attribute is used to specify the Message Contract'. The header and the Body of the SOAP message are specified with the attributes [MessageHeader] and [MessageBodyMember].

Example:

[MessageContract]

public class ProcessMsDnHRequestMessage

{

 [MessageHeader]

 public int requiestId;

 

 [MessageBodyMember(Position=0) ]

 public MsDnHRequest msdnhRequest;

}


Now, above is used with the service contract as shown bellow:

[ServiceContract]

public interface IMsDnHRequest

{

 [OperationContract]

 public MsDnHResponseMessage msdnhRequest (ProcessMsDnHRequestMessage message);

}



Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About Gaurav Arora

Experience:7 year(s)
Home page:http://www.msdotnetheaven.com/forums
Member since:Monday, September 07, 2009
Level:Starter
Status: [Member]
Biography:MCA, M.Phill(Comp.Sc.),MCP, MCTS, Sr. Software Engineer.
 Latest post(s) from G_arora

   ◘ Discussing Constructors in C# posted on 9/15/2009 5:58:48 AM
   ◘ Explore State Management posted on 9/11/2009 10:38:15 AM
   ◘ Working with Silverlight2 controls : A detailed look posted on 9/9/2009 9:31:20 AM
   ◘ Silverlight2 : A detailed look posted on 9/9/2009 9:04:17 AM
   ◘ Lets Start devlopment using Silverlight2 posted on 9/9/2009 8:04:09 AM


 Responses
Posted by: Vuyiswamb | Posted on: 18 Oct 2009 12:50:45 PM

This is nice Article. you got to the Point. The DotnetFunda Article Structure is best if its kept. So that you can have a Conclusion in your articles.

Thanks for sharing


Kind Regards


Vuyiswa Maseko

Submit Article


About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)