Create .net web service with soap protocol

Why to create .net webservice? webservices are developed for exchanging data with another application, that application can be developed in different language or technology, in webservice, we use xml data format to send or receive data over HTTP, web service are designed for application to application communication. webservice uses xml-based SOAP protocol, full form of SOAP is- simple object access protocol.

There is another alternate approach for application-to-application communication, which can be developed using web api, in new application development, we prefer web api than web service, at the end of this post, i introduce web api development using different asp.net framework .

Create Asp.net Webservice

Here we learn how to create our first webservice using asp.net framework, add .asmx file as given below, as we have added "WTRService.asmx". The asmx file will come with a code behind file, for example with specified file, there will be a “WTRService.asmx.cs” file created automatically.

Now let's look how to create service and webmethod, in code behind file WTRService.asmx.cs, we write all service methods.

using System; using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebTrainingRoom
{
/// < summary>
/// This is a sample webservice called WTRService, you can name anything
/// < /summary>
[WebService(Namespace = "http://webtrainingroom.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class WTRService : System.Web.Services.WebService { [WebMethod] public string WelcomeMessage() { return "Welcome to WebTrainingRoom.Com"; } [WebMethod] public string GetClientName(int clientId) { // here you write code to fetch client details based on clientId return "WebTrainingRoom.Com"; } } }

Now, let's run the webservice, this is how the newly created service will look like

create webservice

Now, we need to test the webservice, simply click on webmethod, provide the parameter value and click on "invoke", you can see how SOAP data format will look like in picture below.

test webservice

Deploy Asp.net Webservice

Deployment of asp.net webservice is just like deploying another asp.net application, you can publish the application in your local environment, then move it to wherever you want it, if you are using visual studio 2019 then it will allow you to deploy in multiple location from studio directly, including azure server.

While deploying any web service, make sure you copy all the files associating with that application.

Web Service vs Web API

All Web services are APIs but all APIs are not web services, web service provide a wsdl file (web service description language) in a soap XML format, when and API can provide xml and json format without extra overhead.

Nowadays all new application developers prefer web api over webservice, below are some web api tutorials that will help you to understand web api development, setting security, deployment and testing.

Learn more about Web API

Good to Read
You may also read
AI and Machine Learning
AI and Machine Learning
Work from Home like a Professional
work from home
Avoid Internet Addiction- Increase Productivity
avoid internet addiction