使用TestServer时,同服务器内HttpClient调用其他控制器失败
问题:ASP.NET Core TestServer集成测试中HttpClient调用微服务返回404
项目配置说明
我有一个ASP.NET Core Server项目,服务配置如下:
services.AddControllers() .AddApplicationPart(gatewayAssembly) .AddApplicationPart(identitiesMicroserviceAssembly) .AddApplicationPart(exceptionsMicroserviceAssembly) .etc ...;
网关程序集内的控制器通过HttpClient与微服务程序集内的控制器通信,HttpClient的基础Uri通过appsettings.json配置。开发环境的配置为:
"MicroserviceUris": { "Identities": "https://localhost:7138", "Exceptions": "https://localhost:7138", "Entitlements": "https://localhost:7138", "Organisations": "https://localhost:7138", "Catalogs": "https://localhost:7138", "Taxes": "https://localhost:7138", "Warehouse": "https://localhost:7138" },
该配置可在微服务迁移时仅修改配置文件即可适配,通过Postman调用网关接口一切正常。
测试环境异常情况
使用TestServer进行集成测试时,integrationtestappsettings.json中配置的微服务基础Uri为http://localhost:
"MicroserviceUris": { "Identities": "http://localhost", "Exceptions": "http://localhost", "Entitlements": "http://localhost", "Organisations": "http://localhost", "Catalogs": "http://localhost", "Taxes": "http://localhost", "Warehouse": "http://localhost" },
测试项目直接调用网关路由正常,但网关控制器通过HttpClient调用微服务接口时返回404。直接从测试项目调用微服务路由http://localhost/api/identities/users是正常的,从日志可以看出二次请求返回404,问题应出在TestServer上创建的HttpClient。
相关日志信息
18:27:17.499info: Microsoft.AspNetCore.Hosting.Diagnostics[1] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M Request starting HTTP/1.1 POST http://localhost/api/uc/useraccountmanagement/registernewuser application/json;+charset=utf-8 - 18:27:17.531info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M Executing endpoint 'Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController.PostAsync (Cosmos.Application.UseCases.UserAccountManagement.Controllers)' 18:27:17.545info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M => Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController.PostAsync (Cosmos.Application.UseCases.UserAccountManagement.Controllers) Route matched with {action = "Post", controller = "RegisterNewUser"}. Executing controller action with signature System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] PostAsync(Cosmos.Application.UseCases.UserAccountManagement.Models.RegisterNewUser.RegisterNewUserModel, System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Routing.EndpointDataSource]) on controller Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController (Cosmos.Application.UseCases.UserAccountManagement.Controllers). 18:27:17.572info: System.Net.Http.HttpClient.IdentitiesProxy.LogicalHandler[100] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M => Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController.PostAsync (Cosmos.Application.UseCases.UserAccountManagement.Controllers) => HTTP POST http://localhost/api/identities/users Start processing HTTP request POST http://localhost/api/identities/users 18:27:17.573info: System.Net.Http.HttpClient.IdentitiesProxy.ClientHandler[100] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M => Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController.PostAsync (Cosmos.Application.UseCases.UserAccountManagement.Controllers) => HTTP POST http://localhost/api/identities/users Sending HTTP request POST http://localhost/api/identities/users 18:27:17.612info: System.Net.Http.HttpClient.IdentitiesProxy.ClientHandler[101] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M => Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController.PostAsync (Cosmos.Application.UseCases.UserAccountManagement.Controllers) => HTTP POST http://localhost/api/identities/users Received HTTP response headers after 37.8005ms - 404 18:27:17.612info: System.Net.Http.HttpClient.IdentitiesProxy.LogicalHandler[101] => RequestPath:/api/uc/useraccountmanagement/registernewuser RequestId:0HMM1MRES2K3M => Cosmos.Application.UseCases.UserAccountManagement.Controllers.RegisterNewUser.RegisterNewUserController.PostAsync (Cosmos.Application.UseCases.UserAccountManagement.Controllers) => HTTP POST http://localhost/api/identities/users End processing HTTP request after 42.4879ms - 404
内容的提问来源于stack exchange,提问作者Neil W
相关产品推荐
相关产品推荐

