Fire和Forget(Asynch)ASP.NET方法调用

我们有一项服务来更新客户信息到服务器。 一次服务呼叫大约需要几秒钟,这是正常的。

现在我们有一个新页面,在一个实例中,可以更新大约35-50个Costumers信息。 此时更改服务界面以接受所有客户是不可能的。

我需要调用一个方法(比如“ProcessCustomerInfo”),它将遍历客户信息并调用Web服务35-50次。 异步调用服务并没有多大用处。

我需要异步调用方法“ProcessCustomerInfo”。 我正在尝试使用RegisterAsyncTask 。 Web上有各种示例,但问题是在我离开此页面后启动此调用后,处理停止。

是否可以实现Fire和Forget方法调用,以便用户可以从页面移开(重定向到另一个页面)而不停止方法处理?

详细信息: http : //www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

基本上,您可以创建一个委托,该委托指向您想要异步运行的方法,然后使用BeginInvoke将其启动。

// Declare the delegate - name it whatever you would like public delegate void ProcessCustomerInfoDelegate(); // Instantiate the delegate and kick it off with BeginInvoke ProcessCustomerInfoDelegate d = new ProcessCustomerInfoDelegate(ProcessCustomerInfo); simpleDelegate.BeginInvoke(null, null); // The method which will run Asynchronously void ProcessCustomerInfo() { // this is where you can call your webservice 50 times } 

这是我鞭打的事情……

 public class DoAsAsync { private Action action; private bool ended; public DoAsAsync(Action action) { this.action = action; } public void Execute() { action.BeginInvoke(new AsyncCallback(End), null); } private void End(IAsyncResult result) { if (ended) return; try { ((Action)((AsyncResult)result).AsyncDelegate).EndInvoke(result); } catch { /* do something */ } finally { ended = true; } } } 

然后

  new DoAsAsync(ProcessCustomerInfo).Execute(); 

还需要在Page指令<%@ Page Async="true" %>设置Async属性

我不确定它到底有多可靠,但它确实适用于我需要的东西。 也许一年前写的这个。

我认为问题在于,您的Web服务期望客户端返回响应,即服务调用本身不是单向通信。

如果您正在使用WCF进行Web服务,请查看http://moustafa-arafa.blogspot.com/2007/08/oneway-operation-in-wcf.html以进行单向服务调用。

我的两分钱: IMO无论谁将这个构造放在你身上,你都无法改变服务界面来添加新的服务方法,那就是提出无理要求的那个。 即使您的服务是公开使用的API,添加新的服务方法也不应影响任何现有的消费者。

你当然可以 。

我认为你想要的是一个真正的背景线程:

在ASP.NET 2.0中安全地运行后台线程

创建后台线程以记录IP信息