DCSIMG
WCF Quick Start - Designing Service Contracts - Gady Elkarif's Blog

Gady Elkarif's Blog

WCF Quick Start - Designing Service Contracts

WCF Quick Start - Designing Service Contracts

The first step in creating WCF service is to define the Service Contract. Each service in WCF consists of endpoints, and each endpoint consists of an address, a binding, and a contract.

Contracts of web services consists from set WSDL and XSD documents. In WCF, contract are a set of .NET type definitions annotated with special attributes, which can be turned into a set of WSDL and XSD documents.

There are three types of contracts in WCF:

  • Service Contract
  • Data Contract
  • Message Contract

Service Contracts

Service Contract describe the service itself. The following image show how to create new WCF Application Program (named ArticleService):

Create new WCF Application Program

 

By taking a simple interface (can be also class) and by annotating it with attributes, we can influence the mapping between the worlds of .NET types and operations to SOAP. With this information, WCF can perform the dispatching and the serialization (of Data Contracts). 

Each method in a service contract is assigned an action value based on service namespace and operation name, for example, the action of GetArticle operation contract is:

http://WcfQuickStart.org/ArticleService/GetArticle

You can override the Action and the Name as done with the two GetArticle methods.

Since interfaces seperating the contract from implementation, it is highly recommended to define service contract as an interface.

    [ServiceContract(Namespace = "http://WcfQuickStart.org/ArticleService/")]

    public interface IArticleService

    {

        [OperationContract]

        String GetArticleAuthor(Int32 id);

 

        [OperationContract(Action = "GetArticleByAuthorAndTitle")]

        Article GetArticle(String author, String title);

 

        [OperationContract(Name = "GetArticleById")]

        Article GetArticle(Int32 id);

 

        [OperationContract(IsOneWay = true)]

        void RateArticle(Int32 id);

    }

Data Contracts

The Data Contract is defined by the type used in the method signature. When a type which is different than Message is used, WCF performs serialization to map between .NET types to the body of the message.

    [DataContract(Namespace = "http://WcfQuickStart.org/DT/Article/")]

    public class Article

    {

        [DataMember]

        public Int32 Id

        {

            get;

            set;

        }

 

        [DataMember]

        public String CreatedBy

        {

            get;

            set;

        }

 

        [DataMember]

        public String Category

        {

            get;

            set;

        }

 

        [DataMember]

        public String Title

        {

            get;

            set;

        }

 

        [DataMember(Name = "Body", IsRequired = false)]

        public String Text

        {

            get;

            set;

        }

 

        [DataMember]

        public DateTime CreatedOn

        {

            get;

            set;

        }

    }

 

  • Only fields marked with DataMember will be serialized.
  • You can serialize private fields.

Message Contracts

If you need to support headers, you can create a class on which you have that models the structure of the entire SOAP envelope for a particular operation.

With [MessageBody] and [MessageHeader] attributes, you have more control on the content of the serialized data than a data contract, you can specify which field map to header versus body.

Lets add operation to IArticleService interface, including two message contracts:

    [OperationContract]

    GetArticlesResponse GetArticles(GetArticlesRequest message);

    [MessageContract]

    public class GetArticlesRequest

    {

        [MessageHeader]

        public String CreatedBy;

 

        [MessageHeader]

        public DateTime CreatedAfter;

    }

 

    [MessageContract]

    public class GetArticlesResponse

    {

        [MessageHeader]

        public String CreatedBy;

 

        [MessageBodyMember]

        public List<Article> Articles;

    }

Download the source code of WCF Quick Start: Contracts.

פורסם: Apr 17 2008, 04:19 PM by egady | with 3 comment(s)
תגים:, , , ,

תוכן התגובה

bred כתב/ה:

great work man thx

# May 20, 2008 3:55 AM

FUfUTQfdVaveroPn כתב/ה:

doors.txt;10;15

# September 15, 2009 2:20 PM

Miss90 כתב/ה:

Of course, being scooped is always a risk with research projects. ,

# October 23, 2009 3:40 AM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 2 and 1 and type the answer here:


Enter the numbers above: