標籤:addm *** core sof ansi 依賴 net ons www
ASP.NET Core 源碼閱讀筆記(1) ---Microsoft.Extensions.DependencyInjection
在asp .net中使用依賴注入很簡單,只需要在Startup類的ConfigureServices(IServiceCollection services)方法中,通過IServiceCollection介面注入就行。
通過介面的Add*方法進行註冊,代碼如下:
ASP.NET Core runtime幫忙註冊如下方法
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { services.AddEntityFrameworkSqlServer().AddDbContext<**Context>(options => options.UseSqlServer(Configuration.GetConnectionString("***"))); //services.AddAuthorization(options => { options.DefaultPolicy }); // Add framework services. services.AddOptions(); services.AddDistributedMemoryCache(); services.AddMvc(); services.AddScoped<MenuService>(); services.AddTransient<ICommon, Common>(); services.AddSingleton<IConfiguration>(Configuration); }
.Net Core 使用依賴注入