NuGet更新后出现的System.Web.Http的FileLoadException问题

3
我刚刚从NuGet更新了我的安装项目,并在运行时得到了以下未处理的异常:
“无法加载文件或程序集'System.Web.Http, Version=4.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35'或其任何依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 的异常: 0x80131040)”
异常是从NinjectWebCommon抛出的。bootstraper.Initialize(CreateKernel);这行代码有问题。
 public static void Start()
 {
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
     DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
     bootstrapper.Initialize(CreateKernel);
 }

我猜测Ninject可能依赖于旧版本的System.Web.Http,但如果不必要,我不想回滚到旧版本。
有人遇到过这个问题并解决了吗?
编辑:
似乎有问题的代码在bootstrapper.Initialize()方法中:
private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
    kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

    RegisterServices(kernel);

    // Set Web API Resolver (using WebApiContrib.Ioc.Ninject)
    GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);

    return kernel;
}

当我注释掉现有注释下的代码行后,该项目运行良好。需要找出如何在没有WebApiContrub.IoC.Ninject的情况下让WebApi正常工作...


你能解决这个问题吗?在尝试了建议的解决方案后,我不再遇到异常了,但是依赖注入还是无法正常工作... - developer82
1个回答

9
听起来你在使用Web API2,但没有进行程序集绑定重定向。
<dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>

1
上述代码添加到我的 web.config 后,移除了异常但导致 WebApi 无法工作。 - Ryan Spears
这意味着您的应用程序中混合了汇编版本。 - Remo Gloor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接