Multiple Web API 2 applications in one solution, both using Ninject

I have two Web API 2 projects, ProjectAand ProjectBinside one solution.

These are independent projects. I want to use ninject in both, but these lines cause problems.

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProjectA.App_Start.NinjectWebCommon), "Start")]

and

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProjectB.App_Start.NinjectWebCommon), "Start")] 

Even if I only have ProjectA as the launch, both NinjectWebCommon classes will execute. And whatever the last one (usually ProjectB), it overwrites what was done first.

An exception:

When called bootstrapper.Initialize(CreateKernel)in ProjectB, I get -

"Sequence contains no elements"

How can I create two projects, one of which or both, can be run projects in one solution using ninject?

The code

This is mine NinjectWebCommon, it is almost identical for ProjectA and ProjectB, only registered services are different.

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProjectA.WebApi.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(ProjectA.WebApi.App_Start.NinjectWebCommon), "Stop")]

namespace ProjectA.WebApi.App_Start
{

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <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();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);

                GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            var mapper = AutoMapperConfig.SetupMappings();
            kernel.Bind<IMapper>().ToConstant(mapper);

            kernel.Bind<IFoo>().To<Foo>();
        }        
    }
}
+4
2

, -, , Visual Studio 2015 DLL . 100%, .

-API, ninject , , , .

, , ! DLL , .

, Visual Studio 2015 ( 2) .

c:\Users\bryan\AppData\Local\Temp\Temporary ASP.NET Files\vs

c:\Users\bryan\AppData\Local\Temp\Temporary ASP.NET Files\root

+1

Ninject, , , . . , , , . Ninject . nuget / ?

0

All Articles