.NET 5 RC1 API Controller constructor issue

87 views Asked by At

I am working moving a Blazor WebAssembly project (Net Core 3.1) over to .NET 5 RC1 but I am having issues with a controller. This code:

        [Authorize]
        [Route("[controller]")]
        [ApiController]
        public class ApplicationUserController : ControllerBase
        {
            private readonly RGDbContext _context;
            // Create a field to store the mapper object
            private readonly IMapper _mapper;
            private readonly UserManager<ApplicationUser> _userManager;
            private readonly SignInManager<ApplicationUser> _signInManager;
            // Field for DBContext

            // Assign the object in the constructor for dependency injection
            public ApplicationUserController(RGDbContext context, IMapper mapper, UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
            {
                _context = context;
                _mapper = mapper;
                _userManager = userManager;
                _signInManager = signInManager;
            }

Works fine under Core 3.1, but now is crashing out as soon as the controller is invoked - sorry I cant be more specific than that, but there is no exception, the calling blazor app just gets a 500 error, and no breakpoints are hit in the controller.

If I comment out the constructor code so I effectively just have:

                public ApplicationUserController()
            {
            }

It works, at least to the extent that I can successfully call in to the controller and hit a break point, and return 'stuff' - I pasted in the 'WeatherController' template code...

My question is should the dependency injection still work in .NET 5 - see that there is this:

Microsoft.Extensions.DependencyInjection

But that looks like something for .Net Framework, as it lists .NET Framework 4.6.1 as one of its dependencies.

Besides, Dependency injection IS working in my project, I use it in the Startup.Cs to pass user and role managers to my config code...

                public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
                     UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
            {

Do controllers behave differently, do I have to go back to a more .Net Framework centric way of initialising/passing these objects, is it broken (fully aware that this is bleeding edge), or am I just barking up the wrong tree?

0

There are 0 answers