2008-09-17 4 views
2

ASP.NET에서 2.0의 SmtpClient.SendAsync로 작업하고 예외를 throw하는 경우 요청을 처리하는 스레드가 작업을 위해 무기한으로 을 기다립니다. 완전한.ASP.NET 2.0의 SmtpClient.SendAsync 버그

이 문제를 재현하려면 전자 메일을 보낼 때 확인할 수없는 호스트 의 잘못된 SMTP 주소를 사용하기 만하면됩니다.

SendAsync를 사용하려면 Page.Async = true로 설정해야합니다.

Page.Async가 false로 설정되고 보내기가 예외를 throw하면 스레드 이 차단되지 않고 페이지가 올바르게 처리됩니다.

TIA. 당신이가 SendAsync를 사용하는 Page.Async = true를 설정해야

답변

2

참고.

그 이유는 무엇입니까? 어떤 Page.Async가 문제의 원인 일 수 있는지 오해.

죄송합니다. 문제를 재현 한 예제를 얻을 수 없습니다.

편집 :

http://msdn.microsoft.com/en-us/magazine/cc163725.aspx (ASP.NET 2.0의 비동기 페이지 WICKED CODE)를 참조하면 RegisterAsyncTask()PageAsyncTask 클래스를 사용하지 않는 것이 코드의 예를 보면을, 나는 볼 수 있습니다. @Async이 true로 설정된 페이지에서 비동기 작업을 실행할 때이 작업을 수행해야한다고 생각합니다. MSDN 매거진의 예는 다음과 같습니다

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

BeginAsyncOperation 내부, 다음, 당신은 비동기 적으로 메일을 보내야합니다.

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 기능을 살펴 :

공공 무효가 SendAsync ( 은 MailMessage 메시지, 개체 경우 UserToken )

에는 반환 값이 없습니다입니다.

어쨌든 SmtpClient.SendAsync가 예외를 throw하지 않는 한 아무래도 정상적으로 작동합니다.