Skip to content
This repository has been archived by the owner on Mar 2, 2023. It is now read-only.

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

Open
nadeem1990 opened this issue Jul 29, 2020 · 8 comments

Comments

@nadeem1990
Copy link

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
Copy link
Author

tagging @devtrends for help.

@ENikS ENikS transferred this issue from unitycontainer/unity Jul 29, 2020
@ENikS
Copy link
Contributor

ENikS commented Jul 29, 2020

Did you try the latest Unity?

@nadeem1990
Copy link
Author

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
Copy link
Contributor

ENikS commented Jul 29, 2020

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
Copy link
Author

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
Copy link

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
Copy link
Author

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants