Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris Wallace 3 posts 23 karma points
    Jan 20, 2014 @ 13:54
    Chris Wallace
    0

    Using Ninject Intercept Attributes with Custom Controllers

    I've been trying to work this out but think I'm missing something. I'm trying to get Ninject Intercept Attributes working in a custom RenderMvcController but for some reason my Interceptor class is not firing (everything builds as it should). I've applied the required Attribute to the relevant method I'm trying to intercept and also made the method virtual but no luck.

    Has anyone had any experience using Ninject Interceptors before? (within Umbraco).

    Here's my action ('query' being resolved by Ninject using constructor injection, got that working at least!)

    public ActionResult LandingMvc()

            {

                var vm = query.Get();

                return CurrentTemplate(vm);

            }

    And here's the query class which has the Interceptor attribute on its Get() method

    public class LandingPageViewModelQuery : ILandingPageViewModelQuery

        {

            private ISiteRepository propertyRepo;

            public LandingPageViewModelQuery(ISiteRepository pr)

            {

                propertyRepo = pr;

            }

            [SiteChrome]

            public virtual ILandingPageViewModel Get()

            {

                var vm = new LandingPageViewModel();

                return vm;

            }

        }

    Finally here's the Intercept Attribute which I would have expected to have fired.

    [AttributeUsage(AttributeTargets.Method, Inherited = true)]

        public class SiteChrome : InterceptAttribute

        {

            public override IInterceptor CreateInterceptor(IProxyRequest request)

            {

                return request.Kernel.Get<SiteChromeInterceptor>();

            }

        }

     

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jan 20, 2014 @ 17:32
    Ismail Mayat
    0

    Chris,

    Do you need to add anything in the AppStart? So when using ninject you have to create and register module in appstart here is what my app start looks like for an mvc project i have where i use ninject may be something similar is needed to register intercept attributes? Just a theory btw :-}

    [assembly: WebActivator.PreApplicationStartMethod(typeof(TEF.Web.App_Start.NinjectWebCommon), "Start")]
    

    [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(TEF.Web.App_Start.NinjectWebCommon), "Stop")]

    namespace TEF.Web.App_Start { using System; using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    
    using Ninject;
    using Ninject.Web.Common;
    
    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();
    
        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
    
            InitAutoMapper();
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
    
        private static void InitAutoMapper()
        {
            Mapper.CreateMap<SearchFormModel, Search>();
        }
    
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
    
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
    
            RegisterServices(kernel);
            return kernel;
        }
    
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ISearchService>().To<SearchService>().InSingletonScope();
        }        
    }
    

    }

  • Chris Wallace 3 posts 23 karma points
    Feb 15, 2014 @ 16:51
    Chris Wallace
    0

    Thanks Ismail, turned out I needed an extra reference to Ninject.Extensions.Interception.DynamicProxy, once that was added the interceptor attributes started working!

Please Sign in or register to post replies

Write your reply to:

Draft