Skip to content

Commit

Permalink
Merge pull request #185 from pengweiqhca/main
Browse files Browse the repository at this point in the history
HttpMessageHandlerFactory => HttpMessageHandler
  • Loading branch information
pengweiqhca authored Dec 30, 2021
2 parents aedc9f1 + 8783423 commit d83f9f4
Show file tree
Hide file tree
Showing 32 changed files with 160 additions and 104 deletions.
1 change: 1 addition & 0 deletions Apollo.AspNetCore.Demo/Apollo.AspNetCore.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Apollo.AspNetCoreHosting/Apollo.AspNetCoreHosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
  直接使用ApolloConfigurationManager请使用Com.Ctrip.Framework.Apollo.Configuration或者Com.Ctrip.Framework.Apollo.ConfigurationManager

$(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<RootNamespace>Microsoft.AspNetCore.Hosting</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

<ItemGroup>
<Compile Include="../Apollo.ExtensionsHosting/HostingBuilderExtensions.cs" />

<ProjectReference Include="..\Apollo.ExtensionsHosting\Apollo.ExtensionsHosting.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />

<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions Apollo.ConfigAdapter.Yaml/Apollo.ConfigAdapter.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<AssemblyName>Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml</AssemblyName>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<PackageReleaseNotes>发布日志请点击打开 https://github.com/apolloconfig/apollo.net/releases</PackageReleaseNotes>
<Description>添加对yml和yaml格式的namespace支持,使用YamlConfigAdapter.Register()添加支持。

Expand Down
25 changes: 11 additions & 14 deletions Apollo.ConfigAdapter.Yaml/YamlConfigurationFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Com.Ctrip.Framework.Apollo.ConfigAdapter;

internal class YamlConfigurationFileParser
{
private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly IDictionary<string, string> _data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Stack<string> _context = new();
private string _currentPath = "";

Expand All @@ -16,12 +16,13 @@ public IDictionary<string, string> Parse(TextReader reader)
var yamlStream = new YamlStream();
yamlStream.Load(reader);

if (yamlStream.Documents.Count > 0 && yamlStream.Documents[0].RootNode is YamlMappingNode mappingNode)
foreach (var node in mappingNode.Children)
if (node.Key is YamlScalarNode ysn && ysn.Value != null)
VisitYamlNode(ysn.Value, node.Value);
else
throw UnsupportedKeyType(node.Key, _currentPath);
if (yamlStream.Documents.Count < 1 || yamlStream.Documents[0].RootNode is not YamlMappingNode mappingNode) return _data;

foreach (var node in mappingNode.Children)
if (node.Key is YamlScalarNode { Value: { } } ysn)
VisitYamlNode(ysn.Value, node.Value);
else
throw UnsupportedKeyType(node.Key, _currentPath);

return _data;
}
Expand Down Expand Up @@ -129,10 +130,6 @@ private void ExitContext()
}

private static bool IsNullValue(YamlScalarNode yamlValue) =>
yamlValue.Style == ScalarStyle.Plain
&& (yamlValue.Value == "~"
|| yamlValue.Value == null
|| yamlValue.Value == "null"
|| yamlValue.Value == "Null"
|| yamlValue.Value == "NULL");
}
yamlValue.Style == ScalarStyle.Plain &&
yamlValue.Value is "~" or null or "null" or "Null" or "NULL";
}
8 changes: 4 additions & 4 deletions Apollo.Configuration/Apollo.Configuration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
  直接使用ApolloConfigurationManager请使用Com.Ctrip.Framework.Apollo.Configuration或者Com.Ctrip.Framework.Apollo.ConfigurationManager

