Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize retrieval of environment variables from IConfiguration instead of directly #1363

Merged
merged 22 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d96807f
Implemented against Dapr.Client.AspNetCore client
WhitWaldo Aug 14, 2024
d309423
SImplified DaprWorkflow DI registration and updated to use IConfigura…
WhitWaldo Aug 14, 2024
d79db7b
Added missing copyright header
WhitWaldo Aug 14, 2024
09544d6
Updated actor registration to prefer the updated IConfiguration-based…
WhitWaldo Aug 14, 2024
fc42cbc
Merge branch 'master' into iconfiguration-preference
WhitWaldo Oct 14, 2024
fa49f23
Merge branch 'master' into iconfiguration-preference
WhitWaldo Oct 14, 2024
851e797
Adopted accepted proposal's guidelines for favoring different environ…
WhitWaldo Oct 15, 2024
ef25775
Merge remote-tracking branch 'origin/iconfiguration-preference' into …
WhitWaldo Oct 15, 2024
616638f
Made some lines a little more concise, added hostname default to Dapr…
WhitWaldo Oct 15, 2024
d386a5c
Fixed and updated unit tests
WhitWaldo Oct 15, 2024
55f375f
Merge branch 'master' into iconfiguration-preference
WhitWaldo Oct 16, 2024
48e4de8
Updated to put endpoint resolution mechanism in DaprDefaults within D…
WhitWaldo Oct 16, 2024
723d955
Updated packages to fix security advisory https://github.com/advisori…
WhitWaldo Oct 17, 2024
18d6c80
Updated Workflow builder to use DaprDefaults with IConfiguration
WhitWaldo Oct 17, 2024
d803bb4
Merge branch 'master' into iconfiguration-preference
WhitWaldo Oct 17, 2024
01e7cbc
Updating global.json
WhitWaldo Oct 17, 2024
c36bded
Tweaked global.json comment
WhitWaldo Oct 17, 2024
ca8566d
Merge remote-tracking branch 'origin/iconfiguration-preference' into …
WhitWaldo Oct 17, 2024
e3e0a85
Adding braces per nit
WhitWaldo Oct 17, 2024
2ce8d35
Consolidated both registration extension methods to remove duplication
WhitWaldo Oct 17, 2024
91849c6
Merge branch 'master' into iconfiguration-preference
WhitWaldo Oct 17, 2024
d558f1f
Merge branch 'master' into iconfiguration-preference
WhitWaldo Oct 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2"/>
<PackageVersion Include="FluentAssertions" Version="5.9.0" />
Expand All @@ -26,6 +27,7 @@
<PackageVersion Include="Microsoft.DurableTask.Client.Grpc" Version="1.3.0" />
<PackageVersion Include="Microsoft.DurableTask.Worker.Grpc" Version="1.3.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="6.0.0"/>
Expand All @@ -39,6 +41,7 @@
<PackageVersion Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageVersion Include="System.Formats.Asn1" Version="6.0.1" />
<PackageVersion Include="System.Text.Json" Version="6.0.10"/>
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
Expand Down
7 changes: 7 additions & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Protos", "src\Dapr.Pro
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common", "src\Dapr.Common\Dapr.Common.csproj", "{B445B19C-A925-4873-8CB7-8317898B6970}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Common.Test", "test\Dapr.Common.Test\Dapr.Common.Test.csproj", "{CDB47863-BEBD-4841-A807-46D868962521}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -302,6 +304,10 @@ Global
{B445B19C-A925-4873-8CB7-8317898B6970}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B445B19C-A925-4873-8CB7-8317898B6970}.Release|Any CPU.Build.0 = Release|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDB47863-BEBD-4841-A807-46D868962521}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -357,6 +363,7 @@ Global
{C74FBA78-13E8-407F-A173-4555AEE41FF3} = {A7F41094-8648-446B-AECD-DCC2CC871F73}
{DFBABB04-50E9-42F6-B470-310E1B545638} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{B445B19C-A925-4873-8CB7-8317898B6970} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{CDB47863-BEBD-4841-A807-46D868962521} = {DD020B34-460F-455F-8D17-CF4A949F100B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}
Expand Down
6 changes: 3 additions & 3 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_comment": "This policy allows the 7.0.101 SDK or patches in that family.",
"_comment": "This policy allows the 8.0.100 SDK or patches in that family.",
"sdk": {
"version": "7.0.101",
"rollForward": "latestMajor"
"version": "8.0.100",
"rollForward": "minor"
}
}
28 changes: 22 additions & 6 deletions src/Dapr.Actors.AspNetCore/ActorsServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -11,9 +11,13 @@
// limitations under the License.
// ------------------------------------------------------------------------

#nullable enable

using System;
using Dapr;
using Dapr.Actors.Client;
using Dapr.Actors.Runtime;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -30,12 +34,9 @@ public static class ActorsServiceCollectionExtensions
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="configure">A delegate used to configure actor options and register actor types.</param>
public static void AddActors(this IServiceCollection services, Action<ActorRuntimeOptions> configure)
public static void AddActors(this IServiceCollection? services, Action<ActorRuntimeOptions>? configure)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}
ArgumentNullException.ThrowIfNull(services, nameof(services));

// Routing and health checks are required dependencies.
services.AddRouting();
Expand All @@ -45,6 +46,8 @@ public static void AddActors(this IServiceCollection services, Action<ActorRunti
services.TryAddSingleton<ActorRuntime>(s =>
{
var options = s.GetRequiredService<IOptions<ActorRuntimeOptions>>().Value;
ConfigureActorOptions(s, options);

var loggerFactory = s.GetRequiredService<ILoggerFactory>();
var activatorFactory = s.GetRequiredService<ActorActivatorFactory>();
var proxyFactory = s.GetRequiredService<IActorProxyFactory>();
Expand All @@ -54,6 +57,8 @@ public static void AddActors(this IServiceCollection services, Action<ActorRunti
services.TryAddSingleton<IActorProxyFactory>(s =>
{
var options = s.GetRequiredService<IOptions<ActorRuntimeOptions>>().Value;
ConfigureActorOptions(s, options);

var factory = new ActorProxyFactory()
{
DefaultOptions =
Expand All @@ -72,5 +77,16 @@ public static void AddActors(this IServiceCollection services, Action<ActorRunti
services.Configure<ActorRuntimeOptions>(configure);
}
}

private static void ConfigureActorOptions(IServiceProvider serviceProvider, ActorRuntimeOptions options)
{
var configuration = serviceProvider.GetService<IConfiguration>();
options.DaprApiToken = !string.IsNullOrWhiteSpace(options.DaprApiToken)
? options.DaprApiToken
: DaprDefaults.GetDefaultDaprApiToken(configuration);
options.HttpEndpoint = !string.IsNullOrWhiteSpace(options.HttpEndpoint)
? options.HttpEndpoint
: DaprDefaults.GetDefaultHttpEndpoint();
}
}
}
1 change: 1 addition & 0 deletions src/Dapr.Actors.AspNetCore/Dapr.Actors.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@

<ItemGroup>
<ProjectReference Include="..\Dapr.Actors\Dapr.Actors.csproj" />
<ProjectReference Include="..\Dapr.Common\Dapr.Common.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Dapr.Actors/Client/ActorProxyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public JsonSerializerOptions JsonSerializerOptions
/// <summary>
/// The Dapr Api Token that is added to the header for all requests.
/// </summary>
public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken();
public string DaprApiToken { get; set; } = DaprDefaults.GetDefaultDaprApiToken(null);

/// <summary>
/// Gets or sets the HTTP endpoint URI used to communicate with the Dapr sidecar.
Expand Down
1 change: 1 addition & 0 deletions src/Dapr.Actors/Dapr.Actors.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
</ItemGroup>

</Project>
22 changes: 7 additions & 15 deletions src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// limitations under the License.
// ------------------------------------------------------------------------

#nullable enable

using System;
using System.Text.Json;

Expand All @@ -34,7 +36,7 @@ public sealed class ActorRuntimeOptions
};
private bool useJsonSerialization = false;
private JsonSerializerOptions jsonSerializerOptions = JsonSerializerDefaults.Web;
private string daprApiToken = DaprDefaults.GetDefaultDaprApiToken();
private string daprApiToken = string.Empty;
private int? remindersStoragePartitions = null;

/// <summary>
Expand Down Expand Up @@ -180,19 +182,14 @@ public JsonSerializerOptions JsonSerializerOptions

set
{
if (value is null)
{
throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null");
}

this.jsonSerializerOptions = value;
this.jsonSerializerOptions = value ?? throw new ArgumentNullException(nameof(JsonSerializerOptions), $"{nameof(ActorRuntimeOptions)}.{nameof(JsonSerializerOptions)} cannot be null");
}
}

/// <summary>
/// The <see cref="DaprApiToken"/> to add to the headers in requests to Dapr runtime
/// </summary>
public string DaprApiToken
public string? DaprApiToken
{
get
{
Expand All @@ -201,12 +198,7 @@ public string DaprApiToken

set
{
if (value is null)
{
throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null");
}

this.daprApiToken = value;
this.daprApiToken = value ?? throw new ArgumentNullException(nameof(DaprApiToken), $"{nameof(ActorRuntimeOptions)}.{nameof(DaprApiToken)} cannot be null");
}
}

Expand Down Expand Up @@ -241,6 +233,6 @@ public int? RemindersStoragePartitions
/// corresponding environment variables.
/// </remarks>
/// <value></value>
public string HttpEndpoint { get; set; } = DaprDefaults.GetDefaultHttpEndpoint();
public string? HttpEndpoint { get; set; }
}
}
3 changes: 1 addition & 2 deletions src/Dapr.AspNetCore/DaprAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.Authentication;

namespace Dapr.AspNetCore
Expand All @@ -29,6 +28,6 @@ public class DaprAuthenticationOptions : AuthenticationSchemeOptions
/// Gets or sets the App API token.
/// By default, the token will be read from the APP_API_TOKEN environment variable.
/// </summary>
public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken();
public string Token { get; set; } = DaprDefaults.GetDefaultAppApiToken(null);
}
}
101 changes: 61 additions & 40 deletions src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -11,55 +11,76 @@
// limitations under the License.
// ------------------------------------------------------------------------

namespace Microsoft.Extensions.DependencyInjection
{
using System;
using System.Linq;
using Dapr.Client;
using Extensions;
#nullable enable

namespace Microsoft.Extensions.DependencyInjection;

using System;
using Dapr;
using Dapr.Client;
using Extensions;
using Configuration;

/// <summary>
/// Provides extension methods for <see cref="IServiceCollection" />.
/// </summary>
public static class DaprServiceCollectionExtensions
{
/// <summary>
/// Provides extension methods for <see cref="IServiceCollection" />.
/// Adds Dapr client services to the provided <see cref="IServiceCollection" />. This does not include integration
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
/// </summary>
public static class DaprServiceCollectionExtensions
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="configure"></param>
public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder>? configure = null)
{
/// <summary>
/// Adds Dapr client services to the provided <see cref="IServiceCollection" />. This does not include integration
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="configure"></param>
public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder> configure = null)
ArgumentNullException.ThrowIfNull(services, nameof(services));

services.TryAddSingleton(serviceProvider =>
{
ArgumentNullException.ThrowIfNull(services, nameof(services));
var builder = CreateDaprClientBuilder(serviceProvider);
configure?.Invoke(builder);
return builder.Build();
});
}

services.TryAddSingleton(_ =>
{
var builder = new DaprClientBuilder();
configure?.Invoke(builder);
/// <summary>
/// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure"></param>
public static void AddDaprClient(this IServiceCollection services,
Action<IServiceProvider, DaprClientBuilder> configure)
{
ArgumentNullException.ThrowIfNull(services, nameof(services));

return builder.Build();
});
}

/// <summary>
/// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration
/// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure"></param>
public static void AddDaprClient(this IServiceCollection services,
Action<IServiceProvider, DaprClientBuilder> configure)
services.TryAddSingleton(serviceProvider =>
{
ArgumentNullException.ThrowIfNull(services, nameof(services));
var builder = CreateDaprClientBuilder(serviceProvider);
configure?.Invoke(serviceProvider, builder);
return builder.Build();
});
}

private static DaprClientBuilder CreateDaprClientBuilder(IServiceProvider serviceProvider)
{
var builder = new DaprClientBuilder();
var configuration = serviceProvider.GetService<IConfiguration>();

// Set the HTTP endpoint, if provided, else use the default endpoint
builder.UseHttpEndpoint(DaprDefaults.GetDefaultHttpEndpoint(configuration));

services.TryAddSingleton(serviceProvider =>
{
var builder = new DaprClientBuilder();
configure?.Invoke(serviceProvider, builder);
// Set the gRPC endpoint, if provided
builder.UseGrpcEndpoint(DaprDefaults.GetDefaultGrpcEndpoint(configuration));

return builder.Build();
});
// Set the API token, if provided
var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration);
if (!string.IsNullOrWhiteSpace(apiToken))
{
builder.UseDaprApiToken(apiToken);
}

return builder;
}
}
11 changes: 4 additions & 7 deletions src/Dapr.Client/DaprClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,14 @@ public static HttpClient CreateInvokeHttpClient(string appId = null, string dapr
public static CallInvoker CreateInvocationInvoker(string appId, string daprEndpoint = null, string daprApiToken = null)
{
var channel = GrpcChannel.ForAddress(daprEndpoint ?? DaprDefaults.GetDefaultGrpcEndpoint());
return channel.Intercept(new InvocationInterceptor(appId, daprApiToken ?? DaprDefaults.GetDefaultDaprApiToken()));
return channel.Intercept(new InvocationInterceptor(appId, daprApiToken ?? DaprDefaults.GetDefaultDaprApiToken(null)));
}

internal static KeyValuePair<string, string>? GetDaprApiTokenHeader(string apiToken)
{
if (string.IsNullOrWhiteSpace(apiToken))
{
return null;
}

return new KeyValuePair<string, string>("dapr-api-token", apiToken);
return string.IsNullOrWhiteSpace(apiToken)
? null
: new KeyValuePair<string, string>("dapr-api-token", apiToken);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Dapr.Client/DaprClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DaprClientBuilder()
};

this.JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
this.DaprApiToken = DaprDefaults.GetDefaultDaprApiToken();
this.DaprApiToken = DaprDefaults.GetDefaultDaprApiToken(null);
}

// property exposed for testing purposes
Expand Down
2 changes: 1 addition & 1 deletion src/Dapr.Client/InvocationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class InvocationHandler : DelegatingHandler
public InvocationHandler()
{
this.parsedEndpoint = new Uri(DaprDefaults.GetDefaultHttpEndpoint(), UriKind.Absolute);
this.apiToken = DaprDefaults.GetDefaultDaprApiToken();
this.apiToken = DaprDefaults.GetDefaultDaprApiToken(null);
}

/// <summary>
Expand Down
Loading
Loading