Let me describe the sitation.
I use web api and in the controller class where I define my http verbs I noticed that I cannot create a constructor. infact if I add a constructor which is empty it is oke but when I modify this constructor it is not. It gives me the following error when I try to post with postman to the method inside the controller class
{
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'DataController'. Make sure that the controller has a parameterless public constructor.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " bij System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n bij System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n bij System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Type Chatbot.Controller.DataController heeft geen standaardconstructor",
"ExceptionType": "System.ArgumentException",
"StackTrace": " bij System.Linq.Expressions.Expression.New(Type type)\r\n bij System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n bij System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n bij System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}
}
the controller class
public class DataController : ApiController {
public string data { get; set; }
public DataController(string analyticsData) {
data = analyticsData;
}
public void Post([FromBody] dynamic data) {
_analyticsData.InsertCredentials("fdf", data, "dfdf");
How can I achieve my goal to add a non empty constructor to the controller class without resulting in this error?
[Edit] I want this because of the following reason:
private AnalyticsData _analyticsData;
public CredentialsController(AnalyticsData analyticsData) {
_analyticsData = analyticsData;
}
//acces the following method send data to database
_analyticsData.InsertCredentials("fdf", "fdd", "dfdf");
I want to acces this method from the constructor however I already get this error if my constructor is not empty. Like I said when I add a empty constructor next to it it will not anymore receive an error but _analyticsData could not be found since this is only available in the second constructor
[Edit] I am now using Unity to DI this is the line I have added inside my WebApiConfig
container.RegisterType<IAnalyticsData, AnalyticsData>(new HierarchicalLifetimeManager());
I tested this with other classes and interfaces and it works. But It does not work when the class for example AnalyticsData has a constructor inside it. So This only works if I remove the constructor from the AnalyticsData class
Constructor from AnalyticsData class
public AnalyticsData(ResourceFile file, ResourceFile creationFile) {
var exists = file.Exists();
_connection = SQLite.Open(file);
if (!exists && creationFile.Exists()) {
SQLite.Execute(_connection, creationFile.GetText());
}
}
The constructor is needed to load the current sqlite database
AnalyticsData = new AnalyticsData(new ResourceFile(Settings.String("DatabaseLocation")), Resources.GetFolder("data").GetFile("create.sql"));
The only problem I am now receiving is that I need to use the constructor I cannot use without it because then I cannot anymore make a connection to the database.
Do I need to do something more then just adding this line?
container.RegisterType<IAnalyticsData, AnalyticsData>(new HierarchicalLifetimeManager());