【发布时间】:2016-06-08 14:58:51
【问题描述】:
当你使用 VS2015 创建一个 ASP.NET Core RC2 项目时,你会得到一个内置的Services folder。有人可以提供服务文件夹使用示例的解释。或者一些可能有帮助的链接。
【问题讨论】:
标签: asp.net asp.net-core asp.net-core-mvc .net-core
当你使用 VS2015 创建一个 ASP.NET Core RC2 项目时,你会得到一个内置的Services folder。有人可以提供服务文件夹使用示例的解释。或者一些可能有帮助的链接。
【问题讨论】:
标签: asp.net asp.net-core asp.net-core-mvc .net-core
也许,您已经阅读了有关此发布候选版本的文档。 https://docs.asp.net/en/latest/fundamentals/dependency-injection.html
ASP.NET Core 的设计初衷就是支持和利用依赖注入。 ASP.NET Core 应用程序可以通过将内置框架服务注入到 Startup 类的方法中来利用它们,并且应用程序服务也可以配置为注入。 ASP.NET Core 提供的默认服务容器提供了最小的功能集,并不打算替换其他容器。
服务,在这种情况下,是指一个类实例,它为应用程序的其他部分提供一些操作或数据。不要误会,服务不是指网络服务,但它可能是。
Asp.net core 有一个集成的 IoC 容器,你可以在你的启动类中设置依赖。
// 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>();
}
【讨论】:
retrieve data from a SQL database 并将其显示在 ASP.NET Core 的下拉列表中?
Asp.NET 将一些默认的预打包服务 加载到容器中,并使它们可用于应用程序。如果您想添加自己的服务: 1. 您在 Services 文件夹中创建服务 2. 在 ConfigureServices 上注册创建的服务(之后,asp.net 容器将知道该服务,并可以将该服务的实例注入到 Configure 和 Views、Controllers 等方法中……) 3. 最后,在 Configure 方法 中添加该服务(以便像默认服务一样对其进行预打包)
【讨论】: