Windows Communication Foundation (WCF) programming using Visual Studio
1. Windows Communication Foundation (WCF) using Visual Studio
2013
Now, let us see how to create simple WCF to do simple mathematical calculations.
I am using Visual Studio 2013 to create this project.
1. Invoke Visual Studio
2. Select New Project
3. Under Installed templates, choose Web → WCF → WCF Service Application
4. Give a name to this project. I have named this project as mathematics
5. Press OK button
6. Now, from Solution Explorer window, delete IService1.cs and Service1.cs files. See
the below picture.
7. Now, Right click on your project – Mathematics
8. Select Add → New Item →WCF Service
9. Give some name to this service. I have names it as maths. Notice that Imaths.cs and
maths.cs files created automatically and you can see them from the Solution
Explorer window.
10. Now open Imaths.cs file by double clicking on it
11. Look for void DoWork() method. Remove this method and add two methods add
and sub to return addition and subtraction of two numbers as shown (see the next
page)
Delete
these files
2. 12. Now, open maths.cs file double clicking on that file
13. Remove method public void DoWork() method. In its place add the code as shown
below:
3. 14. Like asmx web services, here too you can build this code and test by selecting Build
→ Build Solution option. Try it yourself an check whether system displays any error
messages or not.
15. You can also run these programs by clicking on Start button. Try it yourself.
Consuming WCF methods from
Now, let us see how to consume the above created methods from a web page.
1. Click on Project – Mathematics.
2. Select Add → New Item → Web Form. Give some name to this webform. I named it
as calculate
3. Change to Design mode
4. Add a Button to this form.
5. Double Click on the button
6. In the Button_click event, add the following codes.
7. Now, run the web page to display the sum and difference of 200 and 100.
Try it yourself:
- Instead of passing hard coded values 200 and 100, please add two textboxes in the
webform so that users can enter two numerical values at runtime.
- BK Dharmaraju, Bangalore, India – has around 30 years of
experience in the field of IT.