I am very new to using Unity, but my problem is that whenever I call my web service, I get an exception indicating that
"Make sure the controller has a hassle-free public constructor"
I have been following several tutorials and I am still having the same problem.
In the Register function of my WebApiConfig class, I have
var container = new UnityContainer(); container.RegisterType<IValidator, Validator>(new HierarchicalLifetimeManager()); config.DependencyResolver = new UnityResolver(container);
Here is my UnityResolver class
using Microsoft.Practices.Unity; using System; using System.Collections.Generic; using System.Web.Http.Dependencies; public class UnityResolver : IDependencyResolver { protected IUnityContainer container; public UnityResolver(IUnityContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = container; } public object GetService(Type serviceType) { try { return container.Resolve(serviceType); } catch (ResolutionFailedException) { return null; } } 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(); } }
I have not registered any controllers, as each tutorial claims that I do not need to do this. Here is my actual controller
public class Controller: ApiController { private IValidator _validator; public Controller(IValidator validator) { this._validator = validator; } [HttpPost] public void ReceiveIPN() { } }
Does anyone have any ideas on what I might be doing wrong? Thank!
EDIT 1: Here is the "implementation" of the Validator class. This is pretty empty because I didnβt want to introduce an error here until I solved the Unity problem.
using System; using System.Collections.Generic; using System.Linq; using System.Web; public class Validator: IValidator { public bool ValidateIPN(string body) { throw new NotImplementedException(); } }
EDIT 2: Here is the full error response I get when I try to invoke the api web route using Fiddler
{"message": "An error occurred.", "exceptionMessage": "An error occurred while trying to create a controller of type" Controller. "Make sure that the controller has stepless publication. Constructor" "ExceptionType.": "System.InvalidOperationException" "StackTrace" : "in the System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, type ControllerType) in System.Web.Http.Controllers.HttpHttp.ControlPerControl.ControllerControllerControllerControllerControllerControllerControllerControllerControllerControllerControllerControllerControllerController.ControllerController. .d__1.MoveNext () "" InnerException ": {" message ":" error ".," exceptionMessage ":" Type "Project.Controller 'does not have a default constructor" "ExceptionType": "System.ArgumentException", " Sta ckTrace ":" in System.Linq.Expressions.Expression.New (type type) in System.Web.Http.Internal.TypeActivator.Create [TBase] (instanceType type) in System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstatorOrActArAct HttpRequestMessage request, Type controllerType, Func`1 & activator) under System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, type ControllerType) "}}