Skip to content

Commit

Permalink
Fix issue enumerating services in newer .NET DI library (#241)
Browse files Browse the repository at this point in the history
Fixes #238
  • Loading branch information
cretz authored May 8, 2024
1 parent ee378c1 commit f40e78e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,23 @@ public static OptionsBuilder<TemporalWorkerServiceOptions> ConfigureOptions(
if (disallowDuplicates)
{
var any = builder.Services.Any(s =>
s.ImplementationInstance is ConfigureNamedOptions<TemporalWorkerServiceOptions> instance &&
instance.Name == optionsName);
{
// Since https://github.com/dotnet/runtime/pull/87183 in 8.0.0+ of
// Microsoft.Extensions.DependencyInjection.Abstractions, simply accessing this
// property on a service can throw an exception if the service is "keyed".
// Knowing whether a service is keyed is only exposed programmatically in that
// newer version and we don't want to depend on that newer version. And we know
// that we never make our options keyed, so we can just swallow the exception.
try
{
return s.ImplementationInstance is ConfigureNamedOptions<TemporalWorkerServiceOptions> instance &&
instance.Name == optionsName;
}
catch (InvalidOperationException)
{
return false;
}
});
if (any)
{
throw new InvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public async Task TemporalWorkerService_ExecuteAsync_SimpleWorker()
services.
AddSingleton<ILoggerFactory>(loggerFactory).
AddScoped<DatabaseClient>().
// We are also adding the DB client as a keyed service to demonstrate keyed service
// support for our DI logic. This used to break because newer DI library versions
// disallowed accessing certain properties on keyed services which we access
// internally for dupe checks.
AddKeyedScoped<DatabaseClient>("client-keyed").
AddHostedTemporalWorker(taskQueue).
AddScopedActivities<DatabaseActivities>().
AddWorkflow<DatabaseWorkflow>();
Expand Down
3 changes: 2 additions & 1 deletion tests/Temporalio.Tests/Temporalio.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.4.0" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="7.0.0" />
Expand Down

0 comments on commit f40e78e

Please sign in to comment.