Dependency Injection - Web Api

Asked

Viewed 123 times

0

Good Afternoon,

I’m trying to create the dependency injection in the Web Api, but I’m not getting it, I get the following error:

SimpleInjector.DiagnosticVerificationException Hresult=0x80131500 Message=The Configuration is invalid. The following Diagnostic warnings Were reported: -[Lifestyle Mismatch] Bracoservice (Singleton) depends on Banco (Transient). -[Lifestyle Mismatch] Cabecaservice (Singleton) depends on Banco (Transient). -[Disposable Transient Component] Banco is Registered as Transient, but Implements Idisposable.

Global.asax.Cs:

using BecomexTeste.Service;
using SimpleInjector;
using SimpleInjector.Integration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace BecomexTeste
{
public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        var container = new Container();

        container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

        container.Register<IBracoService, BracoService>(Lifestyle.Singleton);
        container.Register<ICabecaService, CabecaService>(Lifestyle.Singleton);

        container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
        container.Verify(); <----- O ERRO ESTÁ AQUI ----->

        GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
    }
}
}
  • Try to change Lifestyle to Transient, like this Lifestyle.Transient

  • I’ve tried, but the error continues

  • What is this bank? is its context? I think you have to register it tbm

  • that’s the bank public class Banco : DbContext&#xA; {&#xA; public virtual DbSet<Braco> Bracos { get; set; }&#xA; public virtual DbSet<Cabeca> Cabeca { get; set; }&#xA; }

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.