1

I am following Onion Architecture for my Web API and AngularJS Project. In the Infrastructure.DependencyResolution part, I am using Simple Injector. Here is my code:

[assembly: PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]
namespace Infrastructure.DependencyResolution
{
    public class IocConfig
    {
         private static Container _container;

         public static void RegisterDependencies()
         {
              _container = new Container();

              _container.Verify();

              _container.RegisterWebApiRequest<IEntitiesContext>(() =>
              {
                   return new MyContext();
              });

              _container.RegisterWebApiRequest<IUserRepository, UserRepository>();
              _container.RegisterWebApiRequest<IAccountService, AccountService>();
              _container.RegisterWebApiRequest<IUnitOfWork, UnitOfWork>();

         }
    }
}

Now If I try to put url to go to my account controller, I am getting this error:

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

I searched and found out that Simple Injector has some different code for Web Api as suggested by their site Like:

// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

I copied that as well but it did not accept GlobalConfiguration.Configuration, I guess because I am using it in a library project.

When I set a break point in the RegisterDependencies method, Visual Studio doesn't stop at the break point.

Can someone show me the way where to go?

Steven
  • 166,672
  • 24
  • 332
  • 435
Usman Khalid
  • 3,032
  • 9
  • 41
  • 66

1 Answers1

5

You are missing is the registration of the SimpleInjectorWebApiDependencyResolver as described in the Web API integration guide:

GlobalConfiguration.Configuration.DependencyResolver =
    new SimpleInjectorWebApiDependencyResolver(container);

Without overriding Web API's default IDependencyResolver, Web API will simply try to instantiate controller instances itself, and this requires controller types to have a default constructor. By replacing this IDependencyResolver with the SimpleInjectorWebApiDependencyResolver requests for controller instances are forwarded to the Simple Injector container.

You will have the most succes when using Simple Injector's Web API Quick Start NuGet package. This will bootstrap your application and allows you to get started quickly.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • GlobalConfiguration is not recognized. I try to include using System.Web.Http but still it is not recognizing. :( – Usman Khalid May 30 '15 at 13:13
  • @UsmanKhalid: `GlobalConfiguration` is defined in the `System.Web.Http.WebHost.dll` assembly. You'll need to include that. – Steven May 30 '15 at 13:15
  • Added and tried. But still getting the same error, An error occurred when trying to create a controller of type 'AccountController'..... – Usman Khalid May 30 '15 at 13:29
  • @UsmanKhalid: What does `AccountController` derive from? I bet it derives from `Controller` (which is MVC) instead of `ApiController` (which is Web API). Please [read this](https://simpleinjector.readthedocs.org/en/latest/mvcintegration.html) to see how to integrate Simple Injector with MVC. – Steven May 30 '15 at 13:31
  • public class AccountController : ApiController – Usman Khalid May 30 '15 at 13:36
  • @UsmanKhalid: In that case the `Configuration.DependencyResolver` might get overridden somewhere else in the application after you set it. – Steven May 30 '15 at 13:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79194/discussion-between-steven-and-usman-khalid). – Steven May 30 '15 at 13:40