0

我有一个使用 ASP.NET C# 的网站,它在一个地方运行得很慢。当我使用分析器检查它时,两个缓慢的地方是 GetResponse 和 GetRequest。我正在连接到第 3 方系统以获取日历上的可用性,因此我通过 POST 与他们进行通信。我们有一个在 ASP 经典中的对应物,它似乎更快,但我没有简单的方法来分析它。

我正在发送一个大约 440 字节的 XML 字符串字节编码,具体取决于一些事情,并且返回值低于 2k。我在这里包括了大部分相关的例程。我尝试将默认代理设置为GetEmptyWebProxy以防万一我读到它会有所帮助。

处理请求/响应需要 2 到 5 秒。压力很大,因为老板在数河马,而我无法获得经典版本的真实统计数据,所以目前这一切都是感知。

我的问题是——我是否尽可能快地前进,这是野兽的本性,还是我可以做些什么来加快这种交流?非常感谢任何帮助。

这是一些代码:

    protected void UpdateInventory(DateTime _SelectedDate)
{
    // build request object
    Envelope _Request = new Envelope();
    _Request.Body = new Body();
    _Request.Body.QueryEvents = new QueryEvents();
    _Request.Header = new Header();

    //setup search criteria for API request
    _Request.Body.QueryEvents.EventRangeBeginDate = _SelectedDate.ToString(_DTFormat);
    _Request.Body.QueryEvents.EventRangeEndDate = _SelectedDate.AddDays(1).ToString(_DTFormat);
    _Request.Body.QueryEvents.EventTypeID = _EventTypeId;
    _Request.Header.SourceID = "BackOffice";
    _Request.Header.MessageID = 0;
    _Request.Header.MessageType = "QueryEvents";
    _Request.Header.TimeStamp = LocaleHelper.LocalNow.ToString(_DTFormat);
    _Request.Header.EchoData = "BO Calendar";

    // send API request
    HttpStatusCode _Status = GetGatewayInventory(_Request);
}

protected HttpStatusCode GetGatewayInventory(Envelope _InvRequest)
{
    // set up return value
    HttpStatusCode _RetVal = HttpStatusCode.Unused;

    // initialize global inventory object
    _Inventory = new Envelope();

    // serialize request object into XML
    XmlSerializer _Serializer = new XmlSerializer(_InvRequest.GetType());
    MemoryStream _Stream = new MemoryStream();
    _Serializer.Serialize(_Stream, _InvRequest);

    XmlDocument _Doc = new XmlDocument();
    _Stream.Position = 0;
    _Doc.Load(_Stream);

    // remove unneeded info.
    XmlNode _Node = _Doc.SelectSingleNode("/Envelope");
    XmlElement _ENode = (XmlElement)_Node;
    _ENode.RemoveAttribute("xmlns:xsi");
    _ENode.RemoveAttribute("xmlns:xsd");

    // clean up
    string _XmlStr = _Doc.InnerXml.Replace("\"1.0\"", "'1.0'");
    byte[] _Bytes = System.Text.Encoding.ASCII.GetBytes(_XmlStr);

    // send string to gateway
    // set web request
    string _GWHost = _GatewayHostLive;

    HttpWebRequest _req = (HttpWebRequest)WebRequest.Create(_GWHost);

    IWebProxy myProxy = GlobalProxySelection.GetEmptyWebProxy();
    GlobalProxySelection.Select = myProxy;
    _req.Proxy = myProxy;      

    _req.Method = "POST";
    _req.ContentLength = _Bytes.Length;
    _req.ContentType = "text/xml; encoding='utf-8'";

    Stream _RequestStream = _req.GetRequestStream();        
    _RequestStream.Write(_Bytes, 0, _Bytes.Length);
    _RequestStream.Close();

    using (HttpWebResponse _Resp = (HttpWebResponse)_req.GetResponse())
    {
        _RetVal = _Resp.StatusCode;

        if (_Resp.StatusCode == HttpStatusCode.OK)
        {
            Stream _respStream = _Resp.GetResponseStream();
            XmlTextReader _xmlreader = new XmlTextReader(_respStream);
            XmlDocument _RespXml = new XmlDocument();
            _RespXml.Load(_xmlreader);
            _xmlreader.Close();

            // deserialize back into object
            StringReader _sr = new StringReader(_RespXml.InnerXml);
            XmlSerializer _XmlSr = new XmlSerializer(_Inventory.GetType());
            XmlReader _Inreader = new XmlTextReader(_sr);
            _Inventory = (Envelope)_XmlSr.Deserialize(_Inreader);

            StripExtrasIfOnlyShowFirstAvailable(_Inventory, CountTotalTickets());

            grd_EventTimes.DataSource = _Inventory.Body.Events.Event;
            grd_EventTimes.DataBind();
            grd_EventTimes.Visible = true;
            if (_Inventory.Body.Events.Event.Count > 0)
                lbl_GatewayId.Text = "GN: " + _Inventory.Body.Events.Event[0].EventName + "  ID:" + _EventTypeId ; 
        }
        _Resp.Close();
    }
    // exit and return value
    return _RetVal;
}
4

1 回答 1

0

您可以尝试通过禁用Nagle算法ServicePointManager.UseNagleAlgorithm = false,请参阅MSDN

Nagle 的(坏)影响可以为您的 POST 请求增加至少半秒的额外延迟。它与小型 POST 请求混在一起很糟糕,因为这些请求在等待(读取)响应之前对底层 TCP 流进行了两次写入。请参阅 Nagle 维基百科链接中的详细信息。

于 2013-11-06T19:07:04.057 回答