3

I'm trying to inject this user service via Unity (mvc5) in an actionfilter but it is null. How can I implement this?

public class TestFilter : ActionFilterAttribute
{
    // this is always null
    [Dependency]
    public IUserService UserService { get; set; }

    // other members
}
3
  • Have you registered UnityFilterAttributeFilterProvider on the FilterProviders ? Commented Aug 3, 2015 at 13:31
  • Hi Sam no I have not, how can I do this please? Commented Aug 3, 2015 at 13:35
  • 1
    Here is an explanation and a suggestion to a similar question that may help you. Commented Aug 3, 2015 at 13:37

1 Answer 1

2

You must register UnityFilterAttributeFilterProvider as a FilterProvider first.

Modify the App_Start > UnityMvcActivator's Start method like this:

public static void Start()
{
    var container = UnityConfig.GetConfiguredContainer();

    FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
    FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));

    Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}

If you could not find the method. You probably installed wrong or out of date package. consider installing Install-Package Unity.Mvc on the package manager console.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.