ASP.Net Core 中startup 类的configservice方法的作用?

面试必备:ASP.Net Core 中startup 类的configservice方法的作用?

简述:

ConfigureServices ,就是配置服务器的DI容器,可以添加一些service进入依赖注入容器。

详解:

把需要的中间件等一些东西添加到DI容器 最后都是添加到IServiceCollection里面

比如

c# 复制代码
services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddInMemoryApiResources(Config.GetResource())
    .AddInMemoryClients(Config.GetClients())
    .AddTestUsers(Config.GetTestUsers())
    .AddProfileService<ProfileService>()
    .AddResourceOwnerValidator<LoginValidator>();

对于.AddProfileService() 已经内置了一个默认实现IProfileService接口的一个类 默认会注入内置的(DefaultProfileServer)

这样写了后 其实里面的实现就是 遇到IProfileService 实例化成自定义的类ProfileService 不使用内置的。

启动时服务:

ASP.NET Core依赖注入在应用程序启动期间提供服务。您可以通过在Startup类的构造方法或其Configure方法中包含适当的接口作为参数来请求这些服务。 ConfigureServices方法只接受一个IServiceCollection参数(但是可以从这个集合中检索任何已注册的服务,所以不需要额外的参数)。

下面是一些通常由启动方法请求的服务:

  • 在构造方法中:IHostingEnvironment,ILogger<Startup>
  • ConfigureServices方法中:IServiceCollection
  • Configure方法中:IApplicationBuilder, IHostingEnvironment, ILoggerFactory

Startup类构造方法或其Configure方法可以请求由WebHostBuilder ConfigureServices方法添加的任何服务。使用WebHostBuilder在启动方法中提供您需要的任何服务。

相关推荐
有来技术7 小时前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5168 小时前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
三水不滴8 小时前
Redis缓存更新策略
数据库·经验分享·redis·笔记·后端·缓存
小邓吖9 小时前
自己做了一个工具网站
前端·分布式·后端·中间件·架构·golang
大爱编程♡10 小时前
SpringBoot统一功能处理
java·spring boot·后端
好好研究12 小时前
总结SSM设置欢迎页的方式
xml·java·后端·mvc
小马爱打代码13 小时前
Spring Boot:第三方 API 调用的企业级容错设计
java·spring boot·后端
csdn2015_14 小时前
springboot task
java·spring boot·后端
czlczl2002092514 小时前
Spring Boot :如何高性能地在 Filter 中获取响应体(Response Body)
java·spring boot·后端
码界奇点15 小时前
基于Spring Boot和Vue3的无头内容管理系统设计与实现
java·spring boot·后端·vue·毕业设计·源代码管理