0
votes

I am creating a simple asmx web service and trying to consume them. Below is the asmx web service

public class AddTwoNumbers : System.Web.Services.WebService
{

    [WebMethod]
    public int GetSum(int a, int b)
    {
        return  a + b;
    }
}

This is the Windows Console application where I am consuming the service that was created

 namespace TestASMXService
 {
 class Program
 {
    static void Main(string[] args)
    {
        AddingNumber.AddTwoNumbers add = new AddingNumber.AddTwoNumbers();
        Console.Write("First Number:");
        int firstNum = Convert.ToInt32(Console.ReadLine());
        Console.Write("Second Number:");
        int secNum = Convert.ToInt32(Console.ReadLine());
        Console.Write ("The Sum is:",add.GetSum(firstNum, secNum));
        Console.ReadLine();
    }
}

It receives the user inputs and does not return any result or throws any error

enter image description here

Can anyone please tell me what I am missing here.

1
Out of curiosity, why ASMX?Dai
I am just learning about asmx. and trying to see how to consume the web methodsxyz
ASMX is deprecated, if not obsolete - if you want to learn about web-services I suggest you using ASP.NET Web API. ASMX shouldn't be used for new projects because it isn't supported by ASP.NET Core: stackoverflow.com/a/35830341/159145Dai
Yes I have done couple learning exercises in the Web API's. But can you please say if there is anything wrong with the above codexyz
I cannot see anything immediately wrong - I suspect it's a runtime configuration issue. Are you sure the ASMX service is running and accessible? If your proxy class was generated by Visual Studio you should be able to step-in to the GetSum call and see where it's blocking - I suspect it's blocking on getting the response. Are you running any firewall software that's blocking local-loopback connections?Dai

1 Answers

0
votes

You are trying to pass an argument in the Console.Write, but do not have a placeholder for it in the format string. The method GetSum will therefore not be called.

Change it to:

Console.Write("The Sum is: {0}", add.GetSum(firstNum, secNum));