简体   繁体   English

长时间加载C#.NET中的第一个连接

[英]Long time to load first connection in C# .NET

I'm making a program that connects to a website and downloads XML from it. 我正在制作一个连接到网站并从中下载XML的程序。 It then displays the information to the user. 然后,它将信息显示给用户。

The problem I am having is when I first open the program and start downloading the XML information it takes a really long time. 我遇到的问题是,当我第一次打开程序并开始下载XML信息时,这花费了很长时间。 When I load another page from the site with the program still open, it takes about half a second to download. 当我从该站点加载另一个页面且该程序仍处于打开状态时,下载大约需要半秒钟。 I was wondering if there was any way to avoid this. 我想知道是否有任何方法可以避免这种情况。

I currently use an HttpWebRequest to download the stream and a StreamReader to read it. 我目前使用HttpWebRequest下载流,并使用StreamReader读取它。 Then I go through and parse the XML using XLINQ. 然后,我使用XLINQ解析XML。

Try explicitly setting the proxy. 尝试显式设置代理。 If you don't have a proxy defined, the HttpRequest class will spend time searching for one. 如果您没有定义代理,则HttpRequest类将花费时间搜索一个。 Once it has (or hasn't) found one, it will use that information for the life of the application, speeding up subsequent requests. 一旦找到(或未找到),它将在应用程序的生命周期内使用该信息,从而加快后续请求的速度。

//internally sets "ProxySet" to true, so won't search for a proxy
request.Proxy = null;

You can also define this in the .config: 您也可以在.config中定义它:

<system.net>
  <defaultProxy
    enabled="false"
    useDefaultCredentials="false" >
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>

The first time delay can be due to a combination of the following: 第一次延迟可能是由于以下原因造成的:

  1. Time to resolve the server DNS entry 是时候解析服务器的DNS条目了
  2. Time to download the proxy autoconfig script, compile and execute it to determine the effective proxy 是时候下载代理自动配置脚本,对其进行编译和执行以确定有效的代理了
  3. network latency from your app to the proxy server (if there is a proxy server in your environment) 从应用程序到代理服务器的网络延迟(如果您的环境中有代理服务器)
  4. network latency from the proxy server to the actual destination server. 从代理服务器到实际目标服务器的网络延迟。
  5. The latency on the server to serve the XML document. 服务器上服务XML文档的等待时间。 If it has to traverse an in-memory object representation and generate the XML document, that might take some time. 如果必须遍历内存中的对象表示形式并生成XML文档,则可能要花费一些时间。 Also, if it is using techniques like XML-Serialization to generate the document, then depending on how the serializer is configured, the first call to serialize/deserialize always takes a long time, due to the fact that an intermediate assembly needs to be generated and compiled. 另外,如果它正在使用XML-Serialization之类的技术来生成文档,则取决于序列化程序的配置方式,由于需要生成中间程序集,因此第一次调用序列化/反序列化始终会花费很长时间。并编译。
  6. Parsing the XML on the client side might take time, esp if the XML document structure is very complex. 在客户端解析XML可能会花费一些时间,尤其是在XML文档结构非常复杂的情况下。
  7. If XLinq (like the XMLSerializer) generates temp assembly for the XML parsing & querying, then the first request will take more time than the subsequent ones. 如果XLinq(如XMLSerializer)为XML解析和查询生成临时程序集,则第一个请求将比后续请求花费更多时间。

To figure out which part is taking time, insert some time logging into your code using System.Diagnostics.Stopwatch(): 要弄清楚哪个部分需要时间,请使用System.Diagnostics.Stopwatch()在代码中插入一些时间:

// this is the time to get the XML doc from the server, including the time to resolve DNS, get proxy etc.
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
timer.Stop();
Console.WriteLine("XML download took: " + timer.ElapsedMilliseconds);

timer.Start();
// now, do your XLinq stuff here...
timer.Stop();
Console.WriteLine("XLinq took: " + timer.ElapsedMilliseconds);

You can insert a loop around this, and see what the difference for the various components between the first request and subsequent requests is. 您可以围绕它插入一个循环,并查看第一个请求和后续请求之间各个组件的区别是什么。

If you find that the difference is in the downloading, and not the querying, then you can investigate further by getting a network sniff using Wireshark . 如果发现差异在于下载而不是查询,则可以使用Wireshark获得网络嗅探来进一步调查。

Hope this helps. 希望这可以帮助。

You would probably have to do some more research to figure out what part of the request is taking longer on the first pass. 您可能需要做更多的研究才能弄清楚请求的哪一部分在第一遍中花费的时间更长。 My first instinct says that the DNS request to get the IP address for the domain name you specify is taking longer, because it isn't cached the first time it runs. 我的第一个直觉是说,获取您指定的域名的IP地址的DNS请求花费的时间更长,因为它不是在第一次运行时就被缓存的。 It could also be the web server on the other end that has to run some start-up scripts the first time you query it. 另一端的Web服务器也可能是您第一次查询时必须运行一些启动脚本的Web服务器。 You mentioned that the first request takes a long time, but you don't say how long. 您提到第一个请求需要很长时间,但是您没有说多长时间。 Is this causing a big problem that it takes so long to do the first request, or is it just an annoyance? 这是否会引起一个大问题,即需要很长时间才能完成第一个请求,还是很烦人?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM