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