How to consume local asmx web service from .net console application

658 Views Asked by At

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:

Connected Services Console App

enter image description here

1

There are 1 best solutions below

4
Kiran Shahi On BEST ANSWER

I hope you have added Service References in your project to your client app. If not follow the following steps to add.

  1. Right click on client project.
  2. Select Add
  3. Select service reference. (Add service reference dialog will appear.) Add service reference dialog
  4. Select WCF Web Service and click next.
  5. Add URI of your service in URI input and click next. Add new WCF Web Service service reference dialog
  6. Click Next enter image description here
  7. Click finish enter image description here

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:

using ServiceReference1;

namespace ConsoleClient3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            WebService1SoapClient client = new(WebService1SoapClient.EndpointConfiguration.WebService1Soap);

            HelloWorldResponse result = client.HelloWorldAsync().Result;
           

            //var add = client.HelloWorldAsync();
            Console.WriteLine(result.Body.HelloWorldResult);
            //Console.ReadLine();
        }
    }
}

You are getting your hello world result as HelloWorldResponse. Check you response type and result in that object.

Preview of result on asmx client console application.

Similarly, to call SumOfNums method follow the following example

using ServiceReference1;

namespace ConsoleClient3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            WebService1SoapClient client = new(WebService1SoapClient.EndpointConfiguration.WebService1Soap);

            int result = client.SumOfNumsAsync(1, 2).Result;

            Console.WriteLine(result);
        }
    }
}

Here, the return type is int so you can print it as a result directly.

Sum result from asmx service in console client applicaiton.

For reference, the asmx web service code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [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;
        }

    }
}

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