tags:

views:

195

answers:

3

Hello,

I am trying to asynchronously execute a web service from my ASP.NET web application. This particular web service (.asmx) belongs to the same project as my web application. I have noticed that when I reference the web service from another web application, I can call the web service asynchronously using the following code:

TestService service = new TestService();
service.TestMethod();

However, if I reference the web service via a separate web application, I notice that I have the option to execute it asynchronously using the following code:

ServiceProxy.TestService service = new ServiceProxy.TestService();        
service.TestMethodAsync();

The trick is, I want to asynchronously execute the web service from a web page that is in the same application as my web service. Is this possible? If so, how?

Is it possible to do this without putting my web service in a separate project?

Thank you,

A: 

It could still reference the same application as a web service, couldn't it?

Assaf Lavie
+1  A: 

First of all, I recommend you put your web services into a separate project.

Second, when you called:

TestService service = new TestService();
service.TestMethod();

you were falling into a trap that you made possible because they were in the same project. You were calling the TestMethod directly. That's not a web service call, it's a direct call to the TestMethod.

I suggest you put the service into another project, then use a Web Reference or Service Reference to access it from your web application.

EDIT: you could keep it in the same application, but that will continue to be confusing. You must always use a web reference or service reference if you're calling it as a web service.

If it's part of the same application, then why is it a web service at all?

John Saunders
A: 

To avoid the confusion, you should probably not be calling your web service method directly at all. Leave that method to be called by ASP.Net in response to a web method request.

You can, however, move the implementation of your web method into another class that can be instantiated and called by both the web service class and your web application at any other time. With that class in place, if you want to call one of its methods asynchronously, you should look at creating a delegate and using BeginInvoke() and EndInvoke().

GBegen