2008-09-17 10 views
2

ASP.NET 2.0でSmtpClient.SendAsyncを使用していて、例外をスローすると、要求を処理しているスレッドが、操作のために無期限に を待っています。コンプリート。ASP.NET 2.0のSmtpClient.SendAsyncのバグ

この問題を再現するには、メールを送信するときに解決できなかったホスト の無効なSMTPアドレスを使用します。

SendAsyncを使用するには、Page.Async = trueを設定する必要があります。

Page.Asyncがfalseに設定され、Sendが例外をスローすると、スレッド がブロックされず、ページが正しく処理されます。

TIA。

答えて

2

あなたがSendAsyncを使用する場合はtrue Page.Async =を設定する必要があります注意してください。

この背後にある根拠を説明してください。 Page.Asyncがあなたの問題の原因であるかもしれないことを誤解しています。

申し訳ありませんが、問題を再現した例を得ることができませんでした。

http://msdn.microsoft.com/en-us/magazine/cc163725.aspx(WICKED CODE:ASP.NET 2.0での非同期ページ)を参照してください

EDIT:あなたのコードの例を見ると、私はあなたがRegisterAsyncTask()PageAsyncTaskクラスを使用していないことがわかります。 @Asyncがtrueに設定されているページで非同期タスクを実行するときは、これを行う必要があると思います。 BeginAsyncOperationインサイド

protected void Page_Load(object sender, EventArgs e) 
{ 
    PageAsyncTask task = new PageAsyncTask(
     new BeginEventHandler(BeginAsyncOperation), 
     new EndEventHandler(EndAsyncOperation), 
     new EndEventHandler(TimeoutAsyncOperation), 
     null 
     ); 
    RegisterAsyncTask(task); 
} 

、そして、あなたが非同期的にメールを送信する必要があります。MSDNマガジンの例は次のようになります。

0

ここは私です。試してみる。

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     // Using an incorrect SMTP server 
     SmtpClient client = new SmtpClient(@"smtp.nowhere.private"); 
     // Specify the e-mail sender. 
     // Create a mailing address that includes a UTF8 character 
     // in the display name. 
     MailAddress from = new MailAddress("[email protected]", 
      "SOMEONE" + (char)0xD8 + " SOMEWHERE", 
     System.Text.Encoding.UTF8); 
     // Set destinations for the e-mail message. 
     MailAddress to = new MailAddress("[email protected]"); 
     // Specify the message content. 
     MailMessage message = new MailMessage(from, to); 
     message.Body = "This is a test e-mail message sent by an application. "; 
     // Include some non-ASCII characters in body and subject. 
     string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' }); 
     message.Body += Environment.NewLine + someArrows; 
     message.BodyEncoding = System.Text.Encoding.UTF8; 
     message.Subject = "test message 1" + someArrows; 
     message.SubjectEncoding = System.Text.Encoding.UTF8; 
     // Set the method that is called back when the send operation ends. 
     client.SendCompleted += new 
     SendCompletedEventHandler(SendCompletedCallback); 
     // The userState can be any object that allows your callback 
     // method to identify this send operation. 
     // For this example, the userToken is a string constant. 
     string userState = "test message1"; 
     try 
     { 
      client.SendAsync(message, userState); 
     } 
     catch (System.Net.Mail.SmtpException ex) 
     { 
      Response.Write(string.Format("Send Error [{0}].", ex.InnerException.Message)); 
     } 
     finally 
     { 
     } 
    } 
    private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) 
    { 
     // Get the unique identifier for this asynchronous operation. 
     String token = (string)e.UserState; 

     if (e.Cancelled) 
     { 
      Response.Write(string.Format("[{0}] Send canceled.", token)); 
     } 
     if (e.Error != null) 
     { 
      Response.Write(string.Format("[{0}] {1}", token, e.Error.ToString())); 
     } 
     else 
     { 
      Response.Write("Message sent."); 
     } 
    } 

} 
1

RegisterAsyncTaskを使用できませんでした。 BeginEventHandlerデリゲートで ルック:

パブリックデリゲートたIAsyncResult BeginEventHandler( オブジェクト送信者、EventArgsの 電子、 AsyncCallbackのCB、 オブジェクトextraData )

それはたIAsyncResultを返す必要があります。 は今SmtpClient.SendAsync機能を見て:

ます。public void SendAsync( はMailMessageメッセージ、 オブジェクトはUserToken )

は戻り値はありません。

SmtpClient.SendAsyncが例外をスローしない限り、とにかくこれはうまくいきます。