加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:28:49 所属栏目:百科 来源:网络整理
导读:我遇到与 described here相同的问题,我的设置几乎是 identical to this,实际上是基于 this guide.当我访问我的控制器中的一个方法,我得到这个 An error occurred when trying to create a controller of type ‘TestController’. Make sure that the contro
我遇到与 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)rn   
at System.Web.Http.Controllers.HttpControllerDescriptor
  .CreateController(HttpRequestMessage request)rn   
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()

这是内部异常的堆栈跟踪

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,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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读