开发者

Access field from an async call back

开发者 https://www.devze.com 2023-02-17 05:04 出处:网络
Here is my workflow (i know its bad, but I need something working now): Create user. Render confirmation template asynchronously and return a string to the completed event.

Here is my workflow (i know its bad, but I need something working now):

  1. Create user.
  2. Render confirmation template asynchronously and return a string to the completed event.
  3. Send email using that string in the completed event.

In steps 1 and 2, I have a string with the email and assigned it to a private field before calling the render t开发者_JS百科emplate. But because I am sending the email after rendering the template asynchronously, in step 3, the field email is null.

How do I keep the email accessible to the completed event?

Sample code:

    public AuthUser RegisterUser(string email, string password, string name)
    {

       //Register user here, etc.

       //Assign to a private field so the render template callback can use it to send the email.
       email = authUser.Email;

       //after render completes, do something
       templateService.RendertemplateCompleted += new System.ComponentModel.AsyncCompletedEventHandler(templateService_RendertemplateCompleted);

       //render template
       templateService.RenderTemplateAsync(null, TemplateName.ConfirmEmail);

       return new AuthUser(CreateUserError.None);

    }


    private string email;
    void templateService_RendertemplateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        //Send confirmation email.
        mailDeliveryService.Send(email, e.UserState as string);

    }


You can send email with using delegete

0

精彩评论

暂无评论...
验证码 换一张
取 消