diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Alert/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Alert/ServiceCollectionExtensions.cs index abc359a3f..2630dedf0 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Alert/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Alert/ServiceCollectionExtensions.cs @@ -15,7 +15,6 @@ public static IServiceCollection AddAlertClient(this IServiceCollection services { builder.Configure = opt => opt.BaseAddress = new Uri(alertServiceBaseAddress); }) - .AddMiddleware() .UseAuthentication(); }); } @@ -31,7 +30,6 @@ public static IServiceCollection AddAlertClient(this IServiceCollection services { builder.BaseAddress = alertServiceBaseAddressFunc.Invoke(); }) - .AddMiddleware() .UseAuthentication(); }); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/AuthenticationService.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/AuthenticationService.cs index 540d7915b..343e6cc33 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/AuthenticationService.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/AuthenticationService.cs @@ -7,12 +7,15 @@ public class AuthenticationService : IAuthenticationService { private readonly TokenProvider _tokenProvider; private readonly JwtTokenValidator? _jwtTokenValidator; + private readonly IMultiEnvironmentContext _multiEnvironmentContext; public AuthenticationService(TokenProvider tokenProvider, - JwtTokenValidator? jwtTokenValidator) + JwtTokenValidator? jwtTokenValidator, + IMultiEnvironmentContext multiEnvironmentContext) { _tokenProvider = tokenProvider; _jwtTokenValidator = jwtTokenValidator; + _multiEnvironmentContext = multiEnvironmentContext; } public async Task ExecuteAsync(HttpRequestMessage requestMessage) @@ -24,5 +27,8 @@ public async Task ExecuteAsync(HttpRequestMessage requestMessage) if (!_tokenProvider.AccessToken.IsNullOrWhiteSpace()) requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _tokenProvider.AccessToken); + + if (!string.IsNullOrEmpty(_multiEnvironmentContext.CurrentEnvironment) && !requestMessage.Headers.Any(x=>x.Key == IsolationConsts.ENVIRONMENT)) + requestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, _multiEnvironmentContext.CurrentEnvironment); } } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/Masa.Contrib.StackSdks.Caller.csproj b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/Masa.Contrib.StackSdks.Caller.csproj index 59b6ed67e..6cab1ddbe 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/Masa.Contrib.StackSdks.Caller.csproj +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/Masa.Contrib.StackSdks.Caller.csproj @@ -14,6 +14,8 @@ + + diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/MasaCallerClientBuilderExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/MasaCallerClientBuilderExtensions.cs index daf79ec1b..818a5c4e1 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/MasaCallerClientBuilderExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/MasaCallerClientBuilderExtensions.cs @@ -1,6 +1,9 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +using Masa.BuildingBlocks.Isolation; +using Masa.Contrib.Isolation.MultiEnvironment; + namespace Microsoft.Extensions.DependencyInjection; public static class MasaCallerClientBuilderExtensions @@ -17,10 +20,13 @@ public static IMasaCallerClientBuilder UseAuthentication( masaCallerClientBuilder.Services.AddHttpContextAccessor(); masaCallerClientBuilder.Services.TryAddScoped(); masaCallerClientBuilder.Services.TryAddScoped(s => s.GetRequiredService().Generater()); + masaCallerClientBuilder.Services.TryAddScoped(); + masaCallerClientBuilder.Services.TryAddScoped(typeof(IMultiEnvironmentContext), s => s.GetRequiredService()); masaCallerClientBuilder.UseAuthentication(serviceProvider => new AuthenticationService( serviceProvider.GetRequiredService(), - null + null, + serviceProvider.GetRequiredService() )); return masaCallerClientBuilder; } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/StackHttpClientCaller.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/StackHttpClientCaller.cs index b03c8c8e8..08e539c26 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/StackHttpClientCaller.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/StackHttpClientCaller.cs @@ -9,7 +9,8 @@ protected override void UseHttpClientPost(MasaHttpClientBuilder masaHttpClientBu { masaHttpClientBuilder.UseAuthentication(serviceProvider => new AuthenticationService( serviceProvider.GetRequiredService(), - serviceProvider.GetRequiredService() + serviceProvider.GetRequiredService(), + serviceProvider.GetRequiredService() )); base.UseHttpClientPost(masaHttpClientBuilder); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/_Imports.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/_Imports.cs index 718d4f9e6..4a8844cdb 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/_Imports.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Caller/_Imports.cs @@ -4,6 +4,8 @@ global using IdentityModel; global using IdentityModel.Client; global using Masa.BuildingBlocks.Service.Caller; +global using Masa.BuildingBlocks.StackSdks.Isolation; +global using Masa.BuildingBlocks.Isolation; global using Masa.Contrib.Service.Caller.HttpClient; global using Masa.Contrib.StackSdks.Caller; global using Microsoft.AspNetCore.Http; @@ -18,4 +20,3 @@ global using System.Reflection; global using System.Security.Claims; global using System.Security.Cryptography; - diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Isolation/EnvironmentCallerMiddleware.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Isolation/EnvironmentCallerMiddleware.cs index 64f42d040..4bf3b065f 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Isolation/EnvironmentCallerMiddleware.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Isolation/EnvironmentCallerMiddleware.cs @@ -14,7 +14,6 @@ public EnvironmentCallerMiddleware(ILoggerFactory? loggerFactory = null) public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDelegate next, CancellationToken cancellationToken = default) { - try { if (masaHttpContext.RequestMessage.Content != null && masaHttpContext.RequestMessage.Content.Headers.ContentType?.MediaType == "application/json") @@ -24,7 +23,7 @@ public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDele { PropertyNameCaseInsensitive = true }); - if (!string.IsNullOrEmpty(obj?.Environment)) + if (!string.IsNullOrEmpty(obj?.Environment) && !masaHttpContext.RequestMessage.Headers.Any(x => x.Key == IsolationConsts.ENVIRONMENT)) { masaHttpContext.RequestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, obj?.Environment); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs index 2a1de0300..94e6887a1 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Mc/ServiceCollectionExtensions.cs @@ -17,7 +17,6 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, s { builder.Configure = opt => opt.BaseAddress = new Uri(mcServiceBaseAddress); }) - .AddMiddleware() .UseAuthentication(); }); } @@ -33,7 +32,6 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, F { builder.BaseAddress = mcServiceBaseAddressFunc.Invoke(); }) - .AddMiddleware() .UseAuthentication(); }); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Scheduler/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Scheduler/ServiceCollectionExtensions.cs index eb2c66ad5..5c40b9924 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Scheduler/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Scheduler/ServiceCollectionExtensions.cs @@ -21,7 +21,6 @@ public static IServiceCollection AddSchedulerClient(this IServiceCollection serv { builder.Configure = opt => opt.BaseAddress = new Uri(schedulerServiceBaseAddress); }) - .AddMiddleware() .UseAuthentication(); }); }