使用Web API时使用Simple Injector时出现问题

时间:2018-02-06 21:08:42

标签: asp.net-web-api dependency-injection simple-injector

我在使用Simple Injector和使用VS 2015创建的默认WebAPI项目时遇到问题。 我有AccountController具有以下构造函数

public AccountController()
{
}

public AccountController(ApplicationUserManager userManager,
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
    UserManager = userManager;
    AccessTokenFormat = accessTokenFormat;
}

为了注册这些,我在Simple Injector中使用了以下代码

// Create the container.
var apiContainer = new Container();
apiContainer.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
apiContainer.Options.ConstructorResolutionBehavior = new ConstructorBehavior();

//register the classes that we are going to use for dependency injection            
apiContainer.Register<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(new ApplicationDbContext()),Lifestyle.Scoped);
apiContainer.Register<IDataProtector>(() => new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider().Create("ASP.NET Identity"),Lifestyle.Transient);            
apiContainer.Register<ISecureDataFormat<AuthenticationTicket>, SecureDataFormat<AuthenticationTicket>>(Lifestyle.Transient);
apiContainer.Register<ITextEncoder, Base64UrlTextEncoder>(Lifestyle.Scoped);
apiContainer.Register<IDataSerializer<AuthenticationTicket>, TicketSerializer>(Lifestyle.Scoped);

//apiContainer.RegisterCommonClasses();

//register the webapi controller
apiContainer.RegisterWebApiControllers(configuration);

但在此之后我收到了

的警告信息
  

[一次性瞬态组件] ApplicationUserManager注册为瞬态,但实现了IDisposable

有人可以帮我解决这个问题吗?使用VS 2015的默认Web api项目,它添加了帐户控制器,并使用ApplicationUserManager并具有以下详细信息

public ApplicationUserManager(IUserStore<ApplicationUser> store)
    : base(store)
{
}

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
    var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

我得到的另一个问题如下

HttpConfiguration类型的构造函数包含名称为“routes”且未注册的类型HttpRouteCollection的参数。请确保已注册HttpRouteCollection,或更改HttpConfiguration的构造函数。

这与HelpController一致,因为它使用以下详细信息:

public HelpController()
    : this(GlobalConfiguration.Configuration)
{
}

public HelpController(HttpConfiguration config)
{
    Configuration = config;
}

0 个答案:

没有答案