在Web API和OWIN中使用简单注入器 [英] Using Simple Injector in Web API and OWIN

查看:152
本文介绍了在Web API和OWIN中使用简单注入器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与此处描述的 并且我的设置几乎与此相同实际上基于本指南.当我在控制器中访问方法时,得到此

I'm experiencing the same problem as described here and my set up is almost identical to this that is actually based on this guide. When I access a method in my controller I get this

尝试创建类型的控制器时发生错误 'TestController'.确保控制器具有无参数 公共构造函数.

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()

这是内部异常的堆栈跟踪

And here's the inner exception's stack trace

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

这是我设置简单注入器的方式

And here's how I set up 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<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 的属性):

I had the same issue but with UnityDependencyResolver. But I think it should also work for SimpleInjectorWebApiDependencyResolver. Try to register your resolver like this (as a Property of 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);
}

这篇关于在Web API和OWIN中使用简单注入器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