ASP.NET 核心 RC2 项目 - 服务文件夹有什么用?
ASP.NET Core RC2 Project -What's a Services folder for?
当您使用 VS2015 创建 ASP.NET Core RC2 项目时,您会得到一个内置的 Services folder
。有人可以提供服务文件夹使用示例的解释吗?或者一些可能有帮助的链接。
也许,您已经阅读了有关此发布候选版本的文档。 https://docs.asp.net/en/latest/fundamentals/dependency-injection.html
ASP.NET Core is designed from the ground up to support and leverage dependency injection. ASP.NET Core applications can leverage built-in framework services by having them injected into methods in the Startup class, and application services can be configured for injection as well. The default services container provided by ASP.NET Core provides a minimal feature set and is not intended to replace other containers.
服务,在此上下文中,指的是一个 class 实例,它为应用程序的其他部分提供一些操作或数据。不要误解,服务不是指网络服务,但它可能是。
Asp.net core集成了IoC容器,可以在启动时设置依赖class.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
Asp.NET 将一些默认的 预打包服务 加载到容器中,并使它们可用于应用程序。如果你想添加自己的服务:
1. 在服务文件夹中创建服务
2. 在 ConfigureServices 上注册创建的服务(之后,asp.net 容器将了解该服务,并可以将该服务的实例注入 Configure 等方法和视图、控制器……)
3. 最后,在 Configure 方法中添加该服务(以便像默认服务一样预先打包它)
当您使用 VS2015 创建 ASP.NET Core RC2 项目时,您会得到一个内置的 Services folder
。有人可以提供服务文件夹使用示例的解释吗?或者一些可能有帮助的链接。
也许,您已经阅读了有关此发布候选版本的文档。 https://docs.asp.net/en/latest/fundamentals/dependency-injection.html
ASP.NET Core is designed from the ground up to support and leverage dependency injection. ASP.NET Core applications can leverage built-in framework services by having them injected into methods in the Startup class, and application services can be configured for injection as well. The default services container provided by ASP.NET Core provides a minimal feature set and is not intended to replace other containers.
服务,在此上下文中,指的是一个 class 实例,它为应用程序的其他部分提供一些操作或数据。不要误解,服务不是指网络服务,但它可能是。
Asp.net core集成了IoC容器,可以在启动时设置依赖class.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
Asp.NET 将一些默认的 预打包服务 加载到容器中,并使它们可用于应用程序。如果你想添加自己的服务: 1. 在服务文件夹中创建服务 2. 在 ConfigureServices 上注册创建的服务(之后,asp.net 容器将了解该服务,并可以将该服务的实例注入 Configure 等方法和视图、控制器……) 3. 最后,在 Configure 方法中添加该服务(以便像默认服务一样预先打包它)