Skip to content

Commit 284d60e

Browse files
authored
Improving doc for disposing response (#409)
***NO_CI***
1 parent 2cc14c8 commit 284d60e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ using FileStream fs = new FileStream($"I:\\i-am-a-binary-file.bin", FileMode.Cre
126126
response.Content.ReadAsStream().CopyTo(fs);
127127
```
128128

129+
### Disposing the response
130+
131+
> [!Important]
132+
> You **MUST** dispose the response sent but any of the request. The device memory is limited and the response content will never be disposed if not asked. Depending on the devices, you may have only 16 possible requests before you'll get an out of memory.
133+
134+
Here is few right patterns to make sure you'll dispose properly the response content:
135+
136+
```csharp
137+
using HttpResponseMessage response = client.Get(apiUrl);
138+
// do whatever you want
139+
// When the response object won't be used, it will be disposed
140+
```
141+
142+
You can as well have an explicit using for a block:
143+
144+
```csharp
145+
using HttpResponseMessage response = client.Get(apiUrl)
146+
{
147+
// do whatever you want
148+
// when exiting the block, the response will be disposed
149+
}
150+
```
151+
152+
Or explicitly dispose the response:
153+
154+
```csharp
155+
HttpResponseMessage response = client.Get(apiUrl);
156+
// do whatever you want
157+
// Dispose explicitly the content
158+
response.Dispose();
159+
```
160+
161+
In all cases, you **MUST** make ure you are disposing the response content.
162+
129163
### Debugging through a reverse proxy
130164

131165
When code is deployed to a MCU it might be desirable to let the device connect to your development machine running IIS Express.

0 commit comments

Comments
 (0)