Extension methods for adding a BlobServiceClient are provided by Microsoft as previously mentioned, however there’s no equivlent for adding access for Table (or Cosmos DB) storage.

The below extension allows CloudTable and CloudTableClients to be injected. Though as Table storage doesn’t yet work with Managed Service Identity the relevant SAS key or connection string needs to be passed through.

CloudTableBuilderExtensions.cs

/// <summary>
/// Extension methods to add <see cref="CloudTableClient"/> and <see cref="CloudTable"/> instances to clients builder
/// </summary>
public static class CloudTableBuilderExtensions
{
	/// <summary>
	/// Registers a <see cref="CloudTableClient"/> instance with the provided <paramref name="connectionString"/>
	/// </summary>
	/// <param name="connectionString">Master connection string for accessing the storage account</param>
	/// <param name="setupAction">Configuration options</param>
	/// <returns></returns>
	public static IAzureClientBuilder<CloudTableClient, FoodOptions> AddCloudTableClient<TBuilder>(this TBuilder builder, string connectionString, Action<FoodOptions> setupAction)
		where TBuilder : IAzureClientFactoryBuilder
	{
		if (setupAction == null) throw new ArgumentNullException(nameof(setupAction));

		return builder.RegisterClientFactory<CloudTableClient, FoodOptions>(options => CloudStorageAccount.Parse(connectionString).CreateCloudTableClient());
	}

	/// <summary>
	/// Registers a <see cref="CloudTableClient"/> instance with the provided <paramref name="connectionString"/>
	/// </summary>
	/// <param name="connectionString">Master connection string for accessing the storage account</param>
	/// <returns></returns>
	public static IAzureClientBuilder<CloudTableClient, FoodOptions> AddCloudTableClient<TBuilder>(this TBuilder builder, string connectionString)
		where TBuilder : IAzureClientFactoryBuilder
	{
		return AddCloudTableClient(builder, connectionString, options => { });
	}

	/// <summary>
	/// Registers a <see cref="CloudTable"/> instance
	/// </summary>
	/// <param name="storageUri">Base URI of the table storage container, e.g. https://mycontainer.table.core.windows.net</param>
	/// <param name="sasToken">SAS token scoped to the table to be accessed</param>
	/// <param name="tableName">Name of the table to be accessed</param>
	/// <param name="setupAction">Configuration options</param>
	/// <returns></returns>
	public static IAzureClientBuilder<CloudTable, FoodOptions> AddCloudTable<TBuilder>(this TBuilder builder, Uri storageUri, string sasToken, string tableName, Action<FoodOptions> setupAction)
		where TBuilder : IAzureClientFactoryBuilder
	{
		if (setupAction == null) throw new ArgumentNullException(nameof(setupAction));

		CloudTable cloudTableClient = new CloudTableClient(storageUri, new StorageCredentials(sasToken)).GetTableReference(tableName);
		return builder.RegisterClientFactory<CloudTable, FoodOptions>(options => new CloudTableClient(storageUri, new StorageCredentials(sasToken)).GetTableReference(tableName));
	}

	/// <summary>
	/// Registers a <see cref="CloudTable"/> instance
	/// </summary>
	/// <param name="storageUri">Base URI of the table storage container, e.g. https://mycontainer.table.core.windows.net</param>
	/// <param name="sasToken">SAS token scoped to the table to be accessed</param>
	/// <param name="tableName">Name of the table to be accessed</param>
	/// <returns></returns>
	public static IAzureClientBuilder<CloudTable, FoodOptions> AddCloudTable<TBuilder>(this TBuilder builder, Uri storageUri, string sasToken, string tableName)
		where TBuilder : IAzureClientFactoryBuilder
	{
		return AddCloudTable(builder, storageUri, sasToken, tableName, options => { });
	}
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
	services.AddControllersWithViews();

	// Add blob storage
	services.AddAzureClients(builder =>
	{
		// Add a storage account client
		builder.AddBlobServiceClient(new Uri(Configuration["BlobStorage:StorageUrl"]));

		// Add a cloud table client
		builder.AddCloudTableClient(Configuration.GetConnectionString("TableStorage"), options =>
		{
			options.TableName = Configuration["TableStorage:TableName"];
		});

		// Add a cloud table
		builder.AddCloudTable(
			new Uri(Configuration["TableStorage:StorageUrl"]),
			Configuration["TableStorage:SasToken"],
			Configuration["TableStorage:TableName"]
			);

		// Use the environment credential by default
		builder.UseCredential(new DefaultAzureCredential());
	});

	services
		.AddRazorPages()
		.AddViewLocalization();
}

Leave a Reply

Your email address will not be published. Required fields are marked *