簡體   English   中英

BeginGetRequestStream在WPF中需要很長時間進行回調

[英]BeginGetRequestStream takes a long time for callback in WPF

我有一個應用程序,它是負載測試器,並使用異步Web API向測試服務器發送大量流量。 該應用程序具有2個GUI版本:一個是通過標准.aspx格式控制的Web應用程序。 另一個是WPF表單應用程序。 但是,在兩種情況下,http代碼都是相同的,因此我對為什么性能有所不同感到困惑。

在WPF應用程序中,CLR調用GetRequestStreamCallback大約需要30秒。 在Web應用程序中,它更像40ms。 我懷疑這與2個應用程序中的線程模型有關(這里沒有顯示很多線程)。 由於GetRequestStreamCallback是回調,因此對其優先級沒有影響。

亞倫非常感謝任何見識

public class PendingRequestWrapper
{
    public HttpWebRequest request;
    PendingRequestWraqpper(HttpWebRequest req) {request = req;}
}

public class Poster
{
    public static void SendPost(string url) {
        HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create(url);
        request.Method = "POST";
        // more header setup ...

        PendingRequestWrapper = new PendingRequestWrapper(request);
        wrap.request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), wrap);        
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    PendingRequestWrapper wrap = asynchronousResult.AsyncState as PendingRequestWrapper;

    try {
        // End the operation
        System.Diagnostics.Debug.Writeln("Received req stream for " + wrap.request.RequestUri.ToString());
        Stream postStream = wrap.request.EndGetRequestStream(asynchronousResult);
    } catch(Exception e) 
    {
        // ...
    }
    // Use the stream
}

}

默認情況下,WPF的asp.net Web性能比IIS Web應用程序慢的原因是每個主機的默認連接限制為2。而在IIS應用程序中,默認值為32k。 解決方法是:

        ServicePoint myPoint = ServicePointManager.FindServicePoint(new Uri("http://example.com"));

        // WPF application needs this!
        myPoint.ConnectionLimit = 10000;

除了可能打開與同一主機的許多連接的負載測試類型之外,這可能與所有應用程序無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM