Skip to content

Commit acfe6de

Browse files
committed
Implement the functions interface on storage as well
1 parent 5b63592 commit acfe6de

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/Aspire.Hosting.Azure.Functions/AzureFunctionsProjectResourceExtensions.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class AzureFunctionsProjectResourceExtensions
3030
builder.Eventing.Subscribe<BeforeStartEvent>((data, token) =>
3131
{
3232
var removeStorage = true;
33-
// Look at all of the resources and if non of them use the default storage, then we can remove it.
33+
// Look at all of the resources and if none of them use the default storage, then we can remove it.
3434
// This is because we're unable to cleanly add a resource to the builder from within a callback.
3535
foreach (var item in data.Model.Resources.OfType<AzureFunctionsProjectResource>())
3636
{
@@ -111,15 +111,8 @@ public static class AzureFunctionsProjectResourceExtensions
111111
context.EnvironmentVariables["ASPNETCORE_FORWARDEDHEADERS_ENABLED"] = "true";
112112
context.EnvironmentVariables["FUNCTIONS_WORKER_RUNTIME"] = "dotnet-isolated";
113113

114-
if (resource.HostStorage.IsEmulator)
115-
{
116-
context.EnvironmentVariables["Storage"] = resource.HostStorage.GetEmulatorConnectionString();
117-
}
118-
else
119-
{
120-
context.EnvironmentVariables["Storage__blobServiceUri"] = resource.HostStorage.BlobEndpoint;
121-
context.EnvironmentVariables["Storage__queueServiceUri"] = resource.HostStorage.QueueEndpoint;
122-
}
114+
// Set the storage connection string.
115+
((IResourceWithAzureFunctionsConfig)resource.HostStorage).ApplyAzureFunctionsConfiguration(context.EnvironmentVariables, "Storage");
123116
})
124117
.WithOtlpExporter()
125118
.WithHttpEndpoint()

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ namespace Aspire.Hosting.Azure;
1212
/// <param name="configureConstruct">Callback to populate the construct with Azure resources.</param>
1313
public class AzureStorageResource(string name, Action<ResourceModuleConstruct> configureConstruct) :
1414
AzureConstructResource(name, configureConstruct),
15-
IResourceWithEndpoints
15+
IResourceWithEndpoints,
16+
IResourceWithAzureFunctionsConfig
1617
{
1718
private EndpointReference EmulatorBlobEndpoint => new(this, "blob");
1819
private EndpointReference EmulatorQueueEndpoint => new(this, "queue");
@@ -42,7 +43,7 @@ public class AzureStorageResource(string name, Action<ResourceModuleConstruct> c
4243
/// Gets the connection string for the Azure Storage emulator.
4344
/// </summary>
4445
/// <returns></returns>
45-
public ReferenceExpression GetEmulatorConnectionString() => IsEmulator
46+
internal ReferenceExpression GetEmulatorConnectionString() => IsEmulator
4647
? ReferenceExpression.Create($"{AzureStorageEmulatorConnectionString.Create(blobPort: EmulatorBlobEndpoint.Port, queuePort: EmulatorQueueEndpoint.Port, tablePort: EmulatorTableEndpoint.Port)}")
4748
: throw new InvalidOperationException("The Azure Storage resource is not running in the local emulator.");
4849

@@ -57,4 +58,17 @@ internal ReferenceExpression GetQueueConnectionString() => IsEmulator
5758
internal ReferenceExpression GetBlobConnectionString() => IsEmulator
5859
? ReferenceExpression.Create($"{AzureStorageEmulatorConnectionString.Create(blobPort: EmulatorBlobEndpoint.Port)}")
5960
: ReferenceExpression.Create($"{BlobEndpoint}");
61+
62+
void IResourceWithAzureFunctionsConfig.ApplyAzureFunctionsConfiguration(IDictionary<string, object> target, string connectionName)
63+
{
64+
if (IsEmulator)
65+
{
66+
target[connectionName] = GetEmulatorConnectionString();
67+
}
68+
else
69+
{
70+
target[$"{connectionName}__blobServiceUri"] = BlobEndpoint;
71+
target[$"{connectionName}__queueServiceUri"] = QueueEndpoint;
72+
}
73+
}
6074
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#nullable enable
2-
Aspire.Hosting.Azure.AzureStorageResource.GetEmulatorConnectionString() -> Aspire.Hosting.ApplicationModel.ReferenceExpression!
2+

0 commit comments

Comments
 (0)