From 39b8d204fd0fac894fdfc883a84a5010c37c6371 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Mon, 23 Feb 2015 11:10:03 -0800 Subject: [PATCH] React to aspnet/HttpAbstractions#160 - Implemented OnResponseCompleted --- .../FeatureContext.cs | 5 +++ .../RequestProcessing/Response.cs | 38 +++++++++++++++++++ .../Resources.Designer.cs | 8 ++++ src/Microsoft.Net.Http.Server/Resources.resx | 3 ++ 4 files changed, 54 insertions(+) diff --git a/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs b/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs index 48d8c23ffb..b2b0906caa 100644 --- a/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs +++ b/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs @@ -360,6 +360,11 @@ namespace Microsoft.AspNet.Server.WebListener Response.OnSendingHeaders(callback, state); } + void IHttpResponseFeature.OnResponseCompleted(Action callback, object state) + { + Response.OnResponseCompleted(callback, state); + } + string IHttpResponseFeature.ReasonPhrase { get { return Response.ReasonPhrase; } diff --git a/src/Microsoft.Net.Http.Server/RequestProcessing/Response.cs b/src/Microsoft.Net.Http.Server/RequestProcessing/Response.cs index 0ab02518a4..be5bf38abd 100644 --- a/src/Microsoft.Net.Http.Server/RequestProcessing/Response.cs +++ b/src/Microsoft.Net.Http.Server/RequestProcessing/Response.cs @@ -48,6 +48,7 @@ namespace Microsoft.Net.Http.Server private BoundaryType _boundaryType; private UnsafeNclNativeMethods.HttpApi.HTTP_RESPONSE_V2 _nativeResponse; private IList, object>> _onSendingHeadersActions; + private IList, object>> _onResponseCompletedActions; private RequestContext _requestContext; @@ -63,6 +64,7 @@ namespace Microsoft.Net.Http.Server _nativeResponse.Response_V1.Version.MinorVersion = 1; _responseState = ResponseState.Created; _onSendingHeadersActions = new List, object>>(); + _onResponseCompletedActions = new List, object>>(); } private enum ResponseState @@ -284,6 +286,7 @@ namespace Microsoft.Net.Http.Server { return; } + NotifyOnResponseCompleted(); // TODO: Verbose log EnsureResponseStream(); _nativeStream.Dispose(); @@ -829,6 +832,17 @@ namespace Microsoft.Net.Http.Server actions.Add(new Tuple, object>(callback, state)); } + public void OnResponseCompleted(Action callback, object state) + { + var actions = _onResponseCompletedActions; + if (actions == null) + { + throw new InvalidOperationException("Response already completed"); + } + + actions.Add(new Tuple, object>(callback, state)); + } + private void NotifyOnSendingHeaders() { var actions = Interlocked.Exchange(ref _onSendingHeadersActions, null); @@ -845,6 +859,30 @@ namespace Microsoft.Net.Http.Server } } + private void NotifyOnResponseCompleted() + { + var actions = Interlocked.Exchange(ref _onResponseCompletedActions, null); + if (actions == null) + { + // Something threw the first time, do not try again. + return; + } + + foreach (var actionPair in actions) + { + try + { + actionPair.Item1(actionPair.Item2); + } + catch (Exception ex) + { + RequestContext.Logger.LogWarning( + String.Format(Resources.Warning_ExceptionInOnResponseCompletedAction, nameof(OnResponseCompleted)), + ex); + } + } + } + private class SendResponseLogContext : ReflectionBasedLogValues { private readonly Response _response; diff --git a/src/Microsoft.Net.Http.Server/Resources.Designer.cs b/src/Microsoft.Net.Http.Server/Resources.Designer.cs index 0d5dffd0c3..cee3ec2600 100644 --- a/src/Microsoft.Net.Http.Server/Resources.Designer.cs +++ b/src/Microsoft.Net.Http.Server/Resources.Designer.cs @@ -166,5 +166,13 @@ namespace Microsoft.Net.Http.Server { return ResourceManager.GetString("Exception_WrongIAsyncResult", resourceCulture); } } + + /// + /// An exception occured while running an action registered with {0}. + /// + internal static string Warning_ExceptionInOnResponseCompletedAction + { + get { return ResourceManager.GetString("Warning_ExceptionInOnResponseCompletedAction"); } + } } } diff --git a/src/Microsoft.Net.Http.Server/Resources.resx b/src/Microsoft.Net.Http.Server/Resources.resx index 005bafca2f..67b954a934 100644 --- a/src/Microsoft.Net.Http.Server/Resources.resx +++ b/src/Microsoft.Net.Http.Server/Resources.resx @@ -147,4 +147,7 @@ The given IAsyncResult does not match this opperation. + + An exception occured while running an action registered with {0}. + \ No newline at end of file