Asynchronous webservice call. The No (Begin ...) method is available!

I know this has been examined before, but I have a service that returns such a string.

[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 read a lot of examples that say that I need to call a method as follows:

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

The fact is that when I added the link, I did not get the BeginHello method. All I saw is HelloAsync. Therefore, I used it in my console application.

  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 message:

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

Why didn’t I get the BeginHello method and why did I get this error as above?

Thank you for your time.

+4
source share
2 answers

If the code runs inside your function public static void Main(string[] args) , you need to make private void callback(IAsyncResult res) static method:

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

That is why you get this error.

Starting with ASP.NET 2.0, there have been some changes in the way you make asynchronous 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 signature of the callback method changes to:

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

Additional Information:

Starting with Visual Studio 2005, the Add Web Reference proxy generator no longer creates BeginXXX/EndXXX . These methods were deprecated in favor of the XXXAsync/XXXCompleted .

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

  • Use the WSDL.exe tool to create a proxy. For instance:

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

    Include the generated MyService.cs file in your project and use this instead of the web link. You need to open the Visual Studio command prompt to do this so that the .NET Framework SDK executables are in your path.

  • There seems to be a hack in Visual Studio (it can no longer be accessed). For more information, see This MS Connect Case:

Start / End Async WebService proxy methods not created in web application projects

My advice would be to take a new approach.

+9
source

Here is what I changed on the client side to make it work.

  static void Main(string[] args) { MyService my = new MyService(); my.HelloCompleted +=new HelloCompletedEventHandler(my_HelloCompleted); my.HelloAsync(); Console.WriteLine("Called webservice"); Console.ReadKey(); } private static void my_HelloCompleted(object sender, HelloCompletedEventArgs e) { Console.Write("Webservice finished executing in my_HelloCompleted."); } 
+2
source

All Articles