I am trying to access my service in a razor page code behind method I am creating a localizer method of type T that will go out and get my resource strings from the database.
public abstract class CellaRazorPage<TModel> : Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>
{
private ILocalizationService _localizationService;
private Localizer _localizer;
private IServiceProvider provider = null;
/// <summary>
/// Get a localized resources
/// </summary>
public Localizer T
{
get
{
ILocalizationService helper = (ILocalizationService)
HttpContext.RequestServices.GetService
(typeof(ILocalizationService));
if (_localizer == null)
{
_localizer = (format, args) =>
{
var resFormat = helper.GetResourceAsync(format, 1).Result;
if (string.IsNullOrEmpty(resFormat))
{
return new LocalizedString(format);
}
return new LocalizedString((args == null || args.Length == 0)
? resFormat
: string.Format(resFormat, args));
};
}
return _localizer;
}
}
}
However this line is not working how would i gain access to the Service so that I can call my method which is called GetResourceAsync
helper where am trying to get the service from the context what should I do here instead.
ILocalizationService helper = (ILocalizationService)
HttpContext.RequestServices.GetService
(typeof(ILocalizationService));
public class LocalizationService : ILocalizationService
{
public virtual async Task<string> GetResourceAsync(string resourceKey,int langugeID)
{
return _context.LocaleStringResource.Where(w => w.ResourceName == resourceKey && w.LanguageId == langugeID && w.isDeleted == false && w.isActive == true).FirstOrDefaultAsync().Result.ResourceValue;
}
}
Oh and I cannot use a constructor for when I use this line it complains and says its not possible because its expecting the service.
/ // <summary>
/// Web view page
/// </summary>
public abstract class CellaRazorPage : CellaRazorPage<dynamic>
{
}