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.
If the code is being run inside your
public static void Main(string[] args)function then you need to makeprivate void callback(IAsyncResult res)a static method: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:
Your callback method signature changes to:
More info:
From Visual Studio 2005 onwards the Add Web Reference proxy generator no longer creates the
BeginXXX/EndXXXmethods. These methods were deprecated in favour of theXXXAsync/XXXCompletedpattern.If you really need to work with the
BeginXXX/EndXXXstyle async methods you can use one of the following methods:Use the
WSDL.exetool to create the proxy. For example:wsdl.exe /out:MyService.cs http://somedomain.com/MyService.asmx?wdslInclude the generated
MyService.csfile 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.There is apparently a hack in Visual Studio (it may no longer be available). For more info see this MS Connect case:
My advice would be to embrace the new approach.