question

wavemaster avatar image
0 Votes"
wavemaster asked ·

How to setup Configuration in Class file?

I would like to move these two "secrets" to secrets.json
@inject IConfiguration config does not work.



 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using Microsoft.Extensions.Configuration;
    
    
    
 /// <summary>
 /// Summary description for TwilioAutomation
 /// </summary>
 namespace MyTwilio
 {
     public class TwilioAutomation
     {
         public static Twilio.TwilioRestClient MySender()
         {
             string sid = "aaaaaa";
             string token = "bbbbbb";
                
             return new Twilio.TwilioRestClient(sid, token);
         }
    
         public class SMS_Message
dotnet-aspnet-core-blazor
· 2
10 |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.

You did not share any relevant code that illustrates how @inject IConfiguration config does not work. The follow example uses the appsettings.json file located in wwwroot of a Blazor WASM application.

 {
   "ApiUrl": {
     "DefaultApi": "https://localhost:44384/"
   },
   "Twilio": {
     "sid": "aaaaaa",
     "token": "bbbbbbb"
   }
 }

Component

 @page "/"
 @inject Microsoft.Extensions.Configuration.IConfiguration config
    
 Welcome to your new app.
    
 <SurveyPrompt Title="How is Blazor working for you?" />
    
 <div>
     @config.GetSection("Twilio:sid").Value
 </div>
 <div>
     @config.GetSection("Twilio:token").Value
 </div>
    
    
 @code {
    
 }


Reference docs


0 Votes 0 ·

I can inject IConfiguration in a razor page, that works, just like your example.

But now I need to do the same in TwilioAutomation.cs, which is a class in my Blazor Server project.

The twilio sid and token are already in secrets.json (accessible through "Manage user secrets")

Just don't know how to access these in a class file.

0 Votes 0 ·

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered ·

Use the following service pattern. The official Core docs cover this service pattern and dependency injection. Again, I'm using the appsettings.json file in wwwroot. You can use whatever file you like. Keep in mind, any custom file must be registered in configuration. See the official docs.

     public interface ITwilioAutomation
     {
         string GetToken();
     }
    
     public class TwilioAutomation : ITwilioAutomation
     {
         private readonly string sid;
         private readonly string token;
         public TwilioAutomation(IConfiguration config)
         {
             sid = config.GetSection("Twilio:sid").Value;
             token = config.GetSection("Twilio:token").Value;
         }
    
         public string GetToken()
         {
             return token;
         }
     }


Register the service in the program.cs file.

 builder.Services.AddScoped<ITwilioAutomation, TwilioAutomation>();

Implementation

 @page "/"
 @inject BlazorWasm.Services.ITwilioAutomation twilio
    
 Welcome to your new app.
    
 <SurveyPrompt Title="How is Blazor working for you?" />
    
 <div>
     @twilio.GetToken()
 </div>
    
 @code {
    
 }


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