question

davidbuckley-4911 avatar image
0 Votes"
davidbuckley-4911 asked davidbuckley-4911 commented

Trying to gain access to ILocalizationService in razor page method

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>
  {
   }



dotnet-csharpdotnet-aspnet-core-mvcdotnet-aspnet-core-razor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

davidbuckley-4911 avatar image
0 Votes"
davidbuckley-4911 answered davidbuckley-4911 commented

@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


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

But. that's not what you asked. You asked how to fetch the service from an abstract class which is not possible. Anyway, glad you found the solution.

1 Vote 1 ·

It is by the above method !

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.