-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consul deregistration using hosted service
- Loading branch information
Showing
5 changed files
with
114 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace ATI.Services.Consul | ||
{ | ||
[PublicAPI] | ||
public static class ConsulDeregistrationExtension | ||
{ | ||
private const string DeregistrationAddress = "_internal/consul/deregister"; | ||
namespace ATI.Services.Consul; | ||
|
||
public static IEndpointConventionBuilder MapConsulDeregistration(this IEndpointRouteBuilder builder, | ||
string deregistrationAddress = null) | ||
{ | ||
return builder.MapDelete(deregistrationAddress ?? DeregistrationAddress, DeregisterDelegate); | ||
} | ||
[PublicAPI] | ||
public static class ConsulDeregistrationExtension | ||
{ | ||
private const string DeregistrationAddress = "_internal/consul/deregister"; | ||
|
||
private static async Task DeregisterDelegate(HttpContext _) | ||
{ | ||
await ConsulRegistrator.DeregisterInstanceAsync(); | ||
} | ||
public static IEndpointConventionBuilder MapConsulDeregistration(this IEndpointRouteBuilder builder, | ||
string deregistrationAddress = null) | ||
{ | ||
var registrator = builder.ServiceProvider.GetService<ConsulRegistrator>(); | ||
return builder.MapDelete(deregistrationAddress ?? DeregistrationAddress, async _ => await registrator.DeregisterInstanceAsync()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ATI.Services.Common.Behaviors; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace ATI.Services.Consul; | ||
|
||
[PublicAPI] | ||
public class ConsulHostedService( | ||
IOptions<ConsulRegistratorOptions> consulRegistratorOptions, | ||
ConsulRegistrator registrator) : IHostedService | ||
{ | ||
public Task StartAsync(CancellationToken ct) | ||
{ | ||
if (bool.TryParse(ConfigurationManager.AppSettings("ConsulEnabled"), out var enabled) && enabled) | ||
return registrator.RegisterServicesAsync(consulRegistratorOptions.Value, | ||
ConfigurationManager.GetApplicationPort()); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken ct) => registrator.DeregisterInstanceAsync(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters