Access field from an async call back
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.
- 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
精彩评论