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

Fix #224, #206: hot reload failed on asp.net core 6+ mini api. #225

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 24 additions & 14 deletions demo/Apollo.AspNetCore.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Startup>());
}
context.Response.StatusCode = 404;

var key = context.Request.Query["key"];
if (string.IsNullOrWhiteSpace(key)) return Task.CompletedTask;

var value = context.RequestServices.GetRequiredService<IConfiguration>()[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();
24 changes: 0 additions & 24 deletions demo/Apollo.AspNetCore.Demo/Startup.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$(PackageReleaseNotes)</Description>
<RootNamespace>Microsoft.AspNetCore.Hosting</RootNamespace>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>$(ApolloVersion).0</Version>
<Version>$(ApolloVersion).1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Apollo.Configuration/Apollo.Configuration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$(PackageReleaseNotes)</Description>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion).0</Version>
<Version>$(ApolloVersion).1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 12 additions & 2 deletions src/Apollo.Configuration/ApolloConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$(PackageReleaseNotes)</Description>
<RootNamespace>Microsoft.Extensions.Hosting</RootNamespace>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion).0</Version>
<Version>$(ApolloVersion).1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down