$(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

Expand All @@ -25,7 +23,9 @@ $(PackageReleaseNotes)</Description>
</AssemblyAttribute>

<ProjectReference Include="..\Apollo\Apollo.csproj" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" />

<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Apollo.Configuration/ApolloConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static IApolloConfigurationBuilder AddNamespace(this IApolloConfiguration
public static IApolloConfigurationBuilder AddNamespace(this IApolloConfigurationBuilder builder, string @namespace, string? sectionKey, ConfigFileFormat format = ConfigFileFormat.Properties)
{
if (string.IsNullOrWhiteSpace(@namespace)) throw new ArgumentNullException(nameof(@namespace));
if (format < ConfigFileFormat.Properties || format > ConfigFileFormat.Txt) throw new ArgumentOutOfRangeException(nameof(format), format, $"最小值{ConfigFileFormat.Properties},最大值{ConfigFileFormat.Txt}");
if (format is < ConfigFileFormat.Properties or > ConfigFileFormat.Txt) throw new ArgumentOutOfRangeException(nameof(format), format, $"最小值{ConfigFileFormat.Properties},最大值{ConfigFileFormat.Txt}");

if (format != ConfigFileFormat.Properties) @namespace += "." + format.GetString();

Expand Down
19 changes: 17 additions & 2 deletions Apollo.Configuration/ApolloOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,22 @@ public virtual string? MetaServer

public IDictionary<string, string> Meta { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public Func<HttpMessageHandler>? HttpMessageHandlerFactory { get; set; }
private HttpMessageHandler _handler = new HttpClientHandler();

public HttpMessageHandler HttpMessageHandler
{
get => _handler;
set => Interlocked.Exchange(ref _handler, value).Dispose();
}

[Obsolete("请使用HttpMessageHandler", true)]
public Func<HttpMessageHandler> HttpMessageHandlerFactory
{
get => () => _handler;
set => HttpMessageHandler = value();
}

public ICacheFileProvider CacheFileProvider { get; set; } = new LocalPlaintextCacheFileProvider();
}

public void Dispose() => _handler.Dispose();
}
21 changes: 21 additions & 0 deletions Apollo.Configuration/Logging/MelLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.Logging;

namespace Com.Ctrip.Framework.Apollo.Logging;

public static class MelLogging
{
public static void UseMel(ILoggerFactory loggerFactory) => LogManager.LogFactory = logger =>
(level, msg, ex) => loggerFactory.CreateLogger(logger).Log(Convert(level), ex, msg);

private static Microsoft.Extensions.Logging.LogLevel Convert(LogLevel level) => level switch
{
LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
LogLevel.Info => Microsoft.Extensions.Logging.LogLevel.Information,
LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning,
LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
LogLevel.Fatal => Microsoft.Extensions.Logging.LogLevel.Critical,
_ => Microsoft.Extensions.Logging.LogLevel.None
};
}

2 changes: 1 addition & 1 deletion Apollo.Configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ services.ConfigureJsonValue<Options>(/*name, */config.GetSection("somePrefix:Jso
+ .ConfigureAppConfiguration(builder =>
+ {
+ var apollo = builder.Build().GetSection("apollo").Get<ApolloOptions>();
+ apollo.HttpMessageHandlerFactory = () => new HttpClientHandler
+ apollo.HttpMessageHandler = new HttpClientHandler
+ {
+ UseProxy = true,
+ Proxy = new WebProxy(new Uri("http://代理地址"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void AppSettingsSectionBuilderTest() =>

[Fact]
public void ConnectionStringsSectionBuilderTest() =>
Assert.Equal("asdfasdf", System.Configuration.ConfigurationManager.ConnectionStrings["abc"]?.ConnectionString);
Assert.Equal("Test connection string", System.Configuration.ConfigurationManager.ConnectionStrings["abc"]?.ConnectionString);

[Fact]
public void NodeReplaceSectionBuilderTest()
Expand Down
2 changes: 1 addition & 1 deletion Apollo.ConfigurationManager.Tests/TestConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public bool TryGetProperty(string key, [NotNullWhen(true)] out string? value) =>

public IEnumerable<string> GetPropertyNames() => _dict.Keys;

public event ConfigChangeEvent ConfigChanged = default!;
public event ConfigChangeEvent? ConfigChanged = default;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
  直接使用ApolloConfigurationManager请使用Com.Ctrip.Framework.Apollo.Configuration或者Com.Ctrip.Framework.Apollo.ConfigurationManager

$(PackageReleaseNotes)</Description>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<PackageTags>$(PackageTags) ConfigurationBuilder ConfigurationManager</PackageTags>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFrameworks>net471;net45;net40;netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion).3</Version>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Apollo.ConfigurationManager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ apollo.net项目中有多个样例客户端的项目:
在读取任何配置之前执行如下代码

``` C#
ConfigUtil.UseHttpMessageHandlerFactory(() => new HttpClientHandler
ConfigUtil.UseHttpMessageHandler(new HttpClientHandler
{
UseProxy = true,
Proxy = new WebProxy(new Uri("http://代理地址"))
Expand Down
15 changes: 11 additions & 4 deletions Apollo.ConfigurationManager/Util/ConfigUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Com.Ctrip.Framework.Apollo.Util;
public class ConfigUtil : IApolloOptions
{
public static NameValueCollection? AppSettings { get; set; }
private static Func<HttpMessageHandler>? _httpMessageHandlerFactory;
private static HttpMessageHandler _handler = new HttpClientHandler();
private static ICacheFileProvider? _cacheFileProvider;
private static readonly Func<Action<LogLevel, string, Exception?>> Logger = () => LogManager.CreateLogger(typeof(ConfigUtil));

Expand Down Expand Up @@ -161,13 +161,20 @@ private void InitRefreshInterval()

public string LocalCacheDir => GetAppConfig(nameof(LocalCacheDir)) ?? Path.Combine(ConfigConsts.DefaultLocalCacheDir, AppId);

public Func<HttpMessageHandler>? HttpMessageHandlerFactory => _httpMessageHandlerFactory;

public bool EnablePlaceholder => bool.TryParse(GetAppConfig(nameof(EnablePlaceholder)), out var enablePlaceholder) && enablePlaceholder;

public ICacheFileProvider CacheFileProvider => _cacheFileProvider ??= new LocalPlaintextCacheFileProvider();

public static void UseHttpMessageHandlerFactory(Func<HttpMessageHandler> factory) => Interlocked.CompareExchange(ref _httpMessageHandlerFactory, factory, null);
public HttpMessageHandler HttpMessageHandler => _handler;

public static void UseHttpMessageHandler(HttpMessageHandler handler) =>
Interlocked.Exchange(ref _handler, handler).Dispose();

[Obsolete("请使用UseHttpMessageHandler", true)]
public static void UseHttpMessageHandlerFactory(Func<HttpMessageHandler> factory) =>
UseHttpMessageHandler(factory());

public static void UseCacheFileProvider(ICacheFileProvider cacheFileProvider) => Interlocked.CompareExchange(ref _cacheFileProvider, cacheFileProvider, null);

public void Dispose() { }
}
7 changes: 3 additions & 4 deletions Apollo.ExtensionsHosting/Apollo.ExtensionsHosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
  直接使用ApolloConfigurationManager请使用Com.Ctrip.Framework.Apollo.Configuration或者Com.Ctrip.Framework.Apollo.ConfigurationManager

$(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<RootNamespace>Microsoft.Extensions.Hosting</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Apollo.Configuration\Apollo.Configuration.csproj" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.1" />

<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<AssemblyName>Com.Ctrip.Framework.Apollo.OpenApi.DependencyInjection</AssemblyName>
<PackageReleaseNotes></PackageReleaseNotes>
<Description>携程Apollo的OpenApi客户端,入口接口IOpenApiFactory</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.0</Version>
<RootNamespace>Com.Ctrip.Framework.Apollo.OpenApi</RootNamespace>
Expand Down
2 changes: 0 additions & 2 deletions Apollo.OpenApi.Tests/Apollo.OpenApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.*" />

<ProjectReference Include="..\Apollo.OpenApi.DependencyInjection\Apollo.OpenApi.DependencyInjection.csproj" />

<ProjectReference Include="..\Apollo\Apollo.csproj" />
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions Apollo.OpenApi/Apollo.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
<AssemblyName>Com.Ctrip.Framework.Apollo.OpenApi</AssemblyName>
<PackageReleaseNotes></PackageReleaseNotes>
<Description>携程Apollo的OpenApi客户端,入口接口IOpenApiFactory</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<TargetFrameworks>net40;net45;netstandard2.0</TargetFrameworks>
<Version>2.1.0</Version>
<RootNamespace>Com.Ctrip.Framework.Apollo.OpenApi</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Apollo\Apollo.csproj" PrivateAssets="All" />
<Compile Include="../Apollo/Core/ConfigConsts.cs" />

<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.*" Condition="'$(TargetFramework)' != 'net40'" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Apollo.Tests/ConfigExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ private class FakeConfig : IConfig
{
private readonly IReadOnlyDictionary<string, string> _data;

public event ConfigChangeEvent ConfigChanged = default!;
public event ConfigChangeEvent? ConfigChanged = default;

public FakeConfig(IReadOnlyDictionary<string, string> data) => _data = data;

public IEnumerable<string> GetPropertyNames() => _data.Keys;

public bool TryGetProperty(string key, [NotNullWhen(true)] out string? value) => _data.TryGetValue(key, out value);
}
}
}
3 changes: 2 additions & 1 deletion Apollo.Tests/ConfigServiceLocatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public async Task MetaServerTest()
moq.SetupGet(o => o.MetaServer).Returns("http://106.54.227.205:8080/");
moq.SetupGet(o => o.ConfigServer).Returns(Array.Empty<string>());
moq.SetupGet(o => o.Timeout).Returns(5000);
moq.SetupGet(o => o.HttpMessageHandler).Returns(new HttpClientHandler());

var options = moq.Object;

Expand All @@ -42,4 +43,4 @@ public async Task ConfigServerTest()
Assert.Equal(1, services.Count);
Assert.Equal(options.ConfigServer.FirstOrDefault(), services[0].HomepageUrl);
}
}
}
1 change: 1 addition & 0 deletions Apollo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.travis.yml = .travis.yml
azure-pipelines.yml = azure-pipelines.yml
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
PackAllProject.ps1 = PackAllProject.ps1
README.md = README.md
EndProjectSection
Expand Down
2 changes: 0 additions & 2 deletions Apollo/Apollo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
  直接使用ApolloConfigurationManager请使用Com.Ctrip.Framework.Apollo.Configuration或者Com.Ctrip.Framework.Apollo.ConfigurationManager

$(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFrameworks>net40;net45;netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion).0</Version>
Expand Down
4 changes: 2 additions & 2 deletions Apollo/ConfigAdapter/JsonConfigurationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class JsonConfigurationParser
{
private JsonConfigurationParser() { }

private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly IDictionary<string, string> _data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Stack<string> _context = new();
private string _currentPath = string.Empty;

Expand Down Expand Up @@ -105,4 +105,4 @@ private void ExitContext()
_context.Pop();
_currentPath = ConfigurationPath.Combine(_context.Reverse());
}
}
}
4 changes: 2 additions & 2 deletions Apollo/IApolloOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Com.Ctrip.Framework.Apollo;

public interface IApolloOptions
public interface IApolloOptions : IDisposable
{
string AppId { get; }
/// <summary>
Expand Down Expand Up @@ -42,7 +42,7 @@ public interface IApolloOptions

string? LocalCacheDir { get; }

Func<HttpMessageHandler>? HttpMessageHandlerFactory { get; }
HttpMessageHandler HttpMessageHandler { get; }

ICacheFileProvider CacheFileProvider { get; }
}
4 changes: 2 additions & 2 deletions Apollo/IConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface IConfig
/// <summary>
/// Config change event subscriber
/// </summary>
event ConfigChangeEvent ConfigChanged;
}
event ConfigChangeEvent? ConfigChanged;
}
Loading

0 comments on commit d83f9f4

Please sign in to comment.