Mix WebAPI and MVC

Apr 9, 2014 at 5:32 PM
Hi everybody,

I´m currently using SimpleInjector in my project with no problems. Below is my SimpleInjectorInitializar.cs, in App_Start folder
public static void Initialize()
        {
            var container = new Container();

            InitializeContainer(container);

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.RegisterMvcAttributeFilterProvider();
            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
        }

        private static void InitializeContainer(Container container)
        {
            container.RegisterOpenGeneric(typeof(IUnitOfWork<>), typeof(UnitOfWork<>), new WebRequestLifestyle());

            container.Register<IContratanteRepository, ContratanteRepository>();
            container.Register<IContratanteService, ContratanteService>();
       }
Since the configuration for WebAPI (http://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration&referringTitle=Integration%20Guide) is a bit different, how I can mix both configurations to use WebAPI and standard MVC in a unique initializer, like above?
Coordinator
Apr 9, 2014 at 6:46 PM
Edited Apr 11, 2014 at 10:01 AM
You can install the Web API integration Quick Start from NuGet. This shows you what code is needed for Web API. What you is merge the two initialization methods into one, where you use one single Container instance. But to be honest, I think you'd be better off giving each technology its own project within the same solution.
Marked as answer by dot_NET_Junkie on 4/18/2014 at 4:01 AM
Apr 9, 2014 at 7:20 PM
Thanks for the Reply,

I´ll try the integration Quick start.

And thanks for the advice for separate projects in that scenario. Really makes sense.

Any doubt I'll be back here