Skip to content

Commit

Permalink
Merge pull request #44 from alistairjevans/filter-update
Browse files Browse the repository at this point in the history
Update action filter execution process to run using the continuation style
  • Loading branch information
tillig authored Aug 15, 2019
2 parents e279f54 + 1389f22 commit d4506ad
Show file tree
Hide file tree
Showing 22 changed files with 922 additions and 288 deletions.
3 changes: 3 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

This software includes code from the open source Microsoft ASP.NET web stack,
licensed under the Apache 2.0 license. You may obtain a copy of the license at
http://www.apache.org/licenses/LICENSE-2.0
169 changes: 0 additions & 169 deletions src/Autofac.Integration.WebApi/ActionFilterWrapper.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
<CodeAnalysisDictionary Include="..\..\build\CodeAnalysisDictionary.xml">
<Link>CodeAnalysisDictionary.xml</Link>
</CodeAnalysisDictionary>
<Compile Include="ActionFilterWrapper.cs" />
<Compile Include="AutofacActionFilterAdapter.cs" />
<Compile Include="ContinuationActionFilterOverrideWrapper.cs" />
<Compile Include="ContinuationActionFilterWrapper.cs" />
<Compile Include="AuthorizationFilterWrapper.cs" />
<Compile Include="ActionFilterOverrideWrapper.cs" />
<Compile Include="AutofacFilterCategory.cs" />
<Compile Include="AutofacOverrideFilter.cs" />
<Compile Include="AutofacWebApiDependencyResolver.cs" />
Expand All @@ -68,6 +69,7 @@
<Compile Include="FilterPredicateMetadata.cs" />
<Compile Include="HttpConfigurationExtensions.cs" />
<Compile Include="HttpRequestMessageProvider.cs" />
<Compile Include="IAutofacContinuationActionFilter.cs" />
<Compile Include="IAutofacAuthenticationFilter.cs" />
<Compile Include="CurrentRequestHandler.cs" />
<Compile Include="DependencyResolverExtensions.cs" />
Expand Down
140 changes: 140 additions & 0 deletions src/Autofac.Integration.WebApi/AutofacActionFilterAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// This software is part of the Autofac IoC container
// Copyright (c) 2012 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Portions of this file come from the Microsoft ASP.NET web stack, licensed
// under the Apache 2.0 license. You may obtain a copy of the license at
// http://www.apache.org/licenses/LICENSE-2.0

