Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Make sure that the controller has a parameterless public constructor #181

Closed
camphan opened this issue Sep 6, 2017 · 5 comments
Closed

Comments

@camphan
Copy link

camphan commented Sep 6, 2017

I had that error:
"An error occurred when trying to create a controller of type 'BankAccountsController'. Make sure that the controller has a parameterless public constructor"
When tried to get this version 1.0 Api
[ApiVersion("1.0")]
public class BankAccountsController : UserApiControllerBase
{
private readonly IGetAccountsQuery _getAccountsQuery;
private readonly IAddBankAccountCommand _addBankAccountCommand;
private readonly IRemoveBankAccountCommand _removeBankAccountCommand;
protected BankAccountsController(IGetAccountsQuery getAccountsQuery,
IAddBankAccountCommand addBankAccountCommand,
IRemoveBankAccountCommand removeBankAccountCommand
)
{
_getAccountsQuery = getAccountsQuery;
_addBankAccountCommand = addBankAccountCommand;
_removeBankAccountCommand = removeBankAccountCommand;
}

    /// <summary>
    /// Get all bank accounts for specific institution
    /// </summary>
    /// <remarks>
    /// This API returns the list of accounts for an institution for an authenticated user
    /// </remarks>
    /// <returns></returns>
    
    [VersionedRoute("api/BankAccounts")]
    [SwaggerResponse(HttpStatusCode.OK, type: typeof(BasiqAccountModel[]))]
    public BasiqAccountModel[] GetAccounts([FromUri] string institutionId)
    {
        var response = _getAccountsQuery.Invoke(new GetAccountsRequest
        {
            UserId = AppContext.CurrentUser.id,
            InstitutionId = institutionId,
            Refreshed = false
        });

        return response.Accounts.Select(AutoMapper.Mapper.Map<BankAccountInfo, BasiqAccountModel>).ToArray();
    }

}
[ApiVersion("2.0")]
public class BankAccountV2Controller : UserApiControllerBase
{
public BankAccountV2Controller()
{

    }
    [VersionedRoute("api/BankAccounts")]
    [SwaggerResponse(HttpStatusCode.OK, type: typeof(BasiqAccountModel[]))]
    public string[] GetAccounts([FromUri] string institutionId)
    {
        return new[] { "abc00" };
    }
}

It works with all the controllers with parameterless constructor (version 2.0)

@commonsensesoftware
Copy link
Collaborator

Best that I can tell, you are using Web API. Are you using some form of dependency injection (DI)? Web API does not support DI out-of-the-box. The default implementation of the IHttpControllerActivator only supports creating controller types with parameterless constructors via Activator.CreateInstance.

API versioning does not get involved with nor modify the way that controllers are activated.

@camphan
Copy link
Author

camphan commented Sep 8, 2017

I'm using windsor to create that controllers, so there are any ways to implement Api versioning in this case?

@commonsensesoftware
Copy link
Collaborator

I assume you're using an implementation of IDependencyResolver, IDependencyScope, or perhaps even IHttpControllerActivator that is backed by Windsor. Any DI framework will work with API versioning. API versioning does not get involved with or otherwise change any behaviors related to DI or controller activation.

API versioning does replace the following built-in Web API services:

  • IHttpControllerSelector
  • IHttpActionSelector

If your application is also replacing these (which is unusual), you'll need to decorate the API versioning types ApiVersionControllerSelector and/or ApiVersionActionSelector with your own types during registration.

If you're not changing these services, then there must be something wrong with the type mapping for the controller in your container.

@commonsensesoftware
Copy link
Collaborator

You might be interested in checking out #184. I provided a working example of using the world's simplest DI container with API versioning. Perhaps you can compare container setup with your own.

@commonsensesoftware
Copy link
Collaborator

Seems like you have resolved your issue. If you continue to face challenges feel free to create a new issue or re-open this one. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants