REST Service with WCF - Testing with Fiddler

In this article I am explaining simple REST service which can accept HTTP GET Request.In response to this request service return data in XML format.You could use return type as Json format as well.This can easily set with WCF interface attribute as  ResponseFormat = WebMessageFormat.Xml.And if you associate your return type  with DataContract attribute System.Runtime.Serialization does the serialization for you.

1) Create Service Library Project

Choose service library Template project as shown below,

WCFLib

2) Define your Contract and your data model

Reference System.ServiceModel.Web will enbable the HTTP requests, by attaching Webget Abbribute to your Contract and also define the Response format and  UriTemplate which will be used for call the you method in the contract, which can be your method name as well.

Also define you data model class, attaching with Datacontract attribute will enable the runtime to serialize your object.

ContractAndService

3) Implement the contract method

It is just implement contract method in your service.Also attach the InstanceContextMode in the service behaviour attribute will you while hosting for example with console application.

Service

I have not done much change  in the app config for the service yet, you can use default service  configuration with webHttpBinding.

4) Hosting Your service with a Console application.

It time to Host your service, I am using simple hosting method which is useful debugging, Console application.

Reference the service library project and host the service as below.If you are running on Vista you might to run with administrator permission.

host

5) Make HTTP Request Using Fiddler Tool

Your service is running in this case at http://localhost:8732/RestService and you want to call HelloWorld method.You have used the UriTemplate as /Hello.

The URL for Get request is http://localhost:8732/RestService/Hello, use it the Fiddler tool as shown below.

fiddler

You can the out in the selected text.Similarly you can extend this and you can other HTTP methods POST,PUT.In that case you can use WebInvoke attribute for contract method.

Hello world with WCF

When I have started reading about WCF it looks like it involves lot XML configuration but its really cool.In this article I have started with simple example which does Hello world with WCF.

1) Create WCF Service Application

Create a Console application and reference it with System.ServiceModel. Add an interface/Contract through we expose the service functionalities. You could do this by attaching [ServiceContract] attribute for the interface.And add method that you want expose through the service contract, by attaching the attribute [OperationContract]. Now its time for the implementation of the method in your WCF service.

As shown below in the code snippet,

  1. 2) Configure your WCF Service.

Add configuration file to the solution to define the end points of your service.The endpoint of a service specifies an address where the service can be found, a binding that contains the information that a client must communicate with the service, and a contract that defines the functionality provided by the service to its clients.

Also define the service behavior and enable the service metadata behavior. End point is specified with the full length namespace of your contract which is WCFExample.WCFServer.ServerService.IWCFExample.

Now we specify the host base address in service tag. and it is http://localhost:8080/ServerService

The Configuration file will look as below,

  1. 3) running the WCF service

Instantiate the service in your Main as shown below.

  1. 4) Create a Client console application.

Add the service reference using the host address.it will create WCF client reference.Using the reference you could instantiate the WCF service and call the method, as shown below

Start WCF service and when you run the client application you could see the out put.

Editing Gridview Using LINQ

       This article describes the easiest way of editing gridview by using LINQ data source and 

       DataContext.

       Explained step by step as below,

1)      Create DataContext using Northwind Database.

 Drag and drop the Product table in to Northwind.dbml datacontext, as shown below.

 

2)      Add Grid view to webform and choose datasource for the Gridview.

Choose new datasource as LINQ which will add new LINQ datasource to page as shown below.

 

3)      Configure the datasource

Choose the all column of the Product Table and also tick the advanced options.

 

 

 

And enable the editing option of the grid view.

 

  

 

Yes you are done! When you run page you will see the grid is populated with all products and you also find the edit link with each row as shown below.

               

 

 

My Own Blog!

I have got opportunity through my friend to create a blog and it just happened in couple of hours!!!

I like it very much.

I would like keep this blog really technical….Let me see how far I can go…