asp.net-mvc – 在web api 2中使用autofac的无参数构造函数错误
嘿伙计我正在使用Autofac作为IOC,这是我的结构:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AdTudent.Repo { public interface IRepository { IAdvertisementRepository Advertisements { get; set; } IProfileRepository Profiles { get; set; } } } 我的存储库类是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AdTudent.Repo { public class Repositories : IRepository { public Repositories(IAdvertisementRepository advertisementRepository,IProfileRepository profileRepository) { Advertisements = advertisementRepository; Profiles = profileRepository; } public IAdvertisementRepository Advertisements { get; set; } public IProfileRepository Profiles { get; set; } } } 我的启动课是: public partial class Startup { public void Configuration(IAppBuilder app) { var builder = new ContainerBuilder(); builder.RegisterType<Repositories>().As<IRepository>(); builder.Register<IGraphClient>(context => { var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data")); graphClient.Connect(); return graphClient; }).SingleInstance(); // STANDARD WEB API SETUP: // Get your HttpConfiguration. In OWIN,you'll create one // rather than using GlobalConfiguration. var config = new HttpConfiguration(); // Register your Web API controllers. //builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // Run other optional steps,like registering filters,// per-controller-type services,etc.,then set the dependency resolver // to be Autofac. var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // OWIN WEB API SETUP: // Register the Autofac middleware FIRST,then the Autofac Web API middleware,// and finally the standard Web API middleware. app.UseAutofacMiddleware(container); app.UseAutofacWebApi(config); app.UseWebApi(config); ConfigureAuth(app); } } 这是我的帐户管理员: public class AccountController : ApiController { private IRepository _repository; private const string LocalLoginProvider = "Local"; private ApplicationUserManager _userManager; private IGraphClient _graphClient; public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; } public AccountController(IRepository repository,IGraphClient graphClient,ISecureDataFormat<AuthenticationTicket> accessTokenFormat) { AccessTokenFormat = accessTokenFormat; _repository = repository; _graphClient = graphClient; } } 但我总是这个问题
我在互联网上搜索,但我找不到任何可以帮助任何人指导我的东西? 解决方法
按
WebApi OWIN documentation:
看起来你有几个问题: >使用OWIN时需要重新启动HttpConfiguration. 根据文档,您还需要Autofac WebApi OWIN package. public class Startup { public void Configuration(IAppBuilder app) { var builder = new ContainerBuilder(); // STANDARD WEB API SETUP: // Get your HttpConfiguration. In OWIN,you'll create one // rather than using GlobalConfiguration. var config = new HttpConfiguration(); // Register your Web API controllers. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // Run other optional steps,then set the dependency resolver // to be Autofac. var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // OWIN WEB API SETUP: // Register the Autofac middleware FIRST,// and finally the standard Web API middleware. app.UseAutofacMiddleware(container); app.UseAutofacWebApi(config); app.UseWebApi(config); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 麻烦与ajax POST调用WCF服务
- 为什么在手动刷新响应时ASP.NET将使用Transfer-Encoding头替
- asp.net – DropDownList的EditorTemplate
- ASP.NET学习CORE中使用Cookie身份认证方法
- asp-classic – 哪里可以存储经典ASP的连接字符串?
- SimpleMembership与ASP.NET MVC 4中的自定义数据库模式
- asp.net-mvc-4 – 返回Web API中的自定义错误对象
- 在不加载实体的情况下更新ASP.NET MVC EF4中的相关实体的最
- asp.net – 包装StaticFileMiddleware以重定向404错误
- IIS7上的ASP.NET应用程序 – 在iisreset启动后非常慢