要獲得本地化的ASP.NET核心工作,你將不得不調用從AddLocalization()
您ConfigureServices
方法中:
public void ConfigureServices(IServiceCollection services)
{
// Add localization
services.AddLocalization();
// Normal MVC stuff
services.AddMvc();
// Simple Injector integration
IntegrationSimpleInjector(services);
}
private void IntegrationSimpleInjector(IServiceCollection services)
{
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IControllerActivator>(
new SimpleInjectorControllerActivator(container));
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);
}
但是,這隻允許IStringLocalizer<T>
從ASP.NET Core配置系統解析,而不是從Simple Injector解析。爲了達到此目的,您必須將IStringLocalizer<T>
交叉連接到Simple Injector容器。這通常意味着調用container.CrossWire<T>(app)
,但由於IStringLocalizer<T>
是通用的,因此您必須定義一個代理類,以將調用轉發給從Core配置系統解析出的類型。這種類型可能如下:
class StringLocalizerProxy<T> : IStringLocalizer<T>
{
private readonly IServiceScope scope;
public StringLocalizerProxy(IServiceScope scope) => this.scope = scope;
public LocalizedString this[string name] => Instance[name];
public LocalizedString this[string name, params object[] a] => Instance[name, a];
public IEnumerable<LocalizedString> GetAllStrings(bool c) => Instance.GetAllStrings(c);
public IStringLocalizer WithCulture(CultureInfo c) => Instance.WithCulture(c);
private IStringLocalizer<T> Instance =>
scope.ServiceProvider.GetRequiredService<IStringLocalizer<T>>();
}
可以按如下這個代理添加到簡單的注射器:
var scopeFactory = builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
container.Register<IServiceScope>(scopeFactory.CreateScope, Lifestyle.Scoped);
container.Register(typeof(IStringLocalizer<>), typeof(StringLocalizerProxy<>),
Lifestyle.Scoped);
有了這個代理和之前的註冊,你將能夠注入IStringLocalizer<T>
實例到您的應用組件。例如:
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
this.localizer = localizer;
}
}
謝謝@Steven,這一切都有道理但是我得到以下有關IServiceScope未被註冊的錯誤? ActivationException:類型StringLocalizerProxy的構造函數包含名稱爲'scope'且類型爲IServiceScope的參數未註冊。請確保已註冊IServiceScope,或更改StringLocalizerProxy 的構造函數。 –
你打電話給'EnableSimpleInjectorCrossWiring'嗎? – Steven
是的,我做過了 - 是否有可能使用IMapper自動映射器導致問題。我從來沒有使用過EnableSimpleInjectorCrossWiring並使用IMapper,然後不得不更新簡單的注入器來獲取EnableSimpleInjectorCrossWiring –