I'm migrating my site, from asp.net 4.5 to 5.0. Previously, I used System.Web.Mvc
to get DependencyResolver.SetResolve
to register dependencies. Is there any publication or documentation that can point me to the new Microsoft.AspNet.Mvc
version?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
http://docs.asp.net/en/latest/fundamentals/dependency-injection.html
That's official documentation for dependency injection for asp.net 5.
Dependency injection is now built into asp.net 5 but you are free to use other libraries like autofac. The default one is working fine for me.
In your starup class, you have a method like this
public void ConfigureServices(IServiceCollection services)
{
//IServiceCollection acts like a container and you
//can register your classes like this:
services.AddTransient<IEmailSender, AuthMessageSender>();
services.Singleton<ISmsSender, AuthMessageSender>();
services.AddScoped<ICharacterRepository, CharacterRepository>();
}
These are some Service Lifetimes and Registration Options