我尝试只创建示例项目,我想使用 Ninject。我像这样创建 NinjectControllerFactory.cs
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>();
}
}
}
我像这样创建 IProductRepository.cs
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; }
}
}
最后一个 EFProductRepository 像这样
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; }
}
}
}
所以当我运行项目时,我得到了这个错误
无法加载文件或程序集 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.IO.FileLoadException:无法加载文件或程序集“EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)
源错误:
第 11 行:公共类 EFProductRepository:IProductsRepository 第 12 行:{ 第 13 行:私有 EFDbContext context = new EFDbContext(); 第 14 行:第 15 行:公共 IQueryable 产品
源文件:c:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.Domain\Concrete\EFProductRepository.cs 行:13
程序集加载跟踪:以下信息有助于确定为什么无法加载程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”。
=== 预绑定状态信息 === LOG: User = Ayyildiz-PC\Ayyildiz LOG: DisplayName = EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (完全指定) 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 调用程序集:SportStore.Domain,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null。=== LOG:此绑定在默认加载上下文中开始。日志:使用应用程序配置文件:C:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.WebUI\web.config 日志:使用主机配置文件:C:\Users\Ayyildiz\Documents\IISExpress\config \aspnet.config 日志:使用 C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config 中的机器配置文件。日志:后策略参考:EntityFramework,版本=6.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089 日志:尝试下载新 URL 文件:///C:/Users/Ayyildiz/AppData/Local/Temp/Temporary ASP。 NET 文件/root/85c19813/4c228712/EntityFramework.DLL。日志:正在尝试下载新的 URL 文件:///C:/Users/Ayyildiz/AppData/Local/Temp/Temporary ASP.NET Files/root/85c19813/4c228712/EntityFramework/EntityFramework.DLL。日志:尝试下载新的 URL 文件:///C:/Users/Ayyildiz/Documents/Visual Studio 2012/Projects/SportStore/SportStore.WebUI/bin/EntityFramework.DLL。警告:比较程序集名称导致不匹配:主要版本错误:无法完成程序集设置(hr = 0x80131040)。
堆栈跟踪:
[FileLoadException:无法加载文件或程序集“EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT 例外:0x80131040)] SportStore.Domain.Concrete.EFProductRepository..ctor() 在 c:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.Domain\Concrete\EFProductRepository.cs:13 DynamicInjectorc6bf6cff03594d109af1167280b346c (Object[]) +70
Ninject.Activation.Providers.StandardProvider.Create(IContext context) 在 c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:97 Ninject.Activation.Context.Resolve () 在 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.WhereSelectEnumerableIterator 1 source) +4077101 Ninject.Planning.Targets.Target 1 .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.WhereSelectArrayIterator 1..ctor(IEnumerable2.MoveNext() +145
System.Linq.Enumerable.SingleOrDefault(IEnumerable1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:197 Ninject.Planning.Targets.Target
2.MoveNext() +66
System.Linq.Buffer1 source) +216
1 来源)+77
System.Linq.Enumerable.ToArray(IEnumerable
Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:96 Ninject.Activation.Context.Resolve()在 c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:157
Ninject.<>c_ DisplayClass10.b _c(IBinding 绑定) 在 c:\Projects\Ninject\ninject\src\Ninject\KernelBase。 cs:386
System.Linq.WhereSelectEnumerableIterator 1 源)+1912.MoveNext() +145
System.Linq.Enumerable.Single(IEnumerable
C:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:151 SportStore.WebUI.Infrastructure.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext , 在 c:\Users\Ayyildiz\Documents\Visual Studio 2012\Projects\SportStore\SportStore.WebUI\Infrastructure\NinjectControllerFactory.cs:29 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) 中键入 controllerType) + 226
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&控制器,IControllerFactory&工厂)+326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+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版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.17929
我正在使用 EntityFramework v6,我正在使用 MSSQL server 2008 r2。NinjectControllerFactory.cs 中的此错误行
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
任何人都可以帮助我吗?谢谢