.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> WCF >> Post New Resource Bookmark and Share

 Subscribe to Articles

Discussing Contracts : in a spark

Posted By :Gaurav Arora      Posted Date :22/08/2009   Points :25   Category: WCF    URL: http://www.msdotnetheaven.com/forums
 


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

C# Code:

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

public class MsDnHService

{

 [DataMember]

 public string ServiceId {get; set;}

}

 

VB.NET Code:

<DataContract([Namespace] := "http://www.msdotnetheaven.com/SampleServices/MsDnH/2009")> _ Public Class MsDnHService Private _ServiceId As String     <DataMember()> _    

 Public Property ServiceId() As String        

Get            

 Return _ServiceId        

End Get        

Set(ByVal value As String)      

      _ServiceId = value      

   End Set    

 End Property End Class



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:

 

C# Code:

[ServiceContract]

public interface IMsDnHService

{

 [OperationContract]

 bool StartServices(MsDnHService msdnhService);

}

 

VB.NET Code:

<ServiceContract()> _

Public Interface IMsDnHService     <OperationContract()> _    

Function StartServices(ByVal msdnhService As MsDnHService) As Boolean

End Interface

 

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:

C# Code:

[MessageContract]

public class ProcessMsDnHRequestMessage

{

 [MessageHeader]

 public int requiestId;

 

 [MessageBodyMember(Position=0) ]

 public MsDnHRequest msdnhRequest;

}

 

VB.NET Code:

<MessageContract()> _

Public Class ProcessMsDnHRequestMessage     <MessageHeader()> _    

Public requiestId As Integer<MessageBodyMember(Position := 0)> _     Public msdnhRequest As MsDnHRequest

End Class

 

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

 

[ServiceContract]

public interface IMsDnHRequest

{

 [OperationContract]

 public MsDnHResponseMessage msdnhRequest (ProcessMsDnHRequestMessage message);

}

 

VB.NET Code:

 

<ServiceContract()> _

Public Interface IMsDnHRequest

 

End Interface



Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend