Skip to content

Commit

Permalink
Fix #639
Browse files Browse the repository at this point in the history
The issue is that the hosted service would not be registered if other
hosted services are present. The fix is to use TryAddEnumble so that we
allow multipled `IHostedService`s but not multiple instances of
the same type.
  • Loading branch information
rynowak committed Dec 24, 2018
1 parent dbf4ffa commit a2f2814
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class HealthCheckServiceCollectionExtensions
public static IHealthChecksBuilder AddHealthChecks(this IServiceCollection services)
{
services.TryAddSingleton<HealthCheckService, DefaultHealthCheckService>();
services.TryAddSingleton<IHostedService, HealthCheckPublisherHostedService>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, HealthCheckPublisherHostedService>());
return new HealthChecksBuilder(services);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Xunit;
Expand Down Expand Up @@ -39,5 +41,56 @@ public void AddHealthChecks_RegistersSingletonHealthCheckServiceIdempotently()
Assert.Null(actual.ImplementationFactory);
});
}

[Fact] // see: https://github.com/aspnet/Extensions/issues/639
public void AddHealthChecks_RegistersPublisherService_WhenOtherHostedServicesRegistered()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddSingleton<IHostedService, DummyHostedService>();
services.AddHealthChecks();

// Assert
Assert.Collection(services.OrderBy(s => s.ServiceType.FullName).ThenBy(s => s.ImplementationType.FullName),
actual =>
{
Assert.Equal(ServiceLifetime.Singleton, actual.Lifetime);
Assert.Equal(typeof(HealthCheckService), actual.ServiceType);
Assert.Equal(typeof(DefaultHealthCheckService), actual.ImplementationType);
Assert.Null(actual.ImplementationInstance);
Assert.Null(actual.ImplementationFactory);
},
actual =>
{
Assert.Equal(ServiceLifetime.Singleton, actual.Lifetime);
Assert.Equal(typeof(IHostedService), actual.ServiceType);
Assert.Equal(typeof(DummyHostedService), actual.ImplementationType);
Assert.Null(actual.ImplementationInstance);
Assert.Null(actual.ImplementationFactory);
},
actual =>
{
Assert.Equal(ServiceLifetime.Singleton, actual.Lifetime);
Assert.Equal(typeof(IHostedService), actual.ServiceType);
Assert.Equal(typeof(HealthCheckPublisherHostedService), actual.ImplementationType);
Assert.Null(actual.ImplementationInstance);
Assert.Null(actual.ImplementationFactory);
});
}

private class DummyHostedService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
}

public Task StopAsync(CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
}
}
}
}

0 comments on commit a2f2814

Please sign in to comment.