When injecting a dependency into the controller, Postman returns an error

0

When doing this in my Controller :

[Route("api/[controller]")]
    public class OptOutClientController : Controller
    {
        IOptOutService _service;

        //Se comentar o construtor dá certo
        public OptOutClientController(IOptOutService service)
        {
            _service = service;
        }

        [HttpPost]
        public async Task<OptOutResult> Unsubscribe([FromBody]OptOutCliente cliente)
        {
            if (cliente == null)
                throw new OptOutException("Informar os dados do cliente OptOut!");

            var result = await _service.Process(new OptOutCliente(cliente.Cpf, cliente.Email, cliente.Telefone, cliente.Bandeira, cliente.Canal));

            return null;

        }

    }

I get a Internal Server Error (500) with the injection in the constructor. If the builder comments, then it works. What do I have to do in Postman or the service call to not receive this error?

EDIT1

MyStartup.cs:

publicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){services.AddMvc();services.AddRouting();}publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){app.UseMvc(routes=>{routes.MapRoute(name:"optoutroute",
                    template: "{controller=OptOutClient}/{action=Index}");
            });
     }
}

EDIT2

My Startup thus became theConfigureServices method

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddRouting();
            services.AddScoped<IOptOutService, ClientUnsubscribe>();
        }

My controller is like this

HttpClient client = new HttpClient();
        private readonly IOptOutService _service;
        public OptOutClientController(IOptOutService service)
        {
            _service = service;
        }
..........

In the startup I tried: Scoped, Singleton, Transient, all cycles and continues with Server Error

    
asked by anonymous 21.06.2018 / 19:48

1 answer

1

You need to declare this dependency of IOptOutService in the ConfigureServices method of the Startup class.

services.addScoped<IOptOutService, SuaImplementacao>();
    
21.06.2018 / 21:08