在Web API和OWIN中使用Simple Injector
我遇到了与此处描述的相同的问题,我的设置几乎与此实际上基于本指南相同 。 当我在我的控制器中访问一个方法时,我得到了这个
尝试创建“TestController”类型的控制器时发生错误。 确保控制器具有无参数的公共构造函数。
这是堆栈跟踪
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator .Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)rn at System.Web.Http.Controllers.HttpControllerDescriptor .CreateController(HttpRequestMessage request)rn at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
这是内部exception的堆栈跟踪
at System.Linq.Expressions.Expression.New(Type type)rn at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)rn at System.Web.Http.Dispatcher.DefaultHttpControllerActivator .GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)rn at System.Web.Http.Dispatcher.DefaultHttpControllerActivator .Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
这是我的控制器的样子
public class TestController : ApiController { private readonly ITestRepo _repo; public TestController(ITestRepo repo) { _repo = repo; } public IEnumerable Get() { return new string[] { "value1", "value2" }; } public string Get(int id) { return _repo.GetId(id); } }
这就是我设置Simple Injector的方法
public class Startup { public void Configuration(IAppBuilder app) { // Create the container as usual. var container = new Container(); // Register your types, for instance using the RegisterWebApiRequest // extension from the integration package: container.RegisterWebApiRequest(); container.RegisterWebApiControllers(GlobalConfiguration.Configuration); container.Verify(); GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); // ConfigureOAuth(app, container); var config = new HttpConfiguration(); WebApiConfig.Register(config); app.UseWebApi(config); } }
我有相同的问题但使用UnityDependencyResolver。 但我认为它也适用于SimpleInjectorWebApiDependencyResolver。 尝试像这样注册您的解析器(作为HttpConfiguration的属性):
上述就是C#学习教程:在Web API和OWIN中使用Simple Injector分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public void Configuration(IAppBuilder app) { var container = GetContainer(); // Initialise container HttpConfiguration config = new HttpConfiguration { DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); }; WebApiConfig.Register(config); app.UseWebApi(config); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1026423.html