How to create web service in VS2019
In this article, we will learn How to create and consume ASMX Web Service in ASP NET Core 5.0. There are two types of Web services implementation used in software development we have a SOAP and REST API. Web Services are applications that allow communications between devices over the internet.
Make sure to have installed the following
- The latest version of Visual Studio
- SQL Server
I. Create a SOAP Webservice in Visual Studio 2019
1- Create an empty ASP.NET Web Application(.NET Framework) .We will use this project as the soap service demo .To do that just follow the steps below.Start VS 2019 a new project and in the search field write web and in the select ASP.NET Web Application(.NET Framework) as shown bellow:
press to Next and give the project name: WebServiceSoapDemo, location and Framework.
press to Next and select an Empty ASP.NET Web Application(.NET Framework). and then on Create.
After creating a new project this is how your solution with an empty template will look like. Please see the image below.
2. Add a new item by right-clicking your project solution and select Add » NewItem. Then search for Web Service ASMX.
3- Now, press on “Add” button, This will generate a template for an ASMX web service (WebServiceSoapDemo.asm) that contains a default web method HelloWorld. Check out the image attached below.
Now, that we have our Soap Web service, we are now ready to add our sample method which in my case I’ll create a login Web Method. This method will validate my login credential on my website and return an error if not validated or does not exist on my user table.
II. Create a Sample WEB Method Login
Let’s create a sample Login method that we will consume later in our ASP.Net Core Web Application. Follow the steps below to add the sample code.
This is the code added to my ResponseModel class. Create a new class file then name it ResponseModel then copy the code snippet below.
ResponseModel :
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebServiceSoapDemo.Models { public class ResponseModel<T> { public T Data { get; set; } public int resultCode { get; set; } public string message { get; set; } } }
Response Code | Description |
---|---|
200 | Email and password found |
500 | Email and password does not exist |
You can copy the code snippet below on your WebServiceSoapDemo.asmx file.
[WebMethod]
public ResponseModel<string> login(string email, string password)
{
ResponseModel<string> response = new ResponseModel<string>();
if (email != null)
{
using (SqlConnection conn = new SqlConnection(@"Server=CODERSIGN\SQLEXPRESS01;Database=IdentityDemo;User Id=freecode;Password=freespot;"))
{
SqlCommand cmd = new SqlCommand("sp_loginUser",conn);
cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@email", SqlDbType.NVarChar, 50).Value = email; cmd.Parameters.Add("@password", SqlDbType.NVarChar, 50).Value = password; SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(); da.Fill(dt);
if (dt.Rows.Count > 0)
{
response.Data = JsonConvert.SerializeObject(dt);
response.resultCode = 200;
}
else {
response.message = "User Not Found!"; response.resultCode = 500;
}
}
} return response;
}
3- Now, run your project and make sure you see the method you just created. See the image below.
WebMethod | Description |
---|---|
HelloWorld | Default method created that will return a “Hello World” string |
login | login validation method |
Soap Service URL: https://localhost:44399/WebServiceSoapDemo.asmx
Conclusion
I have created a Web Service soap demo project and have added a ResponseModel, then created a Login web method in the WebServiceSoapDemo to validate user login, and tested it that it is working fine.
The code can be download from the GitHub
In myNext post I am going to create a ASP.NET Core Web Application Project. to consume this Web Service (WebServiceSoapDemo.asmx)
This post was part of Web and WCF Services step by step