Constructor with dependency injection causes http 500 error in react asp.net core app


Renacre

The following controllers handle usage operations such as registration. However, when I Registeruse axios to call the method in the controller, I get the following error.

Failed to load resource: The server responded with a status of 500()

However, if I comment the code in the constructor, the call succeeds. I don't understand why the constructor produces this problem. Any suggestions?

public class ApplicationUserController : ControllerBase
{
    private IUserService _userService;
    private IMapper _mapper;
    private readonly AppSettings _appSettings;

    public ApplicationUserController(
        IUserService userService,
        IMapper mapper,
        IOptions<AppSettings> appSettings
    )
    {
        _userService = userService;
        _mapper = mapper;
        _appSettings = appSettings.Value;
    }

    [AllowAnonymous]
    [HttpPost]
    [Route("api/ApplicationUser/Register")]
    public IActionResult Register([FromBody]ApplicationUserDto userDto)
    { 
         //implementation details
    }
}

Here is the IUSerServices interface:

public interface IUserService
{
    ApplicationUser Authenticate(string username, string password);

}

public class UserService : IUserService
{
    private myDbContext _context;

    public UserService(myDbContext context)
    {
        _context = context;
    }

    public ApplicationUser Authenticate(string username, string password)
    {
        //implementation details
    }
}

DI is registered atStartup.cs

services.AddScoped<IUserService, UserService>();
services.AddScoped<myDbContext>();
Ginesh

IMapperis an interface in the AutoMapper library.

You need to add the package and callAutoMapper.Extensions.Microsoft.DependencyInjection

services.AddAutoMapper();

Related


Dependency Injection in ASP.NET Core

R4nc1d In Autofac you can use RegisterAssemblyTypes to register your dependencies so you can do something like that, is there a way to do something like DIthis in builtins.net Core builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data")) .Where(t =

Instant Dependency Injection in ASP.net Core

Kento Is it possible to get the dependencies on the fly by somehow getting a reference to the IServiceProvider or some class that can resolve the dependencies? For example, when handling exceptions to UseExceptionHandleroutput something meaningful to the clien

.net core dependency injection with parameters on constructor

Chai Wei Jian In .net core, if I use dependency injection, do all constructor parameters have to be provided by DI? for example: public Person CreateClient() { string phone = "12345678"; return new Person(phoneNumber: phone); } public class Pe

.Net core dependency injection inject from constructor

Tony Joraz I need to inject everything I declare in the installer from the constructor. can i do it? How to inject service from constructor? Similar to the Injector service in Angular 2. Injected service without constructor in controller something like this

Advanced Dependency Injection in ASP.NET Core

Palmy I have the following interfaces, abstract classes, etc. public interface IAggregateRootMapping<T> : IAggregateDefinition where T : AggregateRoot { IEnumerable<Expression<Func<T, object>>> IncludeDefinitions { get; } } public abstract class Aggregate

ASP.Net Core RouteBuilder and Dependency Injection

Kevin Brighton I am trying to do routing in ASP.NET Core 2.0. I have a class that wraps System.DateTime.UtcNowfunctionality (for simpler unit testing) public interface IDateTimeWrapper { DateTime UtcNow(); } public sealed class DateTimeWrapper : IDateTime

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

asp.net mvc core dependency injection constructor parameters

Lord Vermilion I am trying to understand dependency injection in ASP.NET MVC CORE. All examples are the same, they show to registerHttpContextAccessor public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSingleton

Instant Dependency Injection in ASP.net Core

Kento Is it possible to get the dependencies on the fly by somehow getting a reference to the IServiceProvider or some class that can resolve the dependencies? For example, when handling exceptions to UseExceptionHandleroutput something meaningful to the clien

Dependency Injection ASP.NET Core Singleton

urge I can figure out what's going on with the following line of code:- Why can't I use a singleton on it? services.AddSingleton<IProductRepository, ProductRepository>(); I'm getting a 500 Internal Server Error with the above code, but using Transientand work

Dependency Injection in ASP.NET Core

R4nc1d In Autofac you can use RegisterAssemblyTypes to register your dependencies so you can do something like that, is there a way to do something like DIthis in builtins.net Core builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data")) .Where(t =

.net core dependency injection with parameters on constructor

Chai Wei Jian In .net core, if I use dependency injection, do all constructor parameters have to be provided by DI? for example: public Person CreateClient() { string phone = "12345678"; return new Person(phoneNumber: phone); } public class Pe

.Net core dependency injection inject from constructor

Tony Joraz I need to inject everything I declare in the installer from the constructor. can i do it? How to inject service from constructor? Similar to the Injector service in Angular 2. Injected service without constructor in controller something like this

.net core dependency injection with parameters on constructor

Chai Wei Jian In .net core, if I use dependency injection, do all constructor parameters have to be provided by DI? for example: public Person CreateClient() { string phone = "12345678"; return new Person(phoneNumber: phone); } public class Pe

.Net core dependency injection inject from constructor

Tony Joraz I need to inject everything I declare in the installer from the constructor. can i do it? How to inject service from constructor? Similar to the Injector service in Angular 2. Injected service without constructor in controller something like this

.net core dependency injection with parameters on constructor

Chai Wei Jian In .net core, if I use dependency injection, do all constructor parameters have to be provided by DI? for example: public Person CreateClient() { string phone = "12345678"; return new Person(phoneNumber: phone); } public class Pe

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

ASP.NET Core MVC causes 500 error on IIS 8.5

Mickey Using .NET Core 3.1, I developed an MVC application and deployed it on IIS 8.5. Using a browser, I can go to the home page when I visit, domain.com/myApp/but I get an http 500 error when I visit domain.com/myApp. This is working locally. The problem occ

.net core dependency injection with parameters on constructor

Chai Wei Jian In .net core, if I use dependency injection, do all constructor parameters have to be provided by DI? for example: public Person CreateClient() { string phone = "12345678"; return new Person(phoneNumber: phone); } public class Pe

.Net core dependency injection inject from constructor

Tony Joraz I need to inject everything I declare in the installer from the constructor. can i do it? How to inject service from constructor? Similar to the Injector service in Angular 2. Injected service without constructor in controller something like this

.Net core dependency injection inject from constructor

Tony Joraz I need to inject everything I declare in the installer from the constructor. can i do it? How to inject service from constructor? Similar to the Injector service in Angular 2. Injected service without constructor in controller something like this

.net core dependency injection with parameters on constructor

Chai Wei Jian In .net core, if I use dependency injection, do all constructor parameters have to be provided by DI? for example: public Person CreateClient() { string phone = "12345678"; return new Person(phoneNumber: phone); } public class Pe

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

Constructor dependency injection in asp.net core 2

Bob 5421 I have read this official documentation : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . Something I don't understand in constructor injection: Let's take a look at my code, it works fine: public class HomeController

asp.net mvc core dependency injection constructor parameters

Lord Vermilion I am trying to understand dependency injection in ASP.NET MVC CORE. All examples are the same, they show to registerHttpContextAccessor public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSingleton