How set timeout on XDocument.Load(string uri)?

耗尽温柔 提交于 2019-12-19 07:15:46

问题


Is there a way to set a timeout on System.Linq.Xml.XDocument.Load(string uri)? Or should I use the technique described in Implement C# Generic Timeout?


回答1:


There is nothing built-in as far as I'm aware. You can fetch the XML content yourself with an instance of WebRequest (which you can set a timeout on) and then pass the XML data directly to the XDocument.Load method.

Technically, the most "robust" solution would be to implement XmlResolver yourself which uses a WebRequest in the GetEntity() implementation to do a timeout. Then create an XmlReader based on your XmlResolver and pass the XmlReader to XDocument.Open.

The reason I say that would be more "robust" is that if the XML file references other entities on the web server (e.g. a DTD) then you would probably want the timeout to apply for that as well and not just the initial request.



来源:https://stackoverflow.com/questions/2970746/how-set-timeout-on-xdocument-loadstring-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!