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)