From 1e5a20c2f2ae8cd9a6a746c66c2a9fb7771fcbf7 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Sat, 22 Sep 2018 08:51:19 +0100 Subject: [PATCH] #630 only set status code if response hasnt started, otherwise exception (#631) --- .../DependencyInjection/OcelotBuilder.cs | 26 +++++++++---------- src/Ocelot/Responder/HttpContextResponder.cs | 12 +++++++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 77f29c5a..94ca903c 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -55,16 +55,16 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); @@ -92,8 +92,8 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); - Services.AddSingleton(); - Services.AddSingleton(); + Services.TryAddSingleton(); + Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); @@ -114,7 +114,7 @@ namespace Ocelot.DependencyInjection Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); - Services.AddSingleton(); + Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); Services.TryAddSingleton(); diff --git a/src/Ocelot/Responder/HttpContextResponder.cs b/src/Ocelot/Responder/HttpContextResponder.cs index 1b20992d..0e964c7a 100644 --- a/src/Ocelot/Responder/HttpContextResponder.cs +++ b/src/Ocelot/Responder/HttpContextResponder.cs @@ -44,7 +44,7 @@ namespace Ocelot.Responder AddHeaderIfDoesntExist(context, new Header("Content-Length", new []{ response.Content.Headers.ContentLength.ToString() }) ); } - context.Response.StatusCode = (int)response.StatusCode; + SetStatusCode(context, (int)response.StatusCode); context.Response.HttpContext.Features.Get().ReasonPhrase = response.ReasonPhrase; @@ -59,7 +59,15 @@ namespace Ocelot.Responder public void SetErrorResponseOnContext(HttpContext context, int statusCode) { - context.Response.StatusCode = statusCode; + SetStatusCode(context, statusCode); + } + + private void SetStatusCode(HttpContext context, int statusCode) + { + if (!context.Response.HasStarted) + { + context.Response.StatusCode = statusCode; + } } private static void AddHeaderIfDoesntExist(HttpContext context, Header httpResponseHeader)