using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace Autofac.Integration.WebApi
{
/// <summary>
/// This is an adapter responsible for wrapping the old style <see cref="IAutofacActionFilter"/>
/// and converting it to a <see cref="IAutofacContinuationActionFilter"/>.
/// </summary>
/// <remarks>
/// The adapter from old -> new is registered in <see cref="RegistrationExtensions.RegisterWebApiFilterProvider"/>.
/// </remarks>
internal class AutofacActionFilterAdapter : IAutofacContinuationActionFilter
{
private readonly IAutofacActionFilter _legacyFilter;

public AutofacActionFilterAdapter(IAutofacActionFilter legacyFilter)
{
_legacyFilter = legacyFilter;
}

public async Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
await _legacyFilter.OnActionExecutingAsync(actionContext, cancellationToken);

if (actionContext.Response != null)
{
return actionContext.Response;
}

return await CallOnActionExecutedAsync(actionContext, cancellationToken, continuation);
}

/// <summary>
/// The content of this method is taken from the ActionFilterAttribute code in the ASP.NET source, since
/// that is basically the reference implementation for invoking an async filter's OnActionExecuted correctly.
/// </summary>
[SuppressMessage("Microsoft.CodeQuality", "CA1068", Justification = "Matching parameter order in original implementtion.")]
private async Task<HttpResponseMessage> CallOnActionExecutedAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
cancellationToken.ThrowIfCancellationRequested();

HttpResponseMessage response = null;
ExceptionDispatchInfo exceptionInfo = null;

try
{
response = await continuation();
}
catch (Exception e)
{
exceptionInfo = ExceptionDispatchInfo.Capture(e);
}

Exception exception;

if (exceptionInfo == null)
{
exception = null;
}
else
{
exception = exceptionInfo.SourceException;
}

HttpActionExecutedContext executedContext = new HttpActionExecutedContext(actionContext, exception)
{
Response = response
};

try
{
await _legacyFilter.OnActionExecutedAsync(executedContext, cancellationToken);
}
catch
{
// Catch is running because OnActionExecuted threw an exception, so we just want to re-throw.
// We also need to reset the response to forget about it since a filter threw an exception.
actionContext.Response = null;
throw;
}

if (executedContext.Response != null)
{
return executedContext.Response;
}

Exception newException = executedContext.Exception;

if (newException != null)
{
if (newException == exception)
{
exceptionInfo.Throw();
}
else
{
throw newException;
}
}

throw new InvalidOperationException();
}
}
}
8 changes: 4 additions & 4 deletions src/Autofac.Integration.WebApi/AutofacWebApiFilterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ private static void ResolveAllScopedFilterOverrides(
ILifetimeScope lifeTimeScope,
HttpActionDescriptor descriptor)
{
ResolveScopedFilter<IAutofacActionFilter, ActionFilterOverrideWrapper>(
filterContext, scope, lifeTimeScope, descriptor, hs => new ActionFilterOverrideWrapper(hs), AutofacFilterCategory.ActionFilterOverride);
ResolveScopedFilter<IAutofacContinuationActionFilter, ContinuationActionFilterOverrideWrapper>(
filterContext, scope, lifeTimeScope, descriptor, hs => new ContinuationActionFilterOverrideWrapper(hs), AutofacFilterCategory.ActionFilterOverride);
ResolveScopedFilter<IAutofacAuthenticationFilter, AuthenticationFilterOverrideWrapper>(
filterContext, scope, lifeTimeScope, descriptor, hs => new AuthenticationFilterOverrideWrapper(hs), AutofacFilterCategory.AuthenticationFilterOverride);
ResolveScopedFilter<IAutofacAuthorizationFilter, AuthorizationFilterOverrideWrapper>(
Expand All @@ -162,8 +162,8 @@ private static void ResolveAllScopedFilterOverrides(

private static void ResolveAllScopedFilters(FilterContext filterContext, FilterScope scope, ILifetimeScope lifeTimeScope, HttpActionDescriptor descriptor)
{
ResolveScopedFilter<IAutofacActionFilter, ActionFilterWrapper>(
filterContext, scope, lifeTimeScope, descriptor, hs => new ActionFilterWrapper(hs), AutofacFilterCategory.ActionFilter);
ResolveScopedFilter<IAutofacContinuationActionFilter, ContinuationActionFilterWrapper>(
filterContext, scope, lifeTimeScope, descriptor, hs => new ContinuationActionFilterWrapper(hs), AutofacFilterCategory.ActionFilter);
ResolveScopedFilter<IAutofacAuthenticationFilter, AuthenticationFilterWrapper>(
filterContext, scope, lifeTimeScope, descriptor, hs => new AuthenticationFilterWrapper(hs), AutofacFilterCategory.AuthenticationFilter);
ResolveScopedFilter<IAutofacAuthorizationFilter, AuthorizationFilterWrapper>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This software is part of the Autofac IoC container
// Copyright (c) 2013 Autofac Contributors
// Copyright (c) 2012 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -32,14 +32,13 @@ namespace Autofac.Integration.WebApi
/// <summary>
/// Resolves a filter override for the specified metadata for each controller request.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
internal sealed class ActionFilterOverrideWrapper : ActionFilterWrapper, IOverrideFilter
internal sealed class ContinuationActionFilterOverrideWrapper : ContinuationActionFilterWrapper, IOverrideFilter
{
/// <summary>
/// Initializes a new instance of the <see cref="ActionFilterOverrideWrapper"/> class.
/// Initializes a new instance of the <see cref="ContinuationActionFilterOverrideWrapper"/> class.
/// </summary>
/// <param name="filterMetadata">The filter metadata.</param>
public ActionFilterOverrideWrapper(HashSet<FilterMetadata> filterMetadata)
public ContinuationActionFilterOverrideWrapper(HashSet<FilterMetadata> filterMetadata)
: base(filterMetadata)
{
}
Expand All @@ -52,4 +51,4 @@ public Type FiltersToOverride
get { return typeof(IActionFilter); }
}
}
}
}
Loading

0 comments on commit d4506ad

Please sign in to comment.