Friday 3 August 2012

Sample web app that uses a WCF Service


A sample web application that consumes the my previous sample wcf service.
  • Open visual studio File --> New Web Site and name it as yours choice. It will create empty web site like this.
  • Right click on solution and select add new item.
  • Select web form and name it as CalculatorClient
  • Add this code in .aspx page.
  • Generate the button click events for above four buttons.
  • Now add the service reference to the web app as shown.
  • It leads you to a window which asking address for web reference. 
  • The address is nothing but our previous wcf service. Now run wcf service application i.e. my previous post then you will get a window with methods of service as shown then right click and copy the address as shown.
  • Then paste that address in address bar and name it as CalculatorService then click on Go next Ok as shown. 
  • Then service will be added to the solution as shown.
  • Then following lines of code automatically added to the web.config automatically the code as shown.It contains bindings and end points for the service.
  • Then write the following code in aspx.cs page.
  • Then run the application you will check all the methods of the service by using this app.
  • Note if the wcf service application stopped then our web app throws an exception.
  • If we click on stop and then use web application then it throws a error like this.
Note: Service has to be run continuously other wise we will get above error in all applications which uses the  service.


Monday 30 July 2012

Sample WCF Service


  • Open VS10 and select new project.

  • Select  WCF Service Application and  name it as SampleCalculator


  • Then solution explorer will created with following default files like IService1.cs and Service1.svc
  • Delete those files and right click on solution explorer--> Add New Item--> Select Wcf Service template and name it as Calculator.
  • Then two files created in Solution Explorer i.e. ICalculator.cs(ServiceContract) and Calculator.svc is a service file which is having inheritance from  ICalculator.cs
  • Then write the following in ICalculator.cs(ServiceContract)


  • Then go to Calculator.svc file and implement ICalculator interface as shown.




  • This will create following method definitions in  Calculator.svc file.
  • Then modify method definitions as shown.
  • Then our WCF Service is ready to run, press F5 or Run button this will lead you to the following screen.


  • Then our service ready to check how its working, all the methods we wrote in service are visible here as shown.
  • Then double click on any method which you want test. I'm going for Add() method. After double click on Add() method we will get method test screen as shown.
  • You can check service by giving values of "a" and "b" in [Request Pane] and click on invoke you will get return value in [Response Pane].
This is WCF Service. In my next post i will try to explain how to consume this Service by using a Web App.