MVC 4 Web Api Controller не имеет конструктора по умолчанию?
Вот след:
An error has occurred.
Type 'ProjectName.Web.Api.Controllers.ContinentsController' does not have a default constructor
System.ArgumentException
at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
Я нахожу это странным, какpublic class UsersController : ApiController { ... }
работает просто отлично. Я сравнил 2 контроллера, все настройки и структуры одинаковы.
я используюNinject
и у меня есть настройки моей системы, похожие наJamie Kurtz
Asp.Net Mvc 4 и веб-интерфейс API: создание службы REST от начала до конца.
По трассировке стека кто-нибудь может определить проблему и как ее решить? Спасибо!
Как просили.
ContinentsController
[LoggingNHibernateSession]
public class ContinentsController : ApiController
{
private readonly ISession _session;
private readonly IContinentMapper _continentMapper;
private readonly IHttpContinentFetcher _httpContinentFetcher;
private readonly IDateTime _dateTime;
public ContinentsController(ISession session, IContinentMapper continentMapper, IHttpContinentFetcher continentFetcher, IDateTime dateTime)
{
_session = session;
_continentMapper = continentMapper;
_httpContinentFetcher = continentFetcher;
_dateTime = dateTime;
}
public IEnumerable Get()
{
var continents = _session
.Query()
.Select(_continentMapper.CreateContinent)
.ToList();
return continents;
}
public Continent Get(long id)
{
var modelContinent = _httpContinentFetcher.GetContinent(id);
var continent = _continentMapper.CreateContinent(modelContinent);
return continent;
}
}
UsersControllerРаботает просто отлично.
public class UsersController : ApiController
{
private readonly ISession _session;
private readonly IUserManager _userManager;
private readonly IUserMapper _userMapper;
private readonly IHttpUserFetcher _userFetcher;
public UsersController(
IUserManager userManager,
IUserMapper userMapper,
IHttpUserFetcher userFetcher,
ISession session)
{
_userManager = userManager;
_userMapper = userMapper;
_userFetcher = userFetcher;
_session = session;
}
[Queryable]
public IQueryable Get()
{
return _session.Query();
}
[LoggingNHibernateSession]
public User Get(Guid id)
{
var user = _userFetcher.GetUser(id);
return _userMapper.CreateUser(user);
}
}
я используюNinjectWebCommon.cs
и в нем у меня есть этот и несколько других методов по умолчанию.
private static void RegisterServices(IKernel kernel)
{
var containerConfigurator = new NinjectConfigurator();
containerConfigurator.Configure(kernel);
GlobalConfiguration.Configuration.MessageHandlers.Add(kernel.Get());
}
Тогда у меня есть:NinjectConfigurator.cs
public class NinjectConfigurator
{
......
private void AddBindings(IKernel container)
{
.....
container.Bind().To();
container.Bind().To();
//HttpFetchers
container.Bind().To();
container.Bind().To();
//TypeMappers
container.Bind().To();
container.Bind().To();
container.Bind().To();
container.Bind().To();
.........
}
.......
}
И то и другоеNinjectWebCommon.cs
а такжеNinjectConfigurator.cs
расположены вApp_Start
папка.
container.Bind().ToMethod(CreateSession);
являетсяNHibernate
, Это внутриNinjectConfigurator.cs
внутриprivate void ConfigureNHibernate(IKernel container) { ... }