依赖注入 – ASP.NET Web API是否有HttpControllerBuilder?
发布时间:2020-12-13 20:24:36 所属栏目:百科 来源:网络整理
导读:如果我想为ASP.NET MVC控制器实现依赖注入,我可以使用IControllerFactory接口创建一个工厂类,然后在Global.asax.cs中注册,如: ControllerBuilder.Current.SetControllerFactory(controllerFactory) (参考:http://www.dotnetcurry.com/ShowArticle.aspx?ID=
如果我想为ASP.NET MVC控制器实现依赖注入,我可以使用IControllerFactory接口创建一个工厂类,然后在Global.asax.cs中注册,如:
ControllerBuilder.Current.SetControllerFactory(controllerFactory) (参考:http://www.dotnetcurry.com/ShowArticle.aspx?ID=786) 现在我的问题是: 如何为IHttpControllerActivator派生类设置工厂? 我们有ASP.NET MVC 4.0中的东西,像: HttpControllerBuilder.Current.SetApiControllerActivator(httpControllerActivator) ? 更新: 我想添加我当前的代码,可能有助于了解情况: 我定制的HttpControllerActivator: public class MyCustomApiActivator : DefaultHttpControllerActivator //or IHttpControllerActivator ? { private readonly Dictionary<string,Func<HttpRequestMessage,IHttpController>> _apiMap; public MyCustomApiActivator(IMyRepository repository) { if (repository == null) { throw new ArgumentException("repository"); } _apiMap = new Dictionary<string,IHttpController>>(); controllerMap["Home"] = context => new HomeController(); _apiMap["MyCustomApi"] = context => new MyCustomApiController(repository); } public IHttpController Create(HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType) { if(_apiMap.ContainsKey(controllerType.Name)) return _apiMap[controllerType.Name](request); return null; } } 我的组成根: public class CompositionRoot { private readonly IHttpControllerActivator _apiControllerActivator; public CompositionRoot() { _apiControllerActivator = CompositionRoot.CreateHttpControllerActivator(); } public IHttpControllerActivator ApiControllerActivator { get { return _apiControllerActivator; } } private static IHttpControllerActivator CreateHttpControllerActivator() { string defaultRepositoryTypeName = ConfigurationManager.AppSettings["DefaultRepositoryTypeName"]; var defaultRepositoryType = Type.GetType(defaultRepositoryTypeName,true); var defaultRepository = (IMyRepository)Activator.CreateInstance(defaultRepositoryType); var apiController = new MyCustomApiActivator(defaultRepository); return apiController; } } 最后这是在我的Global.asax.cs里面,我需要一个技巧: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var root = new CompositionRoot(); //The following line raise this compile-time error: // cannot convert from 'System.Web.Http.Dispatcher.IHttpControllerActivator' // to 'System.Web.Mvc.IControllerFactory' ControllerBuilder.Current.SetControllerFactory(root.ApiControllerActivator); }
Dependency Injection在.NET中的作者Mark Seemann在DI上与ASP.NET WebAPI有一个简短的系列.
他将组合根放在IHttpControllerActivator中 > Dependency Injection and Lifetime Management with ASP.NET Web API 也许这有帮助. 更新 GlobalConfiguration.Configuration.Services.Replace( typeof(IHttpControllerActivator),new PoorMansCompositionRoot()); 全局注册您的自定义HttpControllerActivator. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |