[英]Fire and Forget (Asynch) ASP.NET Method Call
我们有服务将客户信息更新到服务器。 一次服务呼叫大约需要几秒钟,这是正常的。
现在我们有一个新页面,在一个实例中,可以更新大约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,添加新的服务方法也不应影响任何现有的消费者。
当然可以 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.