- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
是否可以解析 IOptions<AppSettings>
的实例?来自 ConfigureServices
启动时的方法? The documentation explicitly says :
Don't use
IOptions<TOptions>
orIOptionsMonitor<TOptions>
inStartup.ConfigureServices.
An inconsistent options state may exist due to the ordering of service registrations.
您可以使用 serviceCollection.BuildServiceProvider()
手动创建服务提供者但这会导致警告:
Calling 'BuildServiceProvider' from application code results in an additional copy of singleton services being created. Consider alternatives such as dependency injecting services as parameters to 'Configure'.
我怎样才能做到这一点?
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(
configuration.GetConfigurationSection(nameof(AppSettings)));
// How can I resolve IOptions<AppSettings> here?
}
最佳答案
如果您需要使用服务提供者手动解析服务,您可以使用此 AddSingleton/AddScoped/AddTransient
重载:
// Works for AddScoped and AddTransient as well
services.AddSingleton<IBarService>(sp =>
{
var fooService = sp.GetRequiredService<IFooService>();
return new BarService(fooService);
}
如果您真的想要,您可以使用 IServiceCollection
上的 BuildServiceProvider()
方法构建一个中间服务提供者:
public void ConfigureService(IServiceCollection services)
{
// Configure the services
services.AddTransient<IFooService, FooServiceImpl>();
services.Configure<AppSettings>(configuration.GetSection(nameof(AppSettings)));
// Build an intermediate service provider
var sp = services.BuildServiceProvider();
// Resolve the services from the service provider
var fooService = sp.GetService<IFooService>();
var options = sp.GetService<IOptions<AppSettings>>();
}
为此,您需要 Microsoft.Extensions.DependencyInjection
包。
但是,请注意,这会导致多个服务提供者实例,进而可能导致多个单例实例。
如果只需要在ConfigureServices
中绑定(bind)一些选项,也可以使用Bind
方法:
var appSettings = new AppSettings();
configuration.GetSection(nameof(AppSettings)).Bind(appSettings);
此功能可通过 Microsoft.Extensions.Configuration.Binder
包获得。
关于c# - 如何解析 ConfigureServices 中的 IOptions 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863981/
所以我一直在尝试以 JWT 分发的形式设置一个带有 token 身份验证的小型测试 api, token 分发部分按预期工作。 然而,由于我想让我的 JWT 服务的方法更通用,以允许对 token 进
如果一个类有两个构造函数,当我在 ConfigureServices 中注册该服务时,服务容器如何选择使用哪一个? 所以假设我有一个名为 MyClass 的类(class)有对应的接口(interfa
我正在使用 Auth0 的 SDK 进行身份验证,并在他们的 sample code 中使用,他们在 ConfigureServices() 中创建了一个事件监听器,例如 OnRedirectToId
您将如何从 configureServices 内部访问配置Giraffe-FSharp 中的方法? 这是 SAFE template 创建的 Giraffe 设置的节选部分通过 dotnet new
我在 Startup.cs 中有类似的代码 services.Configure( Configuration.GetSection("AppSettings")); services.Add
我想在 ConfigureServices 中注册一个通用类(需要这个类,因为我想实现模式:存储库和工作单元)以获得依赖注入(inject)。但是我不知道怎么办。 这是我的界面: public int
关注点分离 (SoC) ConfigureServices 中注册的依赖指令(启动类的方法)由不同的 DI 组成,如 Repository、Fluent Validations 等。 我将如何将 DI
我要配置 ASP.NET Core Identity基于驻留在数据库中的设置而不是 AppSetting.json或硬编码值。因此,我很想在方法 ConfigureServices(IServiceC
在我的 ConfigureServices 方法中,我想读取一个文件(在我的例子中是一个用于签署 token 的证书,但它可以是设置服务所需的任何文件)。因此,我需要知道 IApplicationEn
在 ConfigureServices 方法中一次添加 httpContextAccessor 与为每个 HttpClient 配置添加 HttpContextAccessor 之间有什么区别。 pu
我创建了一个记录器,它应该在应用程序运行启动期间记录任何内容。我希望它在 Startup 和 ConfigureServices 之间持续存在。我将它存储在类似于配置的属性中 这是代码 public
我创建了一个记录器,它应该在应用程序运行启动期间记录任何内容。我希望它在 Startup 和 ConfigureServices 之间持续存在。我将它存储在类似于配置的属性中 这是代码 public
我有一个 .Net Core 项目,它注册了一些单例,如下所示: public void ConfigureServices(IServiceCollection services) { se
有时,在服务注册期间,我需要从 DI 容器解析其他(已经注册的)服务。对于像 Autofac 或 DryIoc 这样的容器,这没什么大不了的,因为您可以在一行上注册服务,在下一行上您可以立即解决它。
在我的 ASP.Net Core 应用程序中,我需要在 ConfigureServices 方法中注入(inject)一些依赖项(在我的例子中是一个存储库)。 问题是该方法不允许使用多个参数来注入(i
自 2018 年 5 月 30 日起,我在 Startup.cs 中的 ASP.NET Core 代码 public IServiceProvider ConfigureServices(IServi
我需要在 ASP core 3.0 中使用这个 AutoFac 当我在启动时使用这段代码: public IServiceProvider ConfigureServices(IServiceColl
是否可以解析 IOptions 的实例?来自 ConfigureServices启动时的方法? The documentation explicitly says : Don't use IOptio
我通常会做以下事情 static void Main() { IConfiguration config = new ConfigurationBuilder()
我有一个 ASP.NET Core 3 应用程序,并且正在使用 AzureAD 进行身份验证。我的 Startup.ConfigureSerivces 方法中有以下几行,其目的是在附加 cookie
我是一名优秀的程序员,十分优秀!