diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index a2aa529..5356a89 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -215,7 +215,7 @@ namespace MontoyaTech.Rest.Net.Example } [RouteGroup("Stream")] - [RouteResponse(typeof(MemoryStream))] + [RouteResponse(typeof(FileStream), Parameter = true)] public static HttpListenerResponse Download(HttpListenerContext context) { return context.Response.WithStatus(HttpStatusCode.OK).WithText("Hello world"); diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index a6d07c4..c29ba1f 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.4.7 + 1.4.8 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RestCSharpClientGenerator.cs b/Rest.Net/RestCSharpClientGenerator.cs index a89631a..d52e9bd 100644 --- a/Rest.Net/RestCSharpClientGenerator.cs +++ b/Rest.Net/RestCSharpClientGenerator.cs @@ -342,6 +342,12 @@ namespace MontoyaTech.Rest.Net writer.Write(this.GetTypeFullyResolvedName(routeRequest.RequestType)).Write(" request"); } + if (routeResponse != null && routeResponse.Parameter) + { + writer.WriteSeparator(); + writer.Write(this.GetTypeFullyResolvedName(routeResponse.ResponseType)).Write(" input"); + } + writer.WriteLine(")").WriteLine("{").Indent(); //Generate the message code @@ -433,24 +439,31 @@ namespace MontoyaTech.Rest.Net //Generate the response code if (this.StaticCode) - writer.WriteBreak().WriteLine($"var response = {this.ClientName}.HttpClient.Send(message);"); + writer.WriteBreak().WriteLine($"var response = {this.ClientName}.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);"); else - writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message);"); + writer.WriteBreak().WriteLine("var response = this.Client.HttpClient.Send(message, HttpCompletionOption.ResponseHeadersRead);"); //Handle the response if (routeResponse != null) { writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent(); - if (routeResponse.ResponseType.IsEquivalentTo(typeof(MemoryStream))) + if (routeResponse.ResponseType.IsAssignableTo(typeof(Stream))) { - writer.WriteBreak().WriteLine($"var stream = new {this.GetTypeFullyResolvedName(routeResponse.ResponseType)}();"); + if (routeResponse.Parameter) + { + writer.WriteBreak().WriteLine("response.Content.CopyToAsync(input).GetAwaiter().GetResult();"); - writer.WriteBreak().WriteLine("response.Content.ReadAsStream().CopyTo(stream);"); + writer.WriteBreak().WriteLine("return input;"); + } + else + { + writer.WriteBreak().WriteLine($"var stream = new {this.GetTypeFullyResolvedName(routeResponse.ResponseType)}();"); - writer.WriteBreak().WriteLine("stream.Seek(0, System.IO.SeekOrigin.Begin);"); + writer.WriteBreak().WriteLine("response.Content.CopyToAsync(stream).GetAwaiter().GetResult();"); - writer.WriteBreak().WriteLine("return stream;"); + writer.WriteBreak().WriteLine("return stream;"); + } } else { diff --git a/Rest.Net/RouteResponse.cs b/Rest.Net/RouteResponse.cs index 11077c6..a6b2214 100644 --- a/Rest.Net/RouteResponse.cs +++ b/Rest.Net/RouteResponse.cs @@ -22,6 +22,11 @@ namespace MontoyaTech.Rest.Net /// public bool Json = true; + /// + /// Whether or not the response is passed as a parameter to the route function. + /// + public bool Parameter = false; + /// /// Creates a default route response. ///