Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

c# - Using Simple Injector in Web API and 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

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

Here's the stack trace

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator
  .Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, 
     Type controllerType)
   
at System.Web.Http.Controllers.HttpControllerDescriptor
  .CreateController(HttpRequestMessage request)
   
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)
  
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
   
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator
   .GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
   
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator
   .Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

Here's what my controller looks like

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);
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...