unitycontainer / aspnet-webapi

Unity.AspNet.WebApi package
Apache License 2.0
9 stars 11 forks source link

Web API Dependency Injection with Unity error - No public constructor is available for type System.Web.Http.Dispatcher.IHttpControllerActivator #17

Open nadeem1990 opened 4 years ago

nadeem1990 commented 4 years ago

I am trying to implement the dependency injection in ASP.Net web API with unity container and have implemented the exactly with below 2 articles:

1) https://stackoverflow.com/a/38770689/3698716 2) https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection

Both are related to each other with some minor change but implementation for both are the same.

After implementing I am getting an error in the method GetService in UnityResolver.cs class:

"Resolution failed with error: No public constructor is available for type System.Web.Http.Dispatcher.IHttpControllerActivator."

nadeem1990 commented 4 years ago

tagging @devtrends for help.

ENikS commented 4 years ago

Did you try the latest Unity?

nadeem1990 commented 4 years ago

Did you try the latest Unity?

I have used Unity 5.11.7 and Unity.WebAPI 5.4.0 versions, Those were the latest available version on the NuGet.

ENikS commented 4 years ago

Unfortunately I know very little about Web API and cant't really help. Try looking into history of this project for one of the examples. If this doesn't help perhaps this could give some clues.

nadeem1990 commented 4 years ago

Unfortunately I know very little about Web API and cant't really help. Try looking into history of this project for one of the examples. If this doesn't help perhaps this could give some clues.

Unable to get any help with web API in the given examples.

devtrends commented 4 years ago

If you want to integrate Unity with ASP.NET Web API, you have at least 3 choices:

1) Do it manually - this is what your links explain how to do by writing your own IDependencyResolver implementation.

2) Use my Unity.WebAPI library. It sounds as though you are already using this, in which case, you might want to look here. Judging by the error, it seems as though the dependency resolver has not been registered so I am wondering if you have missed out adding UnityConfig.RegisterComponents(); to Application_Start()?

3) Use Unity.AspNet.WebApi. I don't know much about this but someone here may be able to help.

nadeem1990 commented 4 years ago

If you want to integrate Unity with ASP.NET Web API, you have at least 3 choices:

  1. Do it manually - this is what your links explain how to do by writing your own IDependencyResolver implementation.
  2. Use my Unity.WebAPI library. It sounds as though you are already using this, in which case, you might want to look here. Judging by the error, it seems as though the dependency resolver has not been registered so I am wondering if you have missed out adding UnityConfig.RegisterComponents(); to Application_Start()?
  3. Use Unity.AspNet.WebApi. I don't know much about this but someone here may be able to help.

@devtrends I have done some minor changes in the code and I dont think those are making problem but now I am getting stackoverflow exception and my code is breaking, I have added my entire code base along with error here

gavinbarron commented 3 years ago

@nadeem1990 is this an issue that is happening for every request or is it infrequent?