要使用的BeginInvoke异步调用的方法和结果保存在会话状态 [英] To invoke a method asynchronously by using BeginInvoke and save the result in Session state

查看:197
本文介绍了要使用的BeginInvoke异步调用的方法和结果保存在会话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个样本code调用的方法使用异步BeginInvoke的,我上一个WebForm按钮单击事件执行此。

按钮点击后,用户被重定向到一个不同的页面,用户等待结果。

该AuthorizePayment方法需要较长的时间来运行,并返回一个INT code。我想在一些地方保存在session或者cookie中(而不是dispaly)当我访问会话补充一点,code,它会引发空异常int值。如何在一个会话或一个cookie保存这个结果?

任何想法?

 公共类CreditCardAut​​horizationManager
{
  //委托,定义方法(S)的签名要异步执行
  公共委托INT AuthorizeDelegate(字符串creditcardNumber,
                                        日期时间expiryDate,
                                        双量);  //方法来启动异步操作
  公共无效StartAuthorize()
  {
      AuthorizeDelegate广告=新AuthorizeDelegate(AuthorizePayment);
      IAsyncResult的AR = ad.BeginInvoke(creditcardNumber,
                                       到期日,
                                       量,
                                       新的AsyncCallback(AuthorizationComplete)
                                       空值);
  }  //方法执行耗时的操作(此方法执行
  //异步从线程池中的线程)
  私人诠释AuthorizePayment(字符串creditcardNumber,
                               日期时间expiryDate,
                               双量)
  {
    INT授权code = 0;    //信用卡授权服务,打开连接...
    //授权信用卡(结果赋给授权code)...
    //信用卡授权服务关闭连接...
    返回授权code;
  }  //方法来处理异步操作的完成
  公共无效AuthorizationComplete(IAsyncResult的AR)
  {
    //请参见管理异步完成的EndInvoke会法
    //后面一节。
  }
}


解决方案

您不能可靠地执行在HTTP请求的工作,因为ASP.NET辅助进程可能会在任意时刻被中止。付款可能已经由供应商执行,但你的网站可能会认为它没有(和付款丢失)。我当然不希望进入我的信用卡数据转换成这样一个系统。

有一个更好的方式做,这是运行在一个AJAX请求支付,并授权code执行同步。

I have this sample code that calls a method asynchronously using begininvoke, I'm executing this on a button click event on a webform.

After the button click, the user is redirected to a different page, where the user waits for the result.

The AuthorizePayment method takes a long time to run and returns a int code. I want to store that int value somewhere in session or a cookie(but not to dispaly) When I access Session to add that code, it throws null exception. How do I save this result in a session or a cookie?

Any idea?

    public class CreditCardAuthorizationManager
{
  // Delegate, defines signature of method(s) you want to execute asynchronously
  public delegate int AuthorizeDelegate(string creditcardNumber, 
                                        DateTime expiryDate, 
                                        double amount);

  // Method to initiate the asynchronous operation
  public void StartAuthorize()
  {
      AuthorizeDelegate ad = new AuthorizeDelegate(AuthorizePayment);
      IAsyncResult ar = ad.BeginInvoke(creditcardNumber, 
                                       expiryDate, 
                                       amount,                  
                                       new AsyncCallback(AuthorizationComplete), 
                                       null);
  }

  // Method to perform a time-consuming operation (this method executes 
  // asynchronously on a thread from the thread pool)
  private int AuthorizePayment(string creditcardNumber, 
                               DateTime expiryDate, 
                               double amount)
  {
    int authorizationCode = 0;

    // Open connection to Credit Card Authorization Service ...
    // Authorize Credit Card (assigning the result to authorizationCode) ...
    // Close connection to Credit Card Authorization Service ...
    return authorizationCode;
  }

  // Method to handle completion of the asynchronous operation
  public void AuthorizationComplete(IAsyncResult ar)
  {
    // See "Managing Asynchronous Completion with the EndInvoke Method"
    // later in this chapter.
  }
}

解决方案

You cannot reliably execute work across HTTP requests because the ASP.NET worker process might be aborted at an arbitrary moment. The payment might have been executed by the provider but your website might think it hasn't (and the payment is lost). I certainly wouldn't want to enter my credit card data into such a system.

A better way to do this is to run the payment on an AJAX request and make the authorization code execute synchronously.

这篇关于要使用的BeginInvoke异步调用的方法和结果保存在会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