Friday, February 3, 2012

WCF Service From CREATION to TEST


Expertising technologies while exploring their features, performances with correct concepts is a must for a developer since when the technology is familiarized, its easier to decide the best technological approach at the start of the application development . It is difficult to change the decision after starting the development. Similarly, if we select the wrong technology, whole application will get jeopardized. Today s topic is create simple WCF service and access the service from client application. Lets start with Microsoft .NET.I personally recommend WCF services other than any of Microsoft technologies exist mainly because of its well defined clean / organized architectural style and inbuilt data serialization . WCF also provides easy way of expose the service through REST or SOAP with few configuration steps. When we code layered application ,we create contracts (Interfaces) and plug and play them throughout our main application. That is very clean . WCF has introduced similar pattern with operation / data / fault contracts.

Open VS 2010 ( Which uses framework 4.0 ) . New project -> WCF service application . You will end up with Service.svc / Service.Cs / Iservice.CS  . Simply service and its interface .
Note – If you use  endpoint as soap should define  basichttpbinding as binding type in web.config file
In REST  you should create webhttpbinding as binding type .
Just create your first service metho without any confusion in service.cs  file . It will be just general method as you usually  type which returns  employee generic list .
Now this is wonderful . You dont need to worry about return data format or any other tags like “webmethod” . WCF take care of all those . After define a method  go to IService.cs file .
Now this is our service controller . We define all the information here like  behavior , what type of data it should return etc .
Eg - 

Service.cs file

public List<Employee> GetAllData()
        {
Return ListofEmployees ;
}
Iservice.cs 
[OperationContract(Name = "GetAllData")]
        [WebGet(UriTemplate = "/GetAllData",
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Json)]
        Employee GetAllData();

Uri Template defines, the way you should access your resource (service) .

Also notice WebGet keyword. It should be there for GET requests.



You can see I have defined Response Format as WebMessageFormat.Json. Don’t forget it can be XML either .Also keep remind , in some of my earlier articles I have mentioned the reason why most of the people love json rather XML .

Now how would you test your service .
This is why every developer who works with .NET should love opensource too.
RestClient is awesome firefox plugin just made for you to test your REST services .

Also it provides service to post the data to server with json format .

Before go for demonstrate  testing ill show you most easiest way to post json to server .
Well this might confuse some of you but , Since json data is just text strings I accept request as a stream and read it just as text input . You will end up with json string  posted by some client application . As I experienced this is most convenient and clean way to work with REST WCF POST services

Service.CS file

//Method Declaration
public Department  Add(Department  DepObj)
        {
        Return DepObj;
        }

You can notice it has built in support to post the data to server with json format
 . Before go for demonstrate testing I will show you most easiest way to
 post json to server . Well this is even also easy task , when MS new 
framework features support object serialization.You would create your 
json object which should 1-1(one-to-one) map with your serverside class .

JSON Object pass from client side
 
 DepObj:{
ID:2,
Name: finance
} 

IService.cs

Now this is how you should define your post method in service contract .

[WebInvoke(Method = POST;, UriTemplate =/Department;, BodyStyle = WebMessageBodyStyle.Bare, 
ResponseFormat = WebMessageFormat.Json)] 
[OperationContract] 
Department Add(Department DepObj); 

Notice here I have defined webinvoke type as POST.

This basic write up would be helpful you to move ahead with good understanding .

Now lets see how to debug and test your services .

Add RESTClient addon to firefox .

Put a break-point in service methid .Run a project and type baseuri+service name in restclient .
Select a request type GET or POST . If you have correctly done evrything ,  break points will be hit in project . Cheers !!.

Here I tried my level best  to demonstrate WCF REST service  from beginning to testing, And sorry for my lazy and unorganized writing .
This article was in my mind from like 2 years but with lack of free time I couldn’t involve in writing it . So please update me with mistakes find  here .
Ill provide you how to secure and authenticate your RESTFULL services with powerful methodology with my next post .  

Attachment on top shows you how to post data to your service from rest client.








2 comments: