【问题标题】:How to inject an IStringLocalizer into IApplicationModelConvention?如何将 IStringLocalizer 注入 IApplicationModelConvention?
【发布时间】:2019-05-21 01:12:07
【问题描述】:

我有一个自定义约定,需要在其中包含本地化字符串。 AFAIK 实例化 IStringLocalizer 的唯一方法是 DependencyInjection。

如何在我的 CustomConvention 中使用 IStringLocalizer?

约定是这样注册的

public void ConfigureServices(IServiceCollection services)
{
     //First I register the localization settings
     services.AddLocalization(o =>
     {
        o.ResourcesPath = "Resources";
     });

      services.AddMvc(options =>
      {
           //My custom convention, I would need to inject an IStringLocalizer  
           //into constructor here, but I can' instantiate it
           options.Conventions.Add(new LocalizationRouteConvention());
      })
}

【问题讨论】:

    标签: asp.net-mvc asp.net-core .net-core


    【解决方案1】:

    我的解决方案并不漂亮。

    您可以这样做,构建服务提供者以获取 StringLocalizer 的实例。

    public void ConfigureServices(IServiceCollection services)
    {
         //First I register the localization settings
         services.AddLocalization(o =>
         {
            o.ResourcesPath = "Resources";
         });
    
         var stringLocalizer = services.BuildServiceProvider().GetService<IStringLocalizer<Resource>>();
    
         services.AddMvc(options =>
         {
              //My custom convention, I would need to inject an IStringLocalizer  
              //into constructor here, but I can' instantiate it
              options.Conventions.Add(new LocalizationRouteConvention(stringLocalizer));
         })
    }
    

    【讨论】:

      【解决方案2】:

      我会建议这里提供的方法https://stackoverflow.com/a/61958762/4627333

      您基本上只需要注册您的类型并创建一个 IConfigureOptions&lt;MvcOptions&gt; 的实现,期待您的约定实现。 DI 将完成剩下的工作。

      public class LocalizationRouteConventionMvcOptions : IConfigureOptions<MvcOptions>
      {
          private readonly LocalizationRouteConvention _convention;
      
          public MyMvcOptions(LocalizationRouteConvention convention)
              => _convention = convention;
      
          public void Configure(MvcOptions options)
              => options.Conventions.Add(_convention);
      }
      
      public void ConfigureServices(IServiceCollection services)
      {
          // or scoped, or transient, as necessary for your service
          services.AddTransient<IStringLocalizer<Resource>, MyStringLocalizer>();
          services.AddSingleton<LocalizationRouteConvention>();
          services.AddSingleton<IConfigureOptions<MvcOptions>, LocalizationRouteConventionMvcOptions>();
      
          services.AddControllers();
      }
      

      您的LocalizationRouteConvention 代码:

      public sealed class LocalizationRouteConvention : IApplicationModelConvention
      {
          private readonly IStringLocalizer<Resource> _stringLocalizer;
      
          public LocalizationRouteConvention(IStringLocalizer<Resource> stringLocalizer)
          {
              _stringLocalizer = stringLocalizer ?? throw new ArgumentNullException(nameof(stringLocalizer));
          }
      
          public void Apply(ApplicationModel application)
          {
              // ...
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-15
        • 1970-01-01
        • 2017-05-17
        • 2012-12-15
        • 2020-01-02
        • 2017-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多