ASP.NET Core Dependency Injection
Relevant source files
The following files were used as context for generating this wiki page:
ASP.NET Core Dependency Injection (DI) provides a built-in framework for managing the lifetime and distribution of the Resend SDK components. It allows you to register the Resend client during application startup and inject it into controllers, Razor pages, or background services.
The primary service provided by this library is IResend, which handles all communication with the Resend API. By using DI, you ensure that the HttpClient used by the library is managed correctly, avoiding common issues like socket exhaustion.
Sources: README.md:55-70, examples/WebRazor/Program.cs:18-24
Service Registration
You register Resend services in the Program.cs file using the IServiceCollection. The SDK provides extension methods to simplify the configuration of the API token and the underlying HttpClient.
Configuration Methods
The library supports two main ways to register the services:
- AddResend Extension: This method accepts an action to configure
ResendClientOptions. You typically set theApiTokenhere. - Manual Registration: You can manually configure options, add a typed
HttpClientforResendClient, and mapIResendto the implementation.
// Option 1: Using the simplified extension
builder.Services.AddResend( o =>
{
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!;
} );
// Option 2: Manual registration in DI container
builder.Services.AddOptions();
builder.Services.Configure<ResendClientOptions>( o =>
{
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!;
} );
builder.Services.AddHttpClient<ResendClient>();
builder.Services.AddTransient<IResend, ResendClient>();
Sources: README.md:57-63, examples/WebRazor/Program.cs:18-24
Dependency Lifecycle
The following diagram shows the relationship between the DI container and the Resend components.
The SDK registers IResend as a transient service, while the underlying HttpClient is managed by the IHttpClientFactory to ensure efficient connection pooling.
Sources: examples/WebRazor/Program.cs:22-24, tools/Resend.DocsCheck/Program.cs:247-254
Injecting and Using Services
Once registered, you access the Resend API by requesting the IResend interface in the constructor of your classes.
Constructor Injection
The DI container automatically provides the configured instance of ResendClient whenever IResend is requested. This pattern is used across various application types, including Razor Pages and API controllers.
public class IndexModel : PageModel
{
private readonly IResend _resend;
public IndexModel( IResend resend )
{
_resend = resend;
}
public async Task OnPost()
{
var message = new EmailMessage();
// ... configure message
await _resend.EmailSendAsync( message );
}
}
Sources: examples/WebRazor/Pages/Index.cshtml.cs:15-21, README.md:76-85
Implementation Components
| Component | Responsibility | Lifetime |
|---|---|---|
IResend | Public interface for sending emails and managing resources (domains, API keys). | Transient |
ResendClient | Core implementation that handles HTTP requests and JSON serialization. | Transient |
ResendClientOptions | Holds configuration data such as the API token and base URL. | Singleton/Options |
HttpClient | Provides the transport layer for API calls. | Managed by Factory |
Sources: examples/WebRazor/Program.cs:20-24, src/Resend/ResendClient.Templates.cs:11-15
Data Flow in DI-Enabled Applications
When a service requests IResend, the DI container resolves the dependencies in a specific order to ensure the client is fully functional.
The flow starts with the request for the interface and concludes with the execution of an API call using the pre-configured HTTP client and options.
Sources: examples/WebRazor/Program.cs:18-24, examples/WebRazor/Pages/Index.cshtml.cs:81-91
Summary
The ASP.NET Core Dependency Injection integration for the Resend SDK allows for centralized configuration and efficient resource management. By using the provided extension methods or manual registration, you can seamlessly integrate email capabilities into modern .NET applications while adhering to best practices for service lifetimes and HTTP client usage.