Pages

Thursday, June 27, 2013

Contracts in WCF

The contracts are very important in WCF which are used to connect with the client.   

The following types of contracts are available in WCF
·         Service Contract
·         Operation Contract
·         Message Contract
·         Data Contract
·         Fault Contract

Service Contract
The service contract is the service which we will be exposing to the client .By accessing this service the client can access the operations available in the service. Basically service contract is a class. Any class which has the tag    “[ServiceContract]” above it will be considered as a service contract as can be seen in the below example.

Example 1:

namespace KSS.WCFService.ServiceContracts
{
    [ServiceContract]
    public partial Class KSSService
    {
        #region KSS 1                            
        [FaultContract(typeof(KSS.WCFService.FaultContracts.UnknownFault))]
        [OperationContract]
        MessageResponse Calculate(MessageRequest Request);                   
        #endregion
    }
}

Message Contract

Message contracts are the rapper classes over the attributes. The WCF communicates using SOAP messages so any data cannot be sent/received between client and server just like that. They have to wrap inside a message contract and then sent to client or server. Basically a message contract is a class. A class having the tag “[MessageContract]” above the class is considered as a Message contract as shown below.

Example2:

 
namespace KSS.WCFService.MessageContracts
{
                [MessageContract]
                public partial class MessageData
                {
                                private KSS.WCF.DataContracts data;
                                private bool isValid;
                                               
                                [MessageBodyMember]
                                public KSS.WCF.DataContracts.Reult Data
                                {
                                                get { return data; }
                                                set { data = value; }
                                }
                                               
                                [MessageBodyMember]
                                public bool IsValid
                                {
                                                get { return isValid; }
                                                set { isValid = value; }
                                }
                }
}

 

A message contract can contain data types like Integer, Strings along with collections, Lists etc. In the above example we can see that message contract has a data contract and a Boolean variable IsValid.

Data Contract

A data contract is the entities used in a normal C# code. Basically it’s a class which has the variables and its properties within it. Any class with the tag “[DataContract]” above it are considered as a data contract class as shown in below example.

Example3:

namespace KSS.WCFService.DataContracts
{
    [DataContract]
    public partial class DCCalculator
    {
                #region Declaring Variables
               
         private int  numberA;
         private int numberB;
         private int sum;
        [DataMember(Name = "ItemA", IsRequired = false, Order = 0)]
        public int NumberA
        {
            get { return numberA; }
            set { numberA = value; }
        }
        [DataMember(Name = "ItemB", IsRequired = false, Order = 1)]
        public int NumberB
        {
            get { return numberB; }
            set { numberB = value; }
        }
                               
        [DataMember(Name = "Result", IsRequired = false, Order = 2)]
        public int Sum
        {
            get { return sum; }
            set { sum = value; }
        }
        #endregion
    }
}
 

 

The members of the data contract class should have “[DataMember]” tag. Only those which have these tags will be visible for the client otherwise the definitions for those will not be available in the Proxy.

Fault Contract

A fault contract is a class which is used to add the fault or exception details and sent to the client if in case an exception has occurred in the services side. Basically it’s class. As can been seen in example 1 the tag “[FaultContract]” is used to define a fault contract.

We will create a WCF Service in the next blog. Hope this was helpful.

Introduction to WCF


Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments. We achieve SOA (Service Oriented Architecture) using WCF.

The main advantage of using WCF is that it doesn’t matter which language is used for client. If both client and server are built in the same language say C# then we could use remoting. Otherwise if the client is in Java and the services is in .net then we should use WCF. There are other advantages like the code reusability, scalability. For example I build a WCF service for the code which used commonly by most of the applications like say an employee information is commonly accessed by all the applications in an organization. Hence the operations like accessing the data, adding the employee data, deleting and updating would be made as a WCF service and all the applications can access this service. The code was reused which helps to reduce the coding effort and the cost in development.
Let’s see the ABCs of WCF .Every now and then we hear this when we talk about WCF. So what’s this ABC?

•       Address: The address is nothing but the URL and port where you can find the service. So it’s like you go to this address and this pin code you will find the service.

e.g. http://localhost:3753/Calculator/service

•       Binding: The binding consists of information which will be useful to the clients.

E.g. of bindings: - basichttpbinding, WSHttpbinding

•       Contract: A contract is nothing but an understanding between the client and the service that this is the way I send the messages and this is what you will respond with.

e.g.:- ICalculatorService

Now, that we understand the ABCs of a WCF. Together these are called as Endpoints.

Working smart using Reusbale assets/code


Every software engineer has this question in their mind how to meet the deadlines .How do I finish my work within the time allotted. Most of the time it happens that when you join a project either the technologies are new or the deadlines are very tough.

How many of us have faced the issue that a new requirement or scope change is given to us today and asked to finish off it by tomorrow before the manager has the status meeting. Almost everyone has faced this situation. There are 2 choices we have 1. Say “No” that you cannot do in this timeline and justify why you cannot do or what problem you are facing and give them the assurance that this is the new timeline that I would require and this is what you are going to do. 2. Don’t say anything try whatever you can and end of the day give them the status.

Well I have a third approach which many in software engineer don’t just think of or do. The third approach is creating a repository of reusable code. When we have time we need to make a repository of the most commonly used codes in the technology like for example you are doing a WPF application in this try out few examples which are available in MSDN or few books and these would be the most commonly useful codes in that technology.  

Instead of us going and searching in Google at a time when new change request or requirement is given and the end moment I prefer we search and keep the code with us in local and use it whenever necessary.  There are 2 advantages a) you will achieve the deadline without much struggle no matter how stringent the deadline may be. b) You already have a working code and all you need is to copy the code. The spare time you get you can invest in learning and more productive work.

Reusable assets are a very important part of an organizations growth. Reusable assets are created and on top there are new things built. Take example of Microsoft they have a .Net framework on top every year they build lot of new things. This they can afford to do because they have a framework which they have built and every time they don’t have  to start from scratch. Likewise it is helpful for us too.