Skip to content

Commit 9325f7d

Browse files
committed
Don't dispose caller-owned resources
1 parent 9456f0c commit 9325f7d

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

ConsumePlugin/GeneratedRestClient.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ module PureGymApi =
565565
System.Uri ("users/new", System.UriKind.Relative)
566566
)
567567

568-
use httpMessage =
568+
let httpMessage =
569569
new System.Net.Http.HttpRequestMessage (
570570
Method = System.Net.Http.HttpMethod.Post,
571571
RequestUri = uri
@@ -799,7 +799,7 @@ module PureGymApi =
799799
System.Uri ("users/new", System.UriKind.Relative)
800800
)
801801

802-
use httpMessage =
802+
let httpMessage =
803803
new System.Net.Http.HttpRequestMessage (
804804
Method = System.Net.Http.HttpMethod.Post,
805805
RequestUri = uri

WoofWare.Myriad.Plugins/HttpClientGenerator.fs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,18 @@ module internal HttpClientGenerator =
667667
[
668668
yield LetBang ("ct", SynExpr.createLongIdent [ "Async" ; "CancellationToken" ])
669669
yield Let ("uri", requestUri)
670+
// Disposing an HttpRequestMessage disposes its Content. When the body wraps a caller-owned resource
671+
// (a Stream the caller supplied, or an HttpContent the caller constructed), disposing it would tear
672+
// down a resource whose lifetime is the caller's responsibility, so in those cases we don't dispose
673+
// the request message.
674+
let bodyOwnedByCaller =
675+
match bodyParam with
676+
| Some (BodyParamMethods.StreamContent, _)
677+
| Some (BodyParamMethods.HttpContent, _) -> true
678+
| _ -> false
679+
670680
yield
671-
Use (
681+
(if bodyOwnedByCaller then Let else Use) (
672682
"httpMessage",
673683
SynExpr.createNew
674684
(SynType.createLongIdent' [ "System" ; "Net" ; "Http" ; "HttpRequestMessage" ])
@@ -690,6 +700,7 @@ module internal HttpClientGenerator =
690700
(SynExpr.tuple [ SynExpr.createIdent "httpMessage" ; SynExpr.createIdent "ct" ])
691701
)
692702
)
703+
693704
if info.EnsureSuccessHttpCode then
694705
yield
695706
Let (

0 commit comments

Comments
 (0)