WebApi/AutoFac和iDependencyResolver错误。 您所在的位置:网站首页 random中文版 WebApi/AutoFac和iDependencyResolver错误。

WebApi/AutoFac和iDependencyResolver错误。

#WebApi/AutoFac和iDependencyResolver错误。| 来源: 网络整理| 查看: 265

I have got a DDD-layered application, so it contains both an Asp.Net MVC 4 project and an WebApi project (2 projects in the presentation layer).

我有一个ddd分层的应用程序,所以它包含了一个Asp。Net MVC 4项目和WebApi项目(表示层中的两个项目)。

Adding Autofac with Asp.Net MVC 4 = ok Adding Autofac with Asp.Net WebApi = not ok

添加Autofac Asp。Net MVC 4 =可以使用Asp添加Autofac。Net WebApi =不可以。

This is my global.asax.cs from my asp.net web api

这是我的global.asax。从我的asp.net web api。

var cOnfiguration= GlobalConfiguration.Configuration; var builder = new ContainerBuilder(); // Configure the container with the integration implementations. // builder.ConfigureWebApi(configuration); builder.RegisterWebApiFilterProvider(configuration); // Register API controllers using assembly scanning. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerLifetimeScope() .OnRelease(x => { ((IUnitOfWork)x).Commit(); }); builder.RegisterType(typeof(DatabaseFactory)).As(typeof(IDatabaseFactory)).InstancePerLifetimeScope().AsImplementedInterfaces(); builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces(); builder.RegisterAssemblyTypes(typeof(SecurityService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces(); var cOntainer= builder.Build(); // Set the dependency resolver implementation. var resolver = new AutofacWebApiDependencyResolver(container); configuration.ServiceResolver.SetResolver(resolver);

The error i'm seeing is : Could not load type 'System.Web.Http.Dependencies.IDependencyResolver' from assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Tried rewriting the part of code for 3 times, but it just doesn't seem to work.

我所看到的错误是:不能加载类型的System.Web.Http.Dependencies。IDependencyResolver包含从汇编。Http、Version = 4.0.0.0文化=中立,都必须31 bf3856ad364e35”。试着重写代码的部分3次,但它似乎不工作。

Already found some links to some blogs, but mostly they contain a reference to builder.ConfigureWebApi(configuration); and that doesn't exist in the newer version.

已经在一些博客中找到了一些链接,但是它们主要包含了builder.ConfigureWebApi(配置)的引用;这在新版本中并不存在。

Any thoughts on how I could solve this?

有什么想法可以解决这个问题吗?

2 个解决方案 #1

1  

In addition to your resolver, you are going to need a controller factory, I don't have expereince creating a controller factory for autofac (my experience is with windsor), but it should be straight forward. You want to use configuration.DependancyResolver like so:

除了您的解析器之外,您还需要一个控制器工厂,我没有为autofac创建一个控制器工厂的经验(我的经验是温莎),但是它应该是直接的。你想要使用配置。DependancyResolver一样:

configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Once you have created your factory you can assign it:

一旦你创建了你的工厂,你可以分配它:

ControllerBuilder.Current.SetControllerFactory(yourfactory); #2

0  

It looks like you are using a pre-release version of Web API because in the RTW version the dependency resolver is set through a property:

看起来您使用的是Web API的预发布版本,因为在RTW版本中,依赖解析器是通过属性设置的:

var resolver = new AutofacWebApiDependencyResolver(container); configuration.DependencyResolver = resolver;

Make sure you are the RTW version of Web API and the latest NuGet package for the Autofac integration:

确保您是RTW版本的Web API和最新的Autofac集成的NuGet包:

Install-Package Autofac.WebApi

安装包Autofac.WebApi



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有