【问题标题】:Unable to resolve options in startup.cs with non core project无法使用非核心项目解析 startup.cs 中的选项
【发布时间】:2018-10-02 13:57:47
【问题描述】:

我有asp net webapi,我需要解决启动文件中的一些选项。这里的选项有一个简单的类,它从 json 配置中获取参数。在启动时注册 optionsAutofacModule where register configuration section 我需要什么。示例:OptionAutofacModule 中的RegisterConfigurationOptions<IdentitySettings>(builder, configuration.GetSection("Identity"));,加上在启动.cs 中调用的containerBuilder.RegisterModule<OptionsAutofacModule>();。所以,我需要访问startup.cs中方法内的“identitysettings”。

编辑 0:我有 net core 项目和经典的 asp net web api。 Net core 项目使用配置,从 json 文件解析什么。我可以使用 IOptions 在控制器经典 .net webapi 项目中访问此配置。但我无法在 startup.cs 中访问此选项

【问题讨论】:

    标签: c# asp.net-mvc-4 startup options autofac-module


    【解决方案1】:

    理论上,解决方案很简单,但我忘记了 beginscope。解决方案:

    config.DependencyResolver
          .BeginScope()
          .GetService(typeof(IOptions<YourOptions>)) as IOptions<YourOptions>;
    

    config 是 HttpConfiguration

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      相关资源
      最近更新 更多