'ASP .NET MVC 4 dependency injection "No parameterless constructor defined for this object"
I am trying to inject IProfileService into controller constructor but I am getting error "No parameterless constructor defined for this object" when calling that controller. If I add parameterless constructor, controller calls it uses it, but then I get "Object reference not set to an instance of an object" error on _profileService.SetPropertyValues(ctx, values); as it is null if not injected.
Interface:
namespace SecurityGuard.Interfaces
{
public interface IProfileService
{
string ApplicationName { get; set; }
int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate);
int DeleteProfiles(string[] usernames);
int DeleteProfiles(ProfileInfoCollection profiles);
ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords);
ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords);
ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords);
ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords);
int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate);
SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection);
void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection);
}
}
Controller class:
public class AjaxSelectController : AmrController
{
private readonly IProfileService _profileService;
public AjaxSelectController(IProfileService profileService) {
_profileService = profileService;
}
[HttpPost]
public ActionResult SaveTableColumnsToDb(string data, string propertyName)
{
try
{
_profileService.SetPropertyValues(ctx, values);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
