I've created a small class library to asynchronously call a WebService(Fire and Forget. I don't need the result).
In a Windows Form application, the XXXAsync()
method works fine. But, in a Web Application, the process is locked until the XXXCompleted
event is fire.
My problem is: I tried to create a Delegate and use the Begin/EndInvoke to call the XXXAsync()
method. It worked fine, but, the w3wp process seems to be consuming a huge amount of memory. I'm calling the EndInvoke method properly. Invoking the GC.Collect
did not free any memory.
I also tried calling the BeginXXX and EndXXXX Methods from the WebService, and got the same result.
My WebService is a dumb HelloWorld, with a Thread.Sleep(10000)
.
How to solve this problem? Is there any other way to invoke the webservice asynchronously avoiding those leaks?
Edit[Added the Code below]:
WebService 开发者_StackOverflow社区code:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : WebService
{
[WebMethod]
[OneWay]
public string HelloWorld()
{
Thread.Sleep(10000);
return "Hello World ";
}
}
ClassLibrary:
public class HelloLibrary : IDisposable
{
private delegate void PerformWhatever();
public HelloLibrary(){}
public string Notify()
{
PerformWhatever p = new PerformWhatever(OnBeforeBegin);
p.BeginInvoke(EndInvokeHandler, p);
return "1";
}
private void OnBeforeBegin()
{
using (localhost.Service1 s = new localhost.Service1())
{
s.HelloWorldAsync();
}
}
private void EndInvokeHandler(IAsyncResult r)
{
var delegateInstance = r.AsyncState as PerformWhatever;
delegateInstance.EndInvoke(r);
}
public void Dispose(){}
}
And, in my Web Application, I call:
using (HelloLibrary library = new HelloLibrary())
{
library.Notify();
}
If you don't need the result and you own the Web Service you should consider implementing the fire and forget functionality on the web service side through the SoapDocumentMethodAttribute by setting it's OneWay property to true.
When an XML Web service method has the OneWay property set to true, the XML Web service client does not have to wait for the Web server to finish processing the XML Web service method. As soon as the Web server has deserialized the SoapServerMessage, but before invoking the XML Web service method, the server returns an HTTP 202 status code. A HTTP 202 status code indicates to the client that the Web server has started processing the message.
精彩评论