dotnet add package Soenneker.Twilio.RestClient
This library provides a singleton of a TwilioRestClient
.
Internally it implements an HttpClient
singleton. This HttpClient
has less overhead than new instances of HttpClient
and IHttpClientFactory
all while correctly handling connection pooling for DNS changes.
See soenneker.utils.httpclientcache for more information.
- Register
ITwilioRestClientUtil
with DI.
public static async Task Main(string[] args)
{
...
builder.Services.AddTwilioRestClientUtilAsSingleton();
}
- Inject
ITwilioRestClientUtil
via constructor, and retrieve aTwilioRestClient
.
public class TestClass
{
ITwilioRestClientUtil _twilioRestClientUtil;
public TestClass(ITwilioRestClientUtil twilioRestClientUtil)
{
_twilioRestClientUtil = twilioRestClientUtil;
}
public async ValueTask SendMessage()
{
var message = await MessageResource.CreateAsync(
new PhoneNumber("+11234567890"),
from: new PhoneNumber("+10987654321"),
body: "Hello World!",
client: await _twilioRestClientUtil.Get()
);
Console.WriteLine(message.Sid);
}