NinjectControllerFactory FileLoadException Error

1.2k views Asked by At

I tried create just example porject and I want use Ninject.I create NinjectControllerFactory.cs like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using System.Web.Routing;
using Moq;
using System.Linq;
using SportStore.Domain.Entities;
using SportStore.Domain.Abstract;
using System.Collections.Generic;
using SportStore.Domain.Concrete;

namespace SportStore.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
                ? null
                : (IController)ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {   
            ninjectKernel.Bind<IProductsRepository>().To<EFProductRepository>();
        }

    }
}

and I create IProductRepository.cs like this

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportStore.Domain.Entities;

namespace SportStore.Domain.Abstract
{
    public interface IProductsRepository
    {
        IQueryable<Product> Products { get; }
    }
}

And the last one EFProductRepository like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportStore.Domain.Abstract;
using SportStore.Domain.Entities;

namespace SportStore.Domain.Concrete
{
    public class EFProductRepository : IProductsRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Product> Products
        {
            get { return context.Products; }
        }
    }
}

So when I run project I get this error

Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source Error:

Line 11: public class EFProductRepository : IProductsRepository Line 12: { Line 13: private EFDbContext context = new EFDbContext(); Line 14: Line 15: public IQueryable Products

Source File: c:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.Domain\Concrete\EFProductRepository.cs Line: 13

Assembly Load Trace: The following information can be helpful to determine why the assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' could not be loaded.

=== Pre-bind state information === LOG: User = Ayyildiz-PC\Ayyildiz LOG: DisplayName = EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (Fully-specified) LOG: Appbase = file:///C:/Users/Ayyildiz/Documents/Visual Studio 2012/Projects/SportStore/SportStore.WebUI/ LOG: Initial PrivatePath = C:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.WebUI\bin Calling assembly : SportStore.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.WebUI\web.config LOG: Using host configuration file: C:\Users\Ayyildiz\Documents\IISExpress\config\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 LOG: Attempting download of new URL file:///C:/Users/Ayyildiz/AppData/Local/Temp/Temporary ASP.NET Files/root/85c19813/4c228712/EntityFramework.DLL. LOG: Attempting download of new URL file:///C:/Users/Ayyildiz/AppData/Local/Temp/Temporary ASP.NET Files/root/85c19813/4c228712/EntityFramework/EntityFramework.DLL. LOG: Attempting download of new URL file:///C:/Users/Ayyildiz/Documents/Visual Studio 2012/Projects/SportStore/SportStore.WebUI/bin/EntityFramework.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Stack Trace:

[FileLoadException: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)] SportStore.Domain.Concrete.EFProductRepository..ctor() in c:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.Domain\Concrete\EFProductRepository.cs:13 DynamicInjectorc6bf6cff03594d109af1167280b346c5(Object[] ) +70
Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:97 Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:157
Ninject.<>c_DisplayClass10.b_c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:386
System.Linq.WhereSelectEnumerableIterator2.MoveNext() +145
System.Linq.Enumerable.SingleOrDefault(IEnumerable
1 source) +4077101 Ninject.Planning.Targets.Target1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:197 Ninject.Planning.Targets.Target1.ResolveWithin(IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:165 Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:114 Ninject.Activation.Providers.<>c_DisplayClass4.b_2(ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:96 System.Linq.WhereSelectArrayIterator2.MoveNext() +66
System.Linq.Buffer
1..ctor(IEnumerable1 source) +216
System.Linq.Enumerable.ToArray(IEnumerable
1 source) +77
Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:96 Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:157
Ninject.<>c_DisplayClass10.b_c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:386
System.Linq.WhereSelectEnumerableIterator2.MoveNext() +145
System.Linq.Enumerable.Single(IEnumerable
1 source) +191
Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:151 SportStore.WebUI.Infrastructure.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in c:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.WebUI\Infrastructure\NinjectControllerFactory.cs:29 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +226
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +177
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

I am using EntityFramework v6 and I am using MSSQL server 2008 r2. This error in NinjectControllerFactory.cs line this

return controllerType == null
                ? null
                : (IController)ninjectKernel.Get(controllerType);

Anyone can help me please ? Thanks

2

There are 2 answers

0
Sothy Chan On

I ran into the same issue and here's what I did to fix mine. Maybe, this will help you fix yours.

The problem appears to be a versioning number issue of EntityFramework in your references (SportsStore.Domain and SportsStore.WebUI) and web.config in SportsStore.WebUI.

Check each EntityFramework reference and change each version to 6.0.0.0. In SportsStore.WebUI, you won't be able to change it since it takes the entry from your web.config. As a result, just remove the EntityFramework reference from SportsStore.WebUI. Compile, and it should work.

I hope this helps.

0
Tej On

just remove references from all places and also check on different config file web.config, packges.config from web ui and web.config, packges.confg from Domain

comment on all placed then add new reference.