Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't instantiate implementation type

I am trying to work with the RequestCultureProvider (essentially trying to find the UI Culture of the current request from within a user class) in ASP.NET Core 1.1

I have the following code in my Startup.cs under ConfigureServices:

services.AddSingleton<IRequestCultureProvider, RequestCultureProvider>();

The following error is being thrown at runtime:

Cannot instantiate implementation type 'Microsoft.AspNetCore.Localization.RequestCultureProvider' for service type 'Microsoft.AspNetCore.Localization.IRequestCultureProvider

How do I get around this? Or is there another method for getting the CurrentUICulture from within a user class.

Concrete examples appreciated.

like image 752
eat-sleep-code Avatar asked Dec 10 '22 12:12

eat-sleep-code


1 Answers

Microsoft.AspNetCore.Localization.RequestCultureProvider is an abstract class, so it cannot be instantiated like this.

I believe the intention of the framework is for you to use Features not Services:

var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

In conjunction with adding the RequestLocalizationMiddleware that sets the feature.

RequestCultureProvider is used for configuring RequestLocalizationOptions that is used by RequestLocalizeationMiddleware (using DI for IOptions<RequestLocalizationOptions>), so it isn't intended to be resolved as a service on its own

like image 65
Tim Avatar answered Dec 29 '22 00:12

Tim