From 7995dbf0064c9924aa5ae25671158d3982317bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=BC=9F?= Date: Mon, 20 Feb 2023 17:09:27 +0800 Subject: [PATCH] Fix #224, #206: hot reload failed on asp.net core 6+ mini api. --- demo/Apollo.AspNetCore.Demo/Program.cs | 38 ++++++++++++------- demo/Apollo.AspNetCore.Demo/Startup.cs | 24 ------------ .../Apollo.AspNetCoreHosting.csproj | 2 +- .../Apollo.Configuration.csproj | 2 +- .../ApolloConfigurationProvider.cs | 14 ++++++- .../Apollo.ExtensionsHosting.csproj | 2 +- 6 files changed, 39 insertions(+), 43 deletions(-) delete mode 100644 demo/Apollo.AspNetCore.Demo/Startup.cs diff --git a/demo/Apollo.AspNetCore.Demo/Program.cs b/demo/Apollo.AspNetCore.Demo/Program.cs index c819659..7d98d53 100644 --- a/demo/Apollo.AspNetCore.Demo/Program.cs +++ b/demo/Apollo.AspNetCore.Demo/Program.cs @@ -1,18 +1,28 @@ using Com.Ctrip.Framework.Apollo.ConfigAdapter; -namespace Apollo.AspNetCore.Demo; +YamlConfigAdapter.Register(); -public class Program +var builder = WebApplication.CreateBuilder(args); + +builder.Host.AddApollo(false); + +var app = builder.Build(); + +app.UseDeveloperExceptionPage(); + +app.Run(context => { - public static Task Main(string[] args) - { - YamlConfigAdapter.Register(); - - return CreateHostBuilder(args).Build().RunAsync(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .AddApollo(false) - .ConfigureWebHostDefaults(builder => builder.UseStartup()); -} \ No newline at end of file + context.Response.StatusCode = 404; + + var key = context.Request.Query["key"]; + if (string.IsNullOrWhiteSpace(key)) return Task.CompletedTask; + + var value = context.RequestServices.GetRequiredService()[key]; + if (value != null) context.Response.StatusCode = 200; + + context.Response.Headers["Content-Type"] = "text/html; charset=utf-8"; + + return context.Response.WriteAsync(value ?? "undefined"); +}); + +app.Run(); diff --git a/demo/Apollo.AspNetCore.Demo/Startup.cs b/demo/Apollo.AspNetCore.Demo/Startup.cs deleted file mode 100644 index 2bc3cc1..0000000 --- a/demo/Apollo.AspNetCore.Demo/Startup.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace Apollo.AspNetCore.Demo; - -public class Startup -{ - public void Configure(IApplicationBuilder app) - { - app.UseDeveloperExceptionPage(); - - app.Run(context => - { - context.Response.StatusCode = 404; - - var key = context.Request.Query["key"]; - if (string.IsNullOrWhiteSpace(key)) return Task.CompletedTask; - - var value = context.RequestServices.GetRequiredService()[key]; - if (value != null) context.Response.StatusCode = 200; - - context.Response.Headers["Content-Type"] = "text/html; charset=utf-8"; - - return context.Response.WriteAsync(value ?? "undefined"); - }); - } -} \ No newline at end of file diff --git a/src/Apollo.AspNetCoreHosting/Apollo.AspNetCoreHosting.csproj b/src/Apollo.AspNetCoreHosting/Apollo.AspNetCoreHosting.csproj index d931381..d93b2f5 100644 --- a/src/Apollo.AspNetCoreHosting/Apollo.AspNetCoreHosting.csproj +++ b/src/Apollo.AspNetCoreHosting/Apollo.AspNetCoreHosting.csproj @@ -12,7 +12,7 @@ $(PackageReleaseNotes) Microsoft.AspNetCore.Hosting netcoreapp3.1 - $(ApolloVersion).0 + $(ApolloVersion).1 diff --git a/src/Apollo.Configuration/Apollo.Configuration.csproj b/src/Apollo.Configuration/Apollo.Configuration.csproj index df231d0..192b8d3 100644 --- a/src/Apollo.Configuration/Apollo.Configuration.csproj +++ b/src/Apollo.Configuration/Apollo.Configuration.csproj @@ -12,7 +12,7 @@ $(PackageReleaseNotes) Com.Ctrip.Framework.Apollo netstandard2.0;netstandard2.1 - $(ApolloVersion).0 + $(ApolloVersion).1 diff --git a/src/Apollo.Configuration/ApolloConfigurationProvider.cs b/src/Apollo.Configuration/ApolloConfigurationProvider.cs index ebef4ca..c8b26fa 100644 --- a/src/Apollo.Configuration/ApolloConfigurationProvider.cs +++ b/src/Apollo.Configuration/ApolloConfigurationProvider.cs @@ -8,6 +8,7 @@ public class ApolloConfigurationProvider : ConfigurationProvider, IRepositoryCha internal string? SectionKey { get; } internal IConfigRepository ConfigRepository { get; } private Task? _initializeTask; + private int _buildCount; public ApolloConfigurationProvider(string? sectionKey, IConfigRepository configRepository) { @@ -46,9 +47,18 @@ void IRepositoryChangeListener.OnRepositoryChange(string namespaceName, Properti OnReload(); } - IConfigurationProvider IConfigurationSource.Build(IConfigurationBuilder builder) => this; + IConfigurationProvider IConfigurationSource.Build(IConfigurationBuilder builder) + { + Interlocked.Increment(ref _buildCount); + + return this; + } - public void Dispose() => ConfigRepository.RemoveChangeListener(this); + public void Dispose() + { + if (Interlocked.Decrement(ref _buildCount) == 0) + ConfigRepository.RemoveChangeListener(this); + } public override string ToString() => string.IsNullOrEmpty(SectionKey) ? $"apollo {ConfigRepository}" diff --git a/src/Apollo.ExtensionsHosting/Apollo.ExtensionsHosting.csproj b/src/Apollo.ExtensionsHosting/Apollo.ExtensionsHosting.csproj index 4a51e72..7147e27 100644 --- a/src/Apollo.ExtensionsHosting/Apollo.ExtensionsHosting.csproj +++ b/src/Apollo.ExtensionsHosting/Apollo.ExtensionsHosting.csproj @@ -12,7 +12,7 @@ $(PackageReleaseNotes) Microsoft.Extensions.Hosting netstandard2.0;netstandard2.1 - $(ApolloVersion).0 + $(ApolloVersion).1