0

I've created the following helper method which uses a constructor to retrieve the DbContext.

namespace AzureSdkExamples.Helpers
{
    public class ComputeHelpers
    {
        private readonly ApplicationDbContext _dbContext;

        public ComputeHelpers(ApplicationDbContext db)
        {
            _dbContext = db;
        }

        public async Task<List<VirtualMachine>> GetClientVirtualMachines(int customerId)
        {
            var azureClient = _dbContext.CreateAzureClient(customerId);

            var virtualMachines = await azureClient.VirtualMachines.ListAsync();

            List<VirtualMachine> virtualMachineObjects = new List<VirtualMachine>();

            int vmCount = 0;

            foreach (var virtualMachine in virtualMachines)
            {
                virtualMachineObjects.Add(
                    new VirtualMachine
                    {
                        Id = vmCount,
                        VirtualMachineName = virtualMachine.ComputerName,
                        AzureVmId = virtualMachine.VMId
                    }
                );
                vmCount++;
            }


            return virtualMachineObjects;
        }
    }
}

The CreateAzureClient is essentially a db call to retrieve my personal subscriptions and tenant information from Azure. It returns an IAzure type.

I want to them call this non-static method from my Controller. I can't use a constructor on the method as this results in the following error:

InvalidOperationException: Unable to resolve service for type 'AzureSdkExamples.Helpers.ComputeHelpers' while attempting to activate 'AzureSdkExamples.Controllers.VirtualMachinesController'.

This is my controller when calling the helper method:

namespace AzureSdkExamples.Controllers
{
    [Route("api/[controller]")]
    public class VirtualMachinesController : Controller
    {
        private readonly ComputeHelpers _computeHelper;

        public VirtualMachinesController(ComputeHelpers computeHelpers)
        {
            _computeHelper = computeHelpers;
        }

        [HttpGet("{id}", Name = "GetAllVirtualMachines")]
        public async Task<JsonResult> GetAllVirtualMachines(int customerId)
        {
            var virtualMachines = await _computeHelper.GetClientVirtualMachines(customerId);

            return Json(virtualMachines);
        }
    }
}

I'm actually at a bit of a loss on what to do, mainly due to my own misunderstanding of how to appropriately chain these constructors/methods the correct way.

I'm trying to take the best approach to avoid potential memory leaks in a multi-user context.

How do I correctly call this helper method from the controller considering the use of DI in the helper?

2
  • Either register helper in DI container, or change controller constructor to accept ApplicationDbContext and construct helper yourself. Commented Jan 29, 2018 at 9:52
  • @Evk This was indeed the problem. Feel a bit stupid for not realising. Commented Jan 29, 2018 at 9:58

1 Answer 1

1

You should register ComputeHelpers in the DI container.

You can do that in the Startup class in the ConfigureServices method, e.g. with AddTransient:

services.AddTransient<ComputeHelpers, ComputeHelpers>();

See the documentation for more details about the Dependency Injection in ASP.NET Core

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

1 Comment

I feel a bit stupid for not reliasing I had to register the helper to the DI services in AspNet. This was the problem. Couple of other issues but basically this was the reason.

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.