I'm trying to learn web services. I started with basic template .asmx. Then I created a .net console application and add web reference to this app. It looks work fine. But I dont know how to call this services inside console and use its methods.
SoapDemo.asmx.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace soapdemo
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int SumOfNums(int FirstNumber, int SecondNumber)
{
return FirstNumber + SecondNumber;
}
[WebMethod(MessageName = "SumOfFloats")]
public float SumOfNums(float FirstNumber, float SecondNumber)
{
return FirstNumber + SecondNumber;
}
}
}
Program.cs :
using SoapDemo;
namespace ConsoleClient3
{
internal class Program
{
static void Main(string[] args)
{
WebService1SoapClient client = new WebService1SoapClient();
//var add = client.HelloWorldAsync();
//Console.WriteLine(add);
//Console.ReadLine();
}
}
}
Connected Services Console App:


I hope you have added Service References in your project to your client app. If not follow the following steps to add.
With this you have added reference to service to your client console application.
After adding Service Reference to your client app, you can access access HelloWorld method of your service looks as follows:
You are getting your hello world result as
HelloWorldResponse. Check you response type and result in that object.Similarly, to call
SumOfNumsmethod follow the following exampleHere, the return type is int so you can print it as a result directly.
For reference, the asmx web service code is as follows:
The above code and action are performed on Visual Studio Enterprise 2022. ASMX web service project was on .NET Framework 4.7.2 and console application was on .NET 7.0