DependencyResolver Asp.net 5.0

2019-09-07 16:07发布

问题:

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