Skip to content

Commit 68a8243

Browse files
committed
Use Endpoint for connection string
1 parent 7013a3d commit 68a8243

File tree

4 files changed

+23
-35
lines changed

4 files changed

+23
-35
lines changed

Aspire.sln

-2
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Azure.Npgsql.EntityF
661661
EndProject
662662
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Components.Common.Tests", "tests\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj", "{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}"
663663
EndProject
664-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}"
665-
EndProject
666664
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Hosting.Azure.ContainerRegistry", "src\Aspire.Hosting.Azure.ContainerRegistry\Aspire.Hosting.Azure.ContainerRegistry.csproj", "{6CBA29C8-FF78-4ABC-BEFA-2A53CB4DB2A3}"
667665
EndProject
668666
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Hosting.Azure.AppService", "src\Aspire.Hosting.Azure.AppService\Aspire.Hosting.Azure.AppService.csproj", "{5DDF8E89-FBBD-4A6F-BF32-7D2140724941}"

src/Aspire.Hosting.Azure.Storage/AzureBlobStorageResource.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class AzureBlobStorageResource(string name, AzureStorageResource storage)
1616
IResourceWithParent<AzureStorageResource>,
1717
IResourceWithAzureFunctionsConfig
1818
{
19+
// NOTE: if ever these contants are changed, the AzureBlobStorageContainerSettings in Aspire.Azure.Storage.Blobs class should be updated as well.
20+
private const string Endpoint = nameof(Endpoint);
21+
private const string ContainerName = nameof(ContainerName);
22+
1923
/// <summary>
2024
/// Gets the parent AzureStorageResource of this AzureBlobStorageResource.
2125
/// </summary>
@@ -34,12 +38,12 @@ internal ReferenceExpression GetConnectionString(string? blobContainerName)
3438
return ConnectionStringExpression;
3539
}
3640

37-
var builder = new ReferenceExpressionBuilder();
38-
builder.Append($"{ConnectionStringExpression}");
41+
ReferenceExpressionBuilder builder = new();
42+
builder.Append($"{Endpoint}=\"{ConnectionStringExpression}\";");
3943

4044
if (!string.IsNullOrEmpty(blobContainerName))
4145
{
42-
builder.Append($"ContainerName={blobContainerName};");
46+
builder.Append($"{ContainerName}={blobContainerName};");
4347
}
4448

4549
return builder.Build();

src/Aspire.Hosting.Azure.Storage/AzureStorageEmulatorConnectionString.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ internal static class AzureStorageEmulatorConnectionString
1010
// Use defaults from https://learn.microsoft.com/azure/storage/common/storage-configure-connection-string#connect-to-the-emulator-account-using-the-shortcut
1111
private const string ConnectionStringHeader = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;";
1212

13-
private static void AppendEndpointExpression(ReferenceExpressionBuilder builder, string key, EndpointReference endpoint)
14-
{
15-
builder.Append($"{key}=http://{endpoint.Property(EndpointProperty.IPV4Host)}:{endpoint.Property(EndpointProperty.Port)}/devstoreaccount1;");
16-
}
17-
1813
public static ReferenceExpression Create(EndpointReference? blobEndpoint = null, EndpointReference? queueEndpoint = null, EndpointReference? tableEndpoint = null)
1914
{
2015
var builder = new ReferenceExpressionBuilder();
@@ -34,5 +29,10 @@ public static ReferenceExpression Create(EndpointReference? blobEndpoint = null,
3429
}
3530

3631
return builder.Build();
32+
33+
static void AppendEndpointExpression(ReferenceExpressionBuilder builder, string key, EndpointReference endpoint)
34+
{
35+
builder.Append($"{key}=http://{endpoint.Property(EndpointProperty.IPV4Host)}:{endpoint.Property(EndpointProperty.Port)}/devstoreaccount1;");
36+
}
3737
}
3838
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Data.Common;
45
using System.Text.RegularExpressions;
56
using Aspire.Azure.Common;
67

@@ -11,9 +12,9 @@ namespace Aspire.Azure.Storage.Blobs;
1112
/// </summary>
1213
public sealed partial class AzureBlobStorageContainerSettings : AzureStorageBlobsSettings, IConnectionStringSettings
1314
{
14-
[GeneratedRegex(@"ContainerName=([^;]*);")]
15-
private static partial Regex ExtractContainerName();
16-
15+
/// <summary>
16+
/// Gets or sets the name of the blob container.
17+
/// </summary>
1718
public string? BlobContainerName { get; set; }
1819

1920
void IConnectionStringSettings.ParseConnectionString(string? connectionString)
@@ -23,30 +24,15 @@ void IConnectionStringSettings.ParseConnectionString(string? connectionString)
2324
return;
2425
}
2526

26-
// In the emulator mode, the connection string may look like:
27-
//
28-
// DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=...;BlobEndpoint=http://127.0.0.1:5555/devstoreaccount1;ContainerName=<container_name>;
29-
//
30-
// When run against the real Azure resources, the connection string will look similar to:
31-
//
32-
// https://<storage_name>.blob.core.windows.net/ContainerName=<container_name>;
33-
//
34-
// Retrieve the container name from the connection string, if it is present; and then
35-
// remove it as it will upset BlobServiceClient.
36-
37-
if (ExtractContainerName().Match(connectionString) is var match)
38-
{
39-
BlobContainerName = match.Groups[1].Value;
40-
connectionString = connectionString.Replace(match.Value, string.Empty);
41-
}
27+
// NOTE: if ever these contants are changed, the AzureBlobStorageResource in Aspire.Hosting.Azure.Storage class should be updated as well.
28+
const string Endpoint = nameof(Endpoint);
29+
const string ContainerName = nameof(ContainerName);
4230

43-
if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri))
44-
{
45-
ServiceUri = uri;
46-
}
47-
else
31+
DbConnectionStringBuilder builder = new() { ConnectionString = connectionString };
32+
if (builder.TryGetValue(Endpoint, out var endpoint) && builder.TryGetValue(ContainerName, out var containerName))
4833
{
49-
ConnectionString = connectionString;
34+
ConnectionString = endpoint.ToString();
35+
BlobContainerName = containerName.ToString();
5036
}
5137
}
5238
}

0 commit comments

Comments
 (0)