Basic Knowledge Of Web Services

Introduction

Hello Everybody. Good day to you all. I would like to share how to add a web service with your existing web site/application. It is very useful to learn. We can reduce N number of repeated code if we are strong in web services.

Web Service

I would like to explain practically, what web service is and why we need web service. For example, I have a project "Student". It is hosted in one Server called "Server A". There is a project which is having similar functionality as project "Student" but with different objectives. I have to use all of what I used in project "Student" in the new project. To achieve what we would like to be achieve in many cases, we just copy all the business logic from project "Student" and paste in new project called "Extend_Student".

We almost repeated all the code that's available already in "Server A". Then,  why do we re-use the code? For the reason that we created the web service and accessed  whatever we needed from project "Student". For that, we need to create a web service and access the existing business.

Steps to create web service

  1. Open an existing project solution and add service.svc.

    project

  2. You can get two  more class files in App_Code folder in the solution, such as iservice.cs, service.cs (you can see the Image-1)

  3. Now, you have to define all the functions you need to access in the web service in iservice. Since it is an interface, you need to declare the function. Do not implement it here.

    code

  4. We have to define the function that we declared in iservice interface in Service.cs located in APP_Code Folder, as below.

    Make sure there is a reference of business in service class, so that we can access the business.

    Here, I referred using Business;

    referred

In the above image, steps are needed to implement web services in existing project. So, the web site exposes the service to the outside world.

I created another project for accessing existing web service.

web service

I need to add Service reference for this project to access web service. Right click on the project Student_Extend -> Add Service Reference -> Pop up window will appear and select drop down from the window. It will show all the web services that are all available in the existing server. Pick the web service which you need to access it and click OK.

web service
We can get into coding. Once we have completed adding service reference, we need to create instance for serviceclient, as shown below:

  1. protected void btnName_Click(object sender, EventArgs e)  
  2. {  
  3.     WSStudent.ServiceClient objWSCALL = new WSStudent.ServiceClient();  
  4.     lblName.Text = objWSCALL.GetStudentName();  
  5. }  
code

All procedures are over. Now, get ready to access the business through the web service call. There are some other ways to access the web service, such as proxy generation. We will  discuss these later.

output

Conclusion

I Hope you all enjoyed this article and are excited about the output. It is very basic in web service. Once you get knowledge in this, we will understand the remaining concepts very easily. I wish you all the best.

 


Similar Articles