I have a few functions I use in multiple controllers. I put the functions in a class so that I can use them without duplicating.
When I try to inject my DB context in the class like I do with my controllers I get:
InvalidOperationException: Unable to resolve service for type 'TMS.Functions.Tools' while attempting to activate 'TMS.Controllers.TestController'.
Example of how I inject in my controllers, and what fails in my class:
private readonly TMSContext _context;
public Tools(TMSContext context)
{
_context = context;
}
This is how I inject my tools function:
using TMS.Functions;
namespace TMS.Controllers
{
[Authorize]
public class PayPortalController : Controller
{
private readonly UserManager<TMSUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly TMSContext _context;
private readonly IConfiguration _config;
private readonly Tools _tools;
public PayPortalController(UserManager<TMSUser> userManager
,RoleManager<IdentityRole> roleManager
,TMSContext context
,IConfiguration config
,Tools tools)
{
_userManager = userManager;
_roleManager = roleManager;
_context = context;
_config = config;
_tools = tools;
}
...
What is the proper way to do this for a class?