# AspireMySqlConnectorExtensions Methods

- Package: [Aspire.MySqlConnector](/reference/api/csharp/aspire.mysqlconnector.md)
- Type: [AspireMySqlConnectorExtensions](/reference/api/csharp/aspire.mysqlconnector/aspiremysqlconnectorextensions.md)
- Kind: `Methods`
- Members: `2`

Extension methods for connecting MySQL database with MySqlConnector client

## AddKeyedMySqlDataSource(IHostApplicationBuilder, string, Action<MySqlConnectorSettings>)

- Name: `AddKeyedMySqlDataSource(IHostApplicationBuilder, string, Action<MySqlConnectorSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.MySqlConnector/AspireMySqlConnectorExtensions.cs#L48-L51)

Registers `MySqlDataSource` as a keyed service for given `name` for connecting MySQL database with MySqlConnector client. Configures health check, logging and telemetry for the MySqlConnector client.

```csharp
public static class AspireMySqlConnectorExtensions
{
    public static void AddKeyedMySqlDataSource(
        this IHostApplicationBuilder builder,
        string name,
        Action<MySqlConnectorSettings>? configureSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `name` (`string`)
  The name of the component, which is used as the `ServiceDescriptor.ServiceKey` of the service and also to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MySqlConnectorSettings>`) `optional`
  An optional method that can be used for customizing options. It's invoked after the settings are read from the configuration.

## Exceptions

- `ArgumentNullException` -- Thrown when `builder` or `name` is null.
- `ArgumentException` -- Thrown if mandatory `name` is empty.
- `InvalidOperationException` -- Thrown when mandatory [MySqlConnectorSettings.ConnectionString](/reference/api/csharp/aspire.mysqlconnector/mysqlconnectorsettings/properties.md#connectionstring) is not provided.

## Remarks

Reads the configuration from "Aspire:MySqlConnector:{name}" section.

## AddMySqlDataSource(IHostApplicationBuilder, string, Action<MySqlConnectorSettings>)

- Name: `AddMySqlDataSource(IHostApplicationBuilder, string, Action<MySqlConnectorSettings>)`
- Modifiers: `extension`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Components/Aspire.MySqlConnector/AspireMySqlConnectorExtensions.cs#L33)

Registers `MySqlDataSource` service for connecting MySQL database with MySqlConnector client. Configures health check, logging and telemetry for the MySqlConnector client.

```csharp
public static class AspireMySqlConnectorExtensions
{
    public static void AddMySqlDataSource(
        this IHostApplicationBuilder builder,
        string connectionName,
        Action<MySqlConnectorSettings>? configureSettings = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IHostApplicationBuilder`)
  The `Hosting.IHostApplicationBuilder` to read config from and add services to.
- `connectionName` (`string`)
  A name used to retrieve the connection string from the ConnectionStrings configuration section.
- `configureSettings` (`Action<MySqlConnectorSettings>`) `optional`
  An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.

## Exceptions

- `ArgumentNullException` -- Thrown if mandatory `builder` is null.
- `InvalidOperationException` -- Thrown when mandatory [MySqlConnectorSettings.ConnectionString](/reference/api/csharp/aspire.mysqlconnector/mysqlconnectorsettings/properties.md#connectionstring) is not provided.

## Remarks

Reads the configuration from "Aspire:MySqlConnector" section.
