This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Null ref exception when using IUrlHelper in a filter #631
Copy link
Copy link
Closed
Description
I was trying to redirect to a different route in my action filter and I am using RedirectToRouteResult. This expects to supply a IUrlHelper and so I placed this to be injectable on my action filter.
I registered my filter to be a singleton and noticed following Null Ref exception...yeah, I realized that registering as singleton is incorrect here as UrlHelper is created on a per-request basis...
Can we give a more meaningful exception here?...
services.AddSingleton<FooActionFilter>();
[ServiceFilter(typeof(FooActionFilter))]
public class FooController : Controller
{
public string Get()
{
return "Bar";
}
public string Get(int id)
{
return "Bar:" + id;
}
}
public class FooActionFilter : IAsyncActionFilter
{
private IUrlHelper urlHelper = null;
public FooActionFilter(IUrlHelper helper)
{
urlHelper = helper;
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
context.Result = new RedirectToRouteResult(urlHelper, new { controller = "Foo", id = 10 });
await next();
}
}
Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNet.Mvc.UrlHelper..ctor(IContextAccessor`1 contextAccessor, IActionSelector actionSelector)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Microsoft.Framework.DependencyInjection.ActivatorUtilities.<>c__DisplayClass0.<CreateFactory>b__4(IServiceProvider services)
at Microsoft.Framework.DependencyInjection.ServiceLookup.Service.Create(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProvider.ResolveService(IService service)
at Microsoft.Framework.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Framework.DependencyInjection.ActivatorUtilities.<>c__DisplayClass0.<CreateFactory>b__4(IServiceProvider services)
at Microsoft.Framework.DependencyInjection.ServiceLookup.Service.Create(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProvider.ResolveService(IService service)
at Microsoft.Framework.DependencyInjection.ServiceProvider.ResolveService(IService service)
at Microsoft.Framework.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.AspNet.Mvc.ServiceFilterAttribute.CreateInstance(IServiceProvider serviceProvider)
at Microsoft.AspNet.Mvc.Filters.DefaultFilterProvider.ProvideFilter(FilterProviderContext context, FilterItem filterItem)
at Microsoft.AspNet.Mvc.Filters.DefaultFilterProvider.Invoke(FilterProviderContext context, Action callNext)
at Microsoft.Framework.DependencyInjection.NestedProviders.NestedProviderManager`1.CallNext.CallNextProvider()
at Microsoft.Framework.DependencyInjection.NestedProviders.NestedProviderManager`1.Invoke(T context)
at Microsoft.AspNet.Mvc.ReflectedActionInvoker.GetFilters()
at Microsoft.AspNet.Mvc.ReflectedActionInvoker.<InvokeActionAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Mvc.MvcRouteHandler.<RouteAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Routing.Template.TemplateRoute.<RouteAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Routing.RouteCollection.<RouteAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Builder.RouterMiddleware.<Invoke>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Security.Infrastructure.AuthenticationMiddleware`1.<Invoke>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.RequestContainer.ContainerMiddleware.<Invoke>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.<ExecuteWithFilter>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Loader.IIS.KlrHttpApplication.<ProcessRequestAsyncImpl>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Loader.IIS.HttpApplicationBase.<InvokeProcessRequestAsyncImpl>d__1.MoveNext()
Metadata
Metadata
Assignees
Labels
No labels