C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » WCF »

Contracts in wcf


Posted Date: 30 Jun 2009      Posted By: Shuby Arora      Member Level: Gold     Points: 1   Responses: 4



Can anyone elaborate Contracts in WCF?

Regards,
Shuby
Check your feet while you are ranning,
Check your hand while you are writting.....
But never check your swept while you are struggling
Lets Share Knowledge...





Responses

Author: Gaurav Arora    30 Jun 2009Member Level: DiamondRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

Hi Shuby,

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

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

In <b>WCF[Windows Comunication Foundation]</b> can be grouped in following three different contract types:
<b>
<li>Data Contract</b>
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 :

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

Following is the piece of example:

[DataContract (Namespace="http://www.msdotnetheaven.com/SampleServices/MsDnH/2009")]
public class MsDnHService
{
[DataMember]
public string ServiceId {get; set;}
}

<b>
<li>Service Contract</b>
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);
}

<b>
<li>Message Contract</b>
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);
}



Hope, the above solve your query.



Thanks & regards,
Gaurav Arora - Sr. Editor
Me in My Own Style



Author: Viji RAJKUMAR    30 Jun 2009Member Level: DiamondRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

Hi shuby,

n WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.

WCF defines four types of contracts.


Service contracts

Describe which operations the client can perform on the service.
There are two types of Service Contracts.
ServiceContract - This attribute is used to define the Interface.
OperationContract - This attribute is used to define the method inside Interface.



[ServiceContract]
interface IMyContract
{
[OperationContract]
string MyMethod( );
}
class MyService : IMyContract
{
public string MyMethod( )
{
return "Hello World";
}
}


Data contracts

Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.

There are two types of Data Contracts.
DataContract - attribute used to define the class
DataMember - attribute used to define the properties.



[DataContract]
class Contact
{
[DataMember]
public string FirstName;

[DataMember]
public string LastName;
}




If DataMember attributes are not specified for a properties in the class, that property can't be passed to-from web service.

Fault contracts

Define which errors are raised by the service, and how the service handles and propagates errors to its clients.


Message contracts

Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format we have to comply with.

Regards

VIJI RAJKUMAR
Pls rate my Answer if it was supportive
http://vijirajkumar.blogspot.com



Author: Miss Meetu Choudhary    30 Jun 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hi Gaurav, Very Well written answer.
Shuby after reading Gaurav's Answer I don't think there is any need for further explanation of the answer so i am just rating the Gaurav's answer.

Thanks and Regards
Miss Meetu Choudhary (Site Coordinator)
Go Green Save Green
My Profile on Google



Author: Shuby Arora    02 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Thanks Meetu, you wrote my words.

I agree with Meetu's words : "there is any need for further explanation of the answer"

Gaurav - Your answer cleared all my doubts, thanks for the examples.

Viji - Good efforts.

Regards,
Shuby
Check your feet while you are ranning,
Check your hand while you are writting.....
But never check your swept while you are struggling
Lets Share Knowledge...



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Doubt
Previous : Newbie to wcf need few basic question answered
Return to Discussion Forum
Post New Message
Category: WCF

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use