I have followed the below sample and it works fine until I have replaced the ProductsContext class by my own LDAPConnector class that need to get data from AD and since it's not working and get the Parameterless error.
http://www.asp.net/web-api/overview/advanced/dependency-injection
What I should add in the webAPIConfig.cs
in order to register this new class ?
I have try this but it's not working:
container.RegisterType<ILDAPConnector, LDAPConnector>(new HierarchicalLifetimeManager());
I have also tried to add parameterless construction in both Controller and in the LDAPConnector class but still get the same issue.
The controller
public class ADAccountController : ApiController
{
IADAccountRepository _repository;
public ADAccountController() : base()
{
}
public ADAccountController(IADAccountRepository repository)
{
_repository = repository;
}
public IHttpActionResult GetByID(string id)
{
try
{
if (!string.IsNullOrWhiteSpace(id))
{
AccountAD contact = _repository.GetByID(id);
if (contact == null)
{
return NotFound();
}
return Ok(contact);
the Repository where I get the error on the LDAPConnector connector = new LDAPConnector();
public class ADAccountRepository : IADAccountRepository
{
static ConcurrentDictionary<string, AccountAD> _todos = new ConcurrentDictionary<string, AccountAD>();
private LDAPConnector connector = new LDAPConnector();
public ADAccountRepository()
{
Add(new AccountAD { Name = "Item1" });
}
public AccountAD GetByID(string id)
{
return connector.GetAccountDetails(id);
//AccountAD item;
//_todos.TryGetValue(id, out item);
//return item;
}
The ADConnector
public class LDAPConnector : ILDAPConnector
{
public LDAPConnector ()
{
}
public AccountAD GetAccountDetails(string id)
{
AccountAD _AccountDetails = new AccountAD();
List<SearchResult> searchResult = new List<SearchResult>();
finally the webapiconfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var container = new UnityContainer();
container.RegisterType<IADAccountRepository, ADAccountRepository>(new HierarchicalLifetimeManager());
container.RegisterType<ILDAPConnector, LDAPConnector>(new HierarchicalLifetimeManager());
//container.RegisterType<LDAPConnector>(new InjectionFactory(c => new LDAPConnector()));
config.DependencyResolver = new UnityResolver(container);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
error reported:
{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'ADAccountController'. 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 'ADConnector.Controllers.ADAccountController' 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)"}}