c# – 在Web API和OWIN中使用简单的注射器

c# – 在Web API和OWIN中使用简单的注射器,第1张

概述我遇到与 described here相同的问题,我的设置几乎是 identical to this,实际上是基于 this guide.当我访问我的控制器中的一个方法,我得到这个 An error occurred when trying to create a controller of type ‘TestController’. Make sure that the controller 我遇到与 described here相同的问题,我的设置几乎是 identical to this,实际上是基于 this guide.当我访问我的控制器中的一个方法,我得到这个

An error occurred when trying to create a controller of type
‘TestController’. Make sure that the controller has a parameterless
public constructor.

这是堆栈跟踪

at System.Web.http.dispatcher.DefaulthttpControllerActivator  .Create(httpRequestMessage request,httpControllerDescriptor controllerDescriptor,Type controllerType)\r\n   at System.Web.http.Controllers.httpControllerDescriptor  .CreateController(httpRequestMessage request)\r\n   at System.Web.http.dispatcher.httpControllerdispatcher.<SendAsync>d__1.MoveNext()

这是内部异常的堆栈跟踪

at System.linq.Expressions.Expression.New(Type type)\r\n  at System.Web.http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.http.dispatcher.DefaulthttpControllerActivator   .GetInstanceOrActivator(httpRequestMessage request,Type controllerType,Func`1& activator)\r\n   at System.Web.http.dispatcher.DefaulthttpControllerActivator   .Create(httpRequestMessage request,Type controllerType)

这是我的控制器的样子

public class TestController : APIController{    private Readonly ITestRepo _repo;    public TestController(ITestRepo repo)    {        _repo = repo;    }    public IEnumerable<string> Get()    {        return new string[] { "value1","value2" };    }    public string Get(int ID)    {        return _repo.GetID(ID);    }}

这就是我如何设置简单的注射器

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<ITestRepo,TestRepo>();      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的属性):
public voID Configuration(IAppBuilder app){    var container = GetContainer(); // Initialise container    httpConfiguration config = new httpConfiguration    {        DependencyResolver = new SimpleInjectorWebAPIDependencyResolver(container);    };    WebAPIConfig.Register(config);    app.UseWebAPI(config);}
总结

以上是内存溢出为你收集整理的c# – 在Web API和OWIN中使用简单的注射器全部内容,希望文章能够帮你解决c# – 在Web API和OWIN中使用简单的注射器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1263481.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存