Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asynchronous webservice call. No (Begin...) method available!

I know this has been addressed before but I have service that returns a string like so.

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{

    [WebMethod]
    public string Hello()
    {
        System.Threading.Thread.Sleep(10000);
        return "Hello User";
    }
}

I've read many examples that say I need to call the method like this:

        MyService my = new MyService();
        AsyncCallback async = new AsyncCallback(callback);
        my.BeginHello();
        Console.WriteLine("Called webservice");

The thing is when I added reference I coudn't get the BeginHello method. All I saw was the HelloAsync. So I used it like this in my console app.

        MyService my = new MyService();
        AsyncCallback async = new AsyncCallback(callback);
        my.HelloAsync();
        Console.WriteLine("Called webservice");

and defined a private callback method like this

    private void callback(IAsyncResult res)
    {
        Console.Write("Webservice finished executing.");
    }

In doing so, I get an error like this:

An object reference is required for the non-static field, method, or property 'AsyncWebserviceCall.Program.callback(System.IAsyncResult)

Why dont I get the BeginHello method & Why do I get this error as above?

Thanks for your time.

like image 666
user20358 Avatar asked Dec 22 '22 22:12

user20358


1 Answers

If the code is being run inside your public static void Main(string[] args) function then you need to make private void callback(IAsyncResult res) a static method:

private static void callback(IAsyncResult res)
{
    Console.Write("Webservice finished executing.");
}

That's why you're getting that error.

From ASP.NET 2.0 onwards there were some changes to how you make async web service calls. Do this instead:

MyService my = new MyService();
my.HelloCompleted += CallBack;
my.HelloAsync();
Console.WriteLine("Called service.");
Console.ReadLine();  // Wait, otherwise console app will just exit.

Your callback method signature changes to:

private static void CallBack(object sender, HelloCompletedEventArgs e)
{
    Console.WriteLine("Webservice finished executing.");
}

More info:

From Visual Studio 2005 onwards the Add Web Reference proxy generator no longer creates the BeginXXX/EndXXX methods. These methods were deprecated in favour of the XXXAsync/XXXCompleted pattern.

If you really need to work with the BeginXXX/EndXXX style async methods you can use one of the following methods:

  1. Use the WSDL.exe tool to create the proxy. For example:

    wsdl.exe /out:MyService.cs http://somedomain.com/MyService.asmx?wdsl

    Include the generated MyService.cs file in your project and use that instead of a Web Reference. You need to open a Visual Studio command prompt for this so that the .NET Framework SDK binaries are in your path.

  2. There is apparently a hack in Visual Studio (it may no longer be available). For more info see this MS Connect case:

Begin/End Async WebService Proxy Methods Not Generated in Web Application Projects

My advice would be to embrace the new approach.

like image 103
Kev Avatar answered Dec 24 '22 13:12

Kev