How can I add service and get setting from appsetting.json in razor class library(RCL)?

melon NG 291 Reputation points
2022-08-31T13:05:00.343+00:00

I need to add Google reCAPTCHA to my site and I achieve this by Unicorn.reCAPTCHA.AspNetCore(https://www.nuget.org/packages/Unicorn.reCAPTCHA.AspNetCore/2.2.1).

There are some sub-domain sites also. I coded an RCL to share their header/footer and some codes of reCAPTCHA.

Before the reCAPTCHA runs, it should add the service in startup.cs like this:
services.AddGoogleRecaptcha(Configuration.GetSection("RecaptchaSettings"));

And the Section in appsetting.json is just like this:
"RecaptchaSettings": {
"SiteKey": "aaaaa",
"SecretKey": "bbbb",
"Version": "v3",
"Domain": "www.recaptcha.net"
}

Now I have to add the codes above for setting the reCAPTCHA in every appsetting.json and starup.cs of the sites, and all the codes are the same, it is so troublesome.

Is there any way for me to get the setting in appsetting.json only once and add the service in RCL? Thank you.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,234 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,731 Reputation points
    2022-08-31T22:46:16.723+00:00

    normally you would supply a IServiceCollection extension helper that added the configuration to the Services for DI.:

    services.AddReCAPTCHA(config);

    simple implementation

     public static IServiceCollection AddReCaptcha(this IServiceCollection services, IConfiguration config, string sectionName = "reCaptchaOptions")  
    {  
         if (!services.Any(x => x.ServiceType == typeof(ReCaptchaOptions)))  
         {  
              var ReCaptchaOptions = new ReCaptchaOptions();  
              Configuration.GetSection(sectionName).Bind(ReCaptchaOptions);   
              services.AddSingleton<ReCaptchaOptions>(ReCaptchaOptions);  
         }  
      
         return services;  
     }  
    

  2. AgaveJoe 26,161 Reputation points
    2022-09-04T22:02:45.573+00:00

    Again, RCL is just a class library and configuration can be injected into any constructor including a class library. The pattern in the link you provided is used in a service pattern. You have not provided any code that shows your service which makes providing an accurate solution difficult at best.

    however, I created an example of fetching configuration in a RCL Razor page using the official RCL and Option pattern documentation.

    Create reusable UI using the Razor class library project in ASP.NET Core
    Bind hierarchical configuration data using the options pattern

    RCL Solution Explorer

    237676-capture.png

    Option model

        public class RecaptchaSettingsOptions  
        {  
            public const string RecaptchaSettings = "RecaptchaSettings";  
            public string SiteKey { get; set; } = string.Empty;  
            public string SecretKey { get; set; } = string.Empty;  
            public string Version { get; set; } = string.Empty;  
            public string Domain { get; set; } = string.Empty;  
        }  
    

    Page1.cshtml

    @page  
    @model RazorClassLib.MyFeature.Pages.Page1Model  
      
    <!DOCTYPE html>  
      
    <html>  
    <head>  
        <meta name="viewport" content="width=device-width" />  
        <title>Page1</title>  
    </head>  
    <body>  
        <div>Hello World</div>  
        <div>SiteKey = @Model.Options.SiteKey</div>  
        <div>SecretKey = @Model.Options.SecretKey</div>  
        <div>Version = @Model.Options.Version</div>  
        <div>SiteKey = @Model.Options.Domain</div>  
    </body>  
    </html>  
    

    Page1.cshtml.cs

    using Microsoft.AspNetCore.Mvc;  
    using Microsoft.AspNetCore.Mvc.RazorPages;  
    using Microsoft.Extensions.Configuration;  
    using Microsoft.Extensions.Options;  
    using RazorClassLib.Options;  
      
    namespace RazorClassLib.MyFeature.Pages;  
      
    public class Page1Model : PageModel  
    {  
      
        private readonly IConfiguration _configuration;  
        public Page1Model(IConfiguration configuration)  
        {  
            _configuration = configuration;  
        }  
        public void OnGet()  
        {  
            Options = new RecaptchaSettingsOptions();  
            _configuration.GetSection(RecaptchaSettingsOptions.RecaptchaSettings).Bind(Options);  
        }  
      
        public RecaptchaSettingsOptions Options { get; set; }  
    }  
    

    Results of https://localhost:7143/myfeature/page1

    Hello World  
    SiteKey = aaaaa  
    SecretKey = bbbb  
    Version = v3  
    SiteKey = www.recaptcha.net