You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()
{
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.
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.
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.
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;
}
}
[ApiVersion("2.0")]
public class BankAccountV2Controller : UserApiControllerBase
{
public BankAccountV2Controller()
{
It works with all the controllers with parameterless constructor (version 2.0)
The text was updated successfully, but these errors were encountered: