Share via

Trying to gain access to ILocalizationService in razor page method

NOVATROOP77 266 Reputation points
Jun 10, 2021, 4:42 PM

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>
 {
  }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,793 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,333 questions
0 comments No comments
{count} votes

Accepted answer
  1. NOVATROOP77 266 Reputation points
    Jun 10, 2021, 8:02 PM

    @AgaveJoe Their is nothing wrong with the design at all and is in fact used by NopComerce please don't comment when You don't understand what trying to receive.

    You can gain access Its very standard practise to use a Razor Page in this way in allot of commercial software.

    I have solved my own issue by using

    var serviceProvider = Context.RequestServices.GetRequiredService<ILocalizationService>();  
    

    So don't need the criticism thanks


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 29,866 Reputation points
    Jun 10, 2021, 6:07 PM

    Your design does not work well with ASP.NET Core DI. Derive an implementation from CellaRazorPage, use constructor injection to resolve the service, then pass the service to the abstract class which is a little clunky. Is there some reason why you can't follow a standard service patterns and use an interface rather than an abstract class?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.