I am struggling with this problem of dependency injection using untiy . I implemented this link https://www.asp.net/web-api/overview/advanced/dependency-injection
But this error turned out:
{ "Message": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'WebAPIController'. Make sure that the controller has a parameterless public constructor.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " 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()", "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "Type 'Fairmoves.Controllers.WebAPIController' does not have a default constructor", "ExceptionType": "System.ArgumentException", "StackTrace": " 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)" } }
My Web Api Controller:
[RoutePrefix("api/WebAPI")] public class WebAPIController : ApiController { private readonly IUserBusinessService UserBusinessService; private readonly ILoginBusinessService LoginBusinessService;
UnityResolver.cs
public class UnityResolver:IDependencyResolver { protected IUnityContainer container; public UnityResolver(IUnityContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = container; } [DebuggerStepThrough] public object GetService(Type serviceType) { try { return container.Resolve(serviceType); } catch (ResolutionFailedException) { return null; } } [DebuggerStepThrough] public IEnumerable<object> GetServices(Type serviceType) { try { return container.ResolveAll(serviceType); } catch (ResolutionFailedException) { return new List<object>(); } } public IDependencyScope BeginScope() { var child = container.CreateChildContainer(); return new UnityResolver(child); } public void Dispose() { container.Dispose(); } }
}
WebApiConfig.cs
public static class WebApiConfig { public static void Register(HttpConfiguration config) {
Unable to determine where the problem is? Please, enter the code and inform me. I followed all of these links below
Make sure that the controller does not have a constructor without parameters?
Autofac - make sure the controller does not have a constructor without parameters
Make sure the controller does not have a constructor without parameters without using Ninject
Make sure the controller has an error without a constructor
But not one of them solved my problem.
dependency-injection asp.net-mvc inversion-of-control asp.net-web-api unity-container
Edukondalu Thaviti Jan 6 '17 at 5:45 on 2017-01-06 05:45
source share