diff --git a/src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore.Tests/DbContextTest.cs b/src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore.Tests/DbContextTest.cs index c21a1a85b..fe056727c 100644 --- a/src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore.Tests/DbContextTest.cs +++ b/src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore.Tests/DbContextTest.cs @@ -204,7 +204,7 @@ public async Task TestDisabledSoftDelete() var dataFilter = serviceProvider.GetRequiredService(); using (dataFilter.Disable()) { - var count = await dbContext.Set().IgnoreQueryFilters().CountAsync(); + var count = await dbContext.Set().CountAsync(); Assert.IsTrue(count == 1); } } diff --git a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/CallerOptionsExtensions.cs b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/CallerOptionsExtensions.cs index 2c9dff968..5a18a9cd1 100644 --- a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/CallerOptionsExtensions.cs +++ b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/CallerOptionsExtensions.cs @@ -6,33 +6,60 @@ namespace Masa.Contrib.Service.Caller.HttpClient; public static class CallerOptionsExtensions { public static IHttpClientBuilder UseHttpClient(this CallerOptions callerOptions, - Func? clientBuilder = null) - => callerOptions.UseHttpClient(Microsoft.Extensions.Options.Options.DefaultName, clientBuilder); + Func? clientBuilder = null, + bool alwaysGetNewestHttpClient = false) + => callerOptions.UseHttpClient(Microsoft.Extensions.Options.Options.DefaultName, clientBuilder, alwaysGetNewestHttpClient); public static IHttpClientBuilder UseHttpClient(this CallerOptions callerOptions, string name, - Func? clientBuilder = null) + Func? clientBuilder = null, + bool alwaysGetNewestHttpClient = false) { - var builder = clientBuilder == null ? new MasaHttpClientBuilder() : clientBuilder.Invoke(); - var httpClientBuilder = callerOptions.Services.AddHttpClient(name, httpClient - => builder.ConfigureHttpClient(httpClient)); - - AddCallerExtensions.AddCaller(callerOptions, name, serviceProvider - => new HttpClientCaller(serviceProvider, name, builder.Prefix)); - return httpClientBuilder; + return callerOptions.UseHttpClient(name, masaHttpClientBuilder => + { + var builder = clientBuilder == null ? new MasaHttpClientBuilder() : clientBuilder.Invoke(); + masaHttpClientBuilder.BaseAddress = builder.BaseAddress; + masaHttpClientBuilder.Prefix = builder.Prefix; + masaHttpClientBuilder.Configure = builder.Configure; + }, alwaysGetNewestHttpClient); } public static IHttpClientBuilder UseHttpClient(this CallerOptions callerOptions, - Action? clientBuilder) - => callerOptions.UseHttpClient(Microsoft.Extensions.Options.Options.DefaultName, clientBuilder); + Action? clientBuilder, + bool alwaysGetNewestHttpClient = false) + => callerOptions.UseHttpClient(Microsoft.Extensions.Options.Options.DefaultName, clientBuilder, alwaysGetNewestHttpClient); public static IHttpClientBuilder UseHttpClient(this CallerOptions callerOptions, string name, - Action? clientBuilder) + Action? clientBuilder, + bool alwaysGetNewestHttpClient = false) { - MasaHttpClientBuilder builder = new MasaHttpClientBuilder(); - clientBuilder?.Invoke(builder); - - return callerOptions.UseHttpClient(name, () => builder); + if (alwaysGetNewestHttpClient) + { + var httpClientBuilder = callerOptions.Services.AddHttpClient(name); + AddCallerExtensions.AddCaller(callerOptions, name, serviceProvider + => + { + var masaHttpClientBuilder = new MasaHttpClientBuilder(); + clientBuilder?.Invoke(masaHttpClientBuilder); + var httpClient = serviceProvider.GetRequiredService().CreateClient(name); + masaHttpClientBuilder.ConfigureHttpClient(httpClient); + return new HttpClientCaller(httpClient, serviceProvider, masaHttpClientBuilder.Prefix); + }); + return httpClientBuilder; + } + else + { + var masaHttpClientBuilder = new MasaHttpClientBuilder(); + clientBuilder?.Invoke(masaHttpClientBuilder); + var httpClientBuilder = callerOptions.Services.AddHttpClient(name, httpClient => masaHttpClientBuilder.ConfigureHttpClient(httpClient)); + AddCallerExtensions.AddCaller(callerOptions, name, serviceProvider + => + { + var httpClient = serviceProvider.GetRequiredService().CreateClient(name); + return new HttpClientCaller(httpClient, serviceProvider, masaHttpClientBuilder.Prefix); + }); + return httpClientBuilder; + } } } diff --git a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCaller.cs b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCaller.cs index 254090b70..98fb79a6a 100644 --- a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCaller.cs +++ b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCaller.cs @@ -9,12 +9,12 @@ public class HttpClientCaller : AbstractCaller private readonly string _prefix; private readonly bool _prefixIsNullOrEmpty; - public HttpClientCaller(IServiceProvider serviceProvider, - string name, + public HttpClientCaller(System.Net.Http.HttpClient httpClient, + IServiceProvider serviceProvider, string prefix) : base(serviceProvider) { - _httpClient = serviceProvider.GetRequiredService().CreateClient(name); + _httpClient = httpClient; _prefix = prefix; _prefixIsNullOrEmpty = string.IsNullOrEmpty(_prefix); } diff --git a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCallerBase.cs b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCallerBase.cs index 6ad27ba6c..f1efa98d2 100644 --- a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCallerBase.cs +++ b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/HttpClientCallerBase.cs @@ -1,4 +1,4 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. namespace Masa.Contrib.Service.Caller.HttpClient; @@ -9,6 +9,8 @@ public abstract class HttpClientCallerBase : CallerBase protected virtual string Prefix { get; set; } = string.Empty; + protected virtual bool IsSupportUpdate { get; set; } = false; + protected HttpClientCallerBase() { @@ -27,7 +29,7 @@ protected virtual IHttpClientBuilder UseHttpClient() httpClientBuilder.Prefix = Prefix; httpClientBuilder.BaseAddress = BaseAddress; httpClientBuilder.Configure = ConfigureHttpClient; - }); + }, IsSupportUpdate); } protected virtual void ConfigureHttpClient(System.Net.Http.HttpClient httpClient) diff --git a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/MasaHttpClientBuilder.cs b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/MasaHttpClientBuilder.cs index fac99d215..5b27fb285 100644 --- a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/MasaHttpClientBuilder.cs +++ b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.HttpClient/MasaHttpClientBuilder.cs @@ -35,7 +35,7 @@ public MasaHttpClientBuilder(string baseAddress, string prefix, Action factory.CreateClient(It.IsAny())).Returns(magicHttpClient); - services.AddSingleton(httpClientFactory.Object); var serviceProvider = services.BuildServiceProvider(); - string name = ""; string prefix = ""; - var caller = new HttpClientCaller(serviceProvider, name, prefix); + var caller = new HttpClientCaller(magicHttpClient, serviceProvider, prefix); var excuteCount = 0; caller.ConfigRequestMessage(httpRequestMessage => diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/CustomHttpClientCaller.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/CustomHttpClientCaller.cs index aeb1a890f..0bdf0af51 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/CustomHttpClientCaller.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/CustomHttpClientCaller.cs @@ -1,12 +1,12 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. namespace Masa.Contrib.Service.Caller.Tests; public class CustomHttpClientCaller : HttpClientCaller { - public CustomHttpClientCaller(IServiceProvider serviceProvider, string name, string baseApi) - : base(serviceProvider, name, baseApi) + public CustomHttpClientCaller(System.Net.Http.HttpClient httpClient, IServiceProvider serviceProvider, string baseApi) + : base(httpClient, serviceProvider, baseApi) { } diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/HttpClientCallerTest.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/HttpClientCallerTest.cs index 9f1c18d0f..f5db137a3 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/HttpClientCallerTest.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/HttpClientCallerTest.cs @@ -31,7 +31,7 @@ public void TestGetRequestUri(string prefix, string methods, string result) var services = new ServiceCollection(); services.AddCaller(opt => opt.UseHttpClient()); var serviceProvider = services.BuildServiceProvider(); - var caller = new CustomHttpClientCaller(serviceProvider, string.Empty, prefix); + var caller = new CustomHttpClientCaller(serviceProvider.GetRequiredService().CreateClient(), serviceProvider, prefix); Assert.IsTrue(caller.GetResult(methods) == result); } @@ -63,12 +63,9 @@ public async Task TestRequestDataIsXmlAsync() }) .Verifiable(); - httpClientFactory.Setup(factory => factory.CreateClient(It.IsAny())).Returns(magicHttpClient); - services.AddSingleton(httpClientFactory.Object); var serviceProvider = services.BuildServiceProvider(); - string name = ""; string prefix = ""; - var caller = new HttpClientCaller(serviceProvider, name, prefix); + var caller = new HttpClientCaller(magicHttpClient, serviceProvider, prefix); var res = await caller.PostAsync("Hello", new RegisterUser("Jim", "123456")); Assert.IsNotNull(res); @@ -92,7 +89,6 @@ public async Task TestRequestMessageReturnOnceAsync() }).Verifiable(); services.AddSingleton(_ => requestMessage.Object); services.AddSingleton(); - Mock httpClientFactory = new(); var handlerMock = new Mock(); var magicHttpClient = new System.Net.Http.HttpClient(handlerMock.Object) { @@ -113,12 +109,9 @@ public async Task TestRequestMessageReturnOnceAsync() }) .Verifiable(); - httpClientFactory.Setup(factory => factory.CreateClient(It.IsAny())).Returns(magicHttpClient); - services.AddSingleton(httpClientFactory.Object); var serviceProvider = services.BuildServiceProvider(); - string name = ""; string prefix = ""; - var caller = new HttpClientCaller(serviceProvider, name, prefix); + var caller = new HttpClientCaller(magicHttpClient, serviceProvider, prefix); var res = await caller.PostAsync("Hello", registerUser); Assert.IsNotNull(res); @@ -292,4 +285,34 @@ public async Task TestResponseIsDateTimeAsync() Assert.IsNotNull(res2); Assert.IsTrue(res2.ToString("yyyy-MM-dd HH:mm:ss") == date.ToString("yyyy-MM-dd HH:mm:ss")); } + + [TestMethod] + public void TestChangeableBaseAddress() + { + var services = new ServiceCollection(); + var key = "caller_test"; + var baiduAddress = "http://www.baidu.com"; + var githubAddress = "http://github.com"; + Environment.SetEnvironmentVariable(key, baiduAddress); + var index = 0; + services.AddCaller(options => + { + options.UseHttpClient(clientBuilder => + { + Assert.AreEqual(string.Empty, clientBuilder.BaseAddress); + clientBuilder.BaseAddress = Environment.GetEnvironmentVariable(key)!; + if (index == 0) Assert.AreEqual(baiduAddress, clientBuilder.BaseAddress.ToString()); + else if (index == 1) Assert.AreEqual(githubAddress, clientBuilder.BaseAddress.ToString()); + }, true); + }); + var serviceProvider = services.BuildServiceProvider(); + start: + using (var scope = serviceProvider.CreateScope()) + { + var caller = scope.ServiceProvider.GetRequiredService(); + Environment.SetEnvironmentVariable(key, githubAddress); + index++; + if (index == 1) goto start; + } + } } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs index 8cac88979..8d7aed069 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs @@ -18,13 +18,13 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services, public static IServiceCollection AddAuthClient(this IServiceCollection services, string authServiceBaseAddress, RedisConfigurationOptions redisOptions) { - ArgumentNullException.ThrowIfNull(authServiceBaseAddress, nameof(authServiceBaseAddress)); + MasaArgumentException.ThrowIfNullOrEmpty(authServiceBaseAddress); return services.AddAuthClient(callerOptions => { callerOptions.UseHttpClient(DEFAULT_CLIENT_NAME, builder => { - builder.Configure = opt => opt.BaseAddress = new Uri(authServiceBaseAddress); + builder.BaseAddress = authServiceBaseAddress; }); callerOptions.DisableAutoRegistration = true; }, redisOptions); @@ -32,7 +32,7 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services, public static IServiceCollection AddAuthClient(this IServiceCollection services, Action callerOptions, RedisConfigurationOptions redisOptions) { - ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions)); + MasaArgumentException.ThrowIfNull(callerOptions); if (services.All(service => service.ServiceType != typeof(IMultiEnvironmentUserContext))) throw new Exception("Please add IMultiEnvironmentUserContext first."); diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs index 619ee5bab..cc235e390 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs @@ -8,10 +8,7 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddMcClient(this IServiceCollection services, string mcServiceBaseAddress) { - if (string.IsNullOrWhiteSpace(mcServiceBaseAddress)) - { - throw new ArgumentNullException(nameof(mcServiceBaseAddress)); - } + MasaArgumentException.ThrowIfNullOrEmpty(mcServiceBaseAddress); return services.AddMcClient(callerOptions => { @@ -23,9 +20,23 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, s }); } + public static IServiceCollection AddMcClient(this IServiceCollection services, Func> mcServiceBaseAddressFunc) + { + MasaArgumentException.ThrowIfNull(mcServiceBaseAddressFunc); + + return services.AddMcClient(callerOptions => + { + callerOptions.UseHttpClient(DEFAULT_CLIENT_NAME, async builder => + { + builder.BaseAddress = await mcServiceBaseAddressFunc.Invoke(); + }, true).AddHttpMessageHandler(); + callerOptions.DisableAutoRegistration = true; + }); + } + public static IServiceCollection AddMcClient(this IServiceCollection services, Action callerOptions) { - ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions)); + MasaArgumentException.ThrowIfNull(callerOptions); if (services.Any(service => service.ServiceType == typeof(IMcClient))) return services; diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/ServiceCollectionExtensions.cs index 1a54d3ba1..9dc711f33 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/ServiceCollectionExtensions.cs @@ -8,31 +8,42 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddPmClient(this IServiceCollection services, string pmServiceBaseAddress) { - if (string.IsNullOrWhiteSpace(pmServiceBaseAddress)) - { - throw new ArgumentNullException(nameof(pmServiceBaseAddress)); - } + MasaArgumentException.ThrowIfNullOrEmpty(pmServiceBaseAddress); return services.AddPmClient(callerOptions => { callerOptions.UseHttpClient(DEFAULT_CLIENT_NAME, builder => { - builder.Configure = opt => opt.BaseAddress = new Uri(pmServiceBaseAddress); + builder.BaseAddress = pmServiceBaseAddress; }); callerOptions.DisableAutoRegistration = true; }); } + public static IServiceCollection AddPmClient(this IServiceCollection services, Func> pmServiceBaseAddressFunc) + { + MasaArgumentException.ThrowIfNull(pmServiceBaseAddressFunc); + + return services.AddPmClient(callerOptions => + { + callerOptions.UseHttpClient(DEFAULT_CLIENT_NAME, async builder => + { + builder.BaseAddress = await pmServiceBaseAddressFunc.Invoke(); + }, true); + callerOptions.DisableAutoRegistration = true; + }); + } + public static IServiceCollection AddPmClient(this IServiceCollection services, Action callerOptions) { - ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions)); + MasaArgumentException.ThrowIfNull(callerOptions); if (services.Any(service => service.ServiceType == typeof(IPmClient))) return services; services.AddCaller(callerOptions.Invoke); - services.AddSingleton(serviceProvider => + services.AddScoped(serviceProvider => { var callProvider = serviceProvider.GetRequiredService().Create(DEFAULT_CLIENT_NAME); var pmCaching = new PmClient(callProvider); diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/AuthClientTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/AuthClientTest.cs index f2ac105a4..c1d66a5e5 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/AuthClientTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/AuthClientTest.cs @@ -29,14 +29,14 @@ public void TestAddAuthClientNoAddMasaIdentity() public void TestAddAuthClientShouldThrowArgumentNullException() { var services = new ServiceCollection(); - Assert.ThrowsException(() => services.AddAuthClient(authServiceBaseAddress: null!, new())); + Assert.ThrowsException(() => services.AddAuthClient(authServiceBaseAddress: null!, new())); } [TestMethod] public void TestAddAuthClientShouldThrowArgumentNullException2() { var services = new ServiceCollection(); - Assert.ThrowsException(() => services.AddAuthClient(callerOptions: null!, new())); + Assert.ThrowsException(() => services.AddAuthClient(callerOptions: null!, new())); } } diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Mc.Tests/McClientTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Mc.Tests/McClientTest.cs index 7add33b0f..9cc3f982d 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Mc.Tests/McClientTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Mc.Tests/McClientTest.cs @@ -20,13 +20,13 @@ public void TestAddMcClient() public void TestAddMcClientShouldThrowArgumentNullException() { var services = new ServiceCollection(); - Assert.ThrowsException(() => services.AddMcClient(mcServiceBaseAddress: null!)); + Assert.ThrowsException(() => services.AddMcClient(mcServiceBaseAddress: null!)); } [TestMethod] public void TestAddMcClientShouldThrowArgumentNullException2() { var services = new ServiceCollection(); - Assert.ThrowsException(() => services.AddMcClient(callerOptions: null!)); + Assert.ThrowsException(() => services.AddMcClient(callerOptions: null!)); } } diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Pm.Tests/PmClientTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Pm.Tests/PmClientTest.cs index d9fbbd462..596ab48fd 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Pm.Tests/PmClientTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Pm.Tests/PmClientTest.cs @@ -41,7 +41,7 @@ public void TestAddPmClientShouldThrowArgumentNullException() { var services = new ServiceCollection(); - Assert.ThrowsException(() => services.AddPmClient("")); + Assert.ThrowsException(() => services.AddPmClient("")); } [TestMethod]