相关文章推荐
长情的豆腐  ·  410 Gone - HTTP | MDN·  1 月前    · 
绅士的书包  ·  403 Forbidden - HTTP ...·  6 天前    · 
豁达的匕首  ·  java ...·  1 年前    · 
重感情的大象  ·  和我一起学 ...·  1 年前    · 

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

@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

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?