0

The problem as reported by the error message is:

"An error occurred when trying to create a controller of type 'BooksController'. Make sure that the controller has a parameterless public constructor."

I'm trying to have the controller successfully execute and/or return a value.

This is a WebApi project using an AngularJs app as the frontend. Any calls to this controller return a 500 error with the above message. Ishould also add that I am using Insight.Database

I have tried creating a new controller and adding and removing the constructor. But I wan't able to get any better hints about what I'm doing wrong.

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading.Tasks; 
using Insight.Database;
using Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.OAuth;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.SqlServer;
using Wallet.Models.Books;
using Wallet.Models.Stores; 
using Wallet.Models.Database;

namespace Wallet.Controllers
{
    [Authorize]
    [RoutePrefix("api/books")]
    public class BooksController : ApiController
    {
        private IBooksDataAccess access; 
        public BooksController()
        {
            access = HttpContext.Current.GetOwinContext().Get<SqlConnection>()
                .As<IBooksDataAccess>(); 
        }

        // POST api/books/GetActions
        [HttpPost]
        [Route("GetActions")] 
        public IList<HistoryAction> ActionsHH([FromBody] Dictionary<string, string> input)
        {
            var name = input["name"]; 
            var results = access.GetHistoricalActions(name); 
            return results; 
        }

        // POST api/books/InsertTransaction
        [HttpPost]
        [Route("InsertTransaction")] 
        public void InsertTx([FromBody] Dictionary<string, string> input)
        {
        }
    }
}

And here's the IBookDataAccess

namespace Wallet.Models.Database
{
    [Sql(Schema = "Models")]
    public interface IBooksDataAccess
    {
        IList<HistoryAction> GetHistoricalActions(string UserName);
        int InsertTransaction(Dictionary<string, string> transaction); 
    }
}
11
  • Possibly a duplicate question. The same has been discussed here.. stackoverflow.com/questions/24014644/… did you try this link? Commented Apr 17, 2015 at 18:31
  • I'm sorry, I put up the wrong code. However the error persists regardless of whether or not a constructor is there. Commented Apr 17, 2015 at 18:54
  • @Vim I did come across that while searching, but even after adding a constructor(empty or not) the error still occurs. Commented Apr 17, 2015 at 19:11
  • I'm not familiar with WebAPI or Angular.js, but the error stating that a parameterless constructor is needed implies to me that it's a serialization issue. This in turn leads me to suggest you ensure a parameterless constructor exists but without any content, and add an Initialize() method, setting your access member variable there instead. This will ensure an instance of the class can be created during serialization without executing code that may not be valid in that context. (Obviously you'll then need to call Initialize() when using the class.) Commented Apr 17, 2015 at 19:25
  • Is the error same for blank constructor? If not, then post that error message. Commented Apr 17, 2015 at 20:13

1 Answer 1

0

This error will occur when the controller cannot find a parameter-less constructor, or in the case when an exception is thrown in the constructor. It looks like you are using Insight.Database, given the .As extension.

If Insight fails to generate the dynamic assembly, it will throw an exception (which you are not catching, since you instantiate the insight auto-interface builder in your member declaration.)

I will have to do some testing, but I believe I recall having issues with the name "transaction" being interpreted as the T-SQL type.

Can you try creating a constructor for your controller, and assigning the database object inside of a try/catch block inside of it?

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! The name of an interface property was causing the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.