How to properly dispose of a WebResponse instance?

前端 未结 6 410
轻奢々
轻奢々 2021-01-03 23:21

Normally, one writes code something like this to download some data using a WebRequest.

using(WebResponse resp = request.GetResponse())  // WebRequest reques         


        
6条回答
  •  自闭症患者
    2021-01-03 23:34

    A very interesting question (although worth pointing out that the WebResponse object will have been disposed as you exit the using). My gut feeling is that it doesn't really matter that you've got a reference to this disposed WebResponse object as long as you don't try to do anything "operational" with it.

    You can probably still access certain properties on the instance for logging purposes (such as the ResponseUri) without getting an ObjectDisposedException, but overall reference held by the exception is not there so you can continue using the instance.

    I'd be interested to see what others say.

提交回复
热议问题