Open Closed

Service to Service communication #8012


User avatar
0
sgarad created
  • ABP Framework version: 8.2.1
  • UI Type: Angular
  • Database System: EF Core (MySQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes Hi There,

I've implemented microservice to microservice synchronous communication as stated here. The soulution works just fine. I'm able to get data from cross service but every time I hit the endpoint on other service I see a new token is generated and it is causing my OpenIddictTokens just keeps increasing. Can't the once generated token be re-used again for next service call? I see the token is generated with 1 hour of expiration but next service call(Same endpoint) does not consider existing token and generates the new token.

Thanks Krishna


11 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The services are stateless; it will not store the token.

    Or you can consider to integration services https://abp.io/docs/latest/framework/api-development/integration-services

  • User Avatar
    0
    sgarad created

    Hi liangshiwei,

    Thanks for the quick reply. If I moved to Integration Service do I still need aa access_token while communication from service to service? When I followed intergration implemntation I gets UnAuthorized exception.

    Thanks Krishna

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi

    Integration services don't need acccess_token.

    Could you share your code?

  • User Avatar
    0
    sgarad created

    Hi liangshiwei,

    Here is the code.

    VendorModule:

    public interface IVendorIntegrationService : IApplicationService, ITransientDependency
    {
        Task<ListResultDto<VendorDto>> GetAllAsync();
    }
    
    [Authorize(VendorManagementPermissions.Vendors.Default)]
    [IntegrationService]
    public class VendorIntegrationService : ApplicationService,IVendorIntegrationService
    {
        private readonly VendorManager _vendorManager;
        private readonly IRepository<Vendor, int> _vendorRepository;
    
        public VendorIntegrationService(VendorManager vendorManager, IRepository<Vendor, int> vendorRepository)
        {
            _vendorManager = vendorManager;
            _vendorRepository = vendorRepository;
        }
        [Authorize(VendorManagementPermissions.Vendors.Default)]
        public async Task<ListResultDto<VendorDto>> GetAllAsync()
        {
            var vendors = await _vendorRepository.GetListAsync();
            return new ListResultDto<VendorDto>(
                ObjectMapper.Map<List<Vendor>, List<VendorDto>>(vendors)
            );
        }
    }
    
    [IntegrationService]
    public class VendorIntegrationController:IVendorIntegrationService
    {
        private readonly IVendorIntegrationService _vendorIntegrationService;
        public VendorIntegrationController(IVendorIntegrationService vendorIntegrationService)
        {
            _vendorIntegrationService = vendorIntegrationService;
        }
        public Task<ListResultDto<VendorDto>> GetAllAsync()
        {
            return _vendorIntegrationService.GetAllAsync();
        }
    }
    
    VendorManagementApplicationModule:AbpModule
    
     private void ConfigureIntegrationServices()
     {
         Configure<AbpAspNetCoreMvcOptions>(options =>
         {
             options.ExposeIntegrationServices = true;
         });
     }
    

    If I'm trying to access this from web-gateway I get 404 not found exception but same works from Vendor service swagger.

    10/3/2024 11:18:38 PM [Information] Request starting "HTTP/1.1" "GET" "http"://"localhost:44397""""/integration-api/vendorservice/vendor""" - null null 10/3/2024 11:18:38 PM [Information] Request finished "HTTP/1.1" "GET" "http"://"localhost:44397""""/integration-api/vendorservice/vendor""" - 404 0 null 1.6004ms 10/3/2024 11:18:38 PM [Information] Request reached the end of the middleware pipeline without being handled by application code. Request path: "GET" "http"://"localhost:44397""""/integration-api/vendorservice/vendor", Response status code: 404

    Accounts Payable Module:

     private readonly IVendorAppService _vendorAppService;
     private readonly IVendorIntegrationService _vendorIntegrationService;
     public SampleAppService(IVendorAppService vendorAppService, IVendorIntegrationService vendorIntegrationService)
     {
         _vendorAppService = vendorAppService;
         _vendorIntegrationService = vendorIntegrationService;
     }
     
      public async Task<ListResultDto<VendorDto>> GetVendorFromAPIntegration()
      {
          return await _vendorIntegrationService.GetAllAsync();
      }
    

    From Accounts payable when I make request first I got 404 so I added Remoteservice configuration in appsettings.json as below.

     "RemoteServices": {
       "AbpIdentity": {
         "BaseUrl": "http://localhost:44377/"
       },
       "Default": { //This is the URL of web-gateway. Which will be used to call other services(Vendor Service).
         "BaseUrl": "http://localhost:44397/",
         "UseCurrentAccessToken": "false"
       },
       "VendorService": {
         "BaseUrl": "http://localhost:44323/",
         "RemoteName": "VendorService",
         "UseCurrentAccessToken": "true"
       }
     },
    

    After adding this I started getting 401 unauthorized. 10/3/2024 11:25:08 PM [Information] Request starting "HTTP/1.1" "GET" "http"://"localhost:44305""""/api/ap-management/sample/vendor-from-aPIntegration""" - null null 10/3/2024 11:25:09 PM [Information] Executing endpoint '"CastandCrew.PSL.AccountsPayableManagement.Samples.SampleController.GetVendorFromAPIntegration (CastandCrew.PSL.AccountsPayableManagement.HttpApi)"' 10/3/2024 11:25:09 PM [Information] Route matched with "{area = \"accountsPayableManagement\", action = \"GetVendorFromAPIntegration\", controller = \"Sample\", page = \"\"}". Executing controller action with signature "System.Threading.Tasks.Task1[Volo.Abp.Application.Dtos.ListResultDto`1[CastandCrew.PSL.VendorManagement.Vendor.VendorDto]] GetVendorFromAPIntegration()" on controller "CastandCrew.PSL.AccountsPayableManagement.Samples.SampleController" ("CastandCrew.PSL.AccountsPayableManagement.HttpApi"). 10/3/2024 11:25:09 PM [Warning] Could not find IdentityClientConfiguration for VendorService. Either define a configuration for VendorService or set a default configuration. 10/3/2024 11:25:09 PM [Information] Start processing HTTP request "GET" "http://localhost:44323/integration-api/vendorservice/vendor?api-version=1.0" 10/3/2024 11:25:09 PM [Information] Sending HTTP request "GET" "http://localhost:44323/integration-api/vendorservice/vendor?api-version=1.0" 10/3/2024 11:25:09 PM [Information] Received HTTP response headers after 26.7839ms - 401 10/3/2024 11:25:09 PM [Information] End processing HTTP request after 27.3146ms - 401 10/3/2024 11:25:09 PM [Error] ---------- RemoteServiceErrorInfo ---------- { "code": "Unauthorized", "message": "Unauthorized", "details": null, "data": null, "validationErrors": null }

    10/3/2024 11:25:09 PM [Error] Volo.Abp.Http.Client.AbpRemoteCallException: Unauthorized at Volo.Abp.Http.Client.ClientProxying.ClientProxyBase1.ThrowExceptionForResponseAsync(HttpResponseMessage response) at Volo.Abp.Http.Client.ClientProxying.ClientProxyBase1.RequestAsync(ClientProxyRequestContext requestContext) at Volo.Abp.Http.Client.ClientProxying.ClientProxyBase1.RequestAsync[T](ClientProxyRequestContext requestContext) at Volo.Abp.Http.Client.DynamicProxying.DynamicHttpProxyInterceptorClientProxy1.CallRequestAsync[T](ClientProxyRequestContext requestContext) at Volo.Abp.Http.Client.DynamicProxying.DynamicHttpProxyInterceptor1.CallRequestAsync[T](ClientProxyRequestContext context) at Volo.Abp.Http.Client.DynamicProxying.DynamicHttpProxyInterceptor1.GetResultAsync(Task task, Type resultType) at Volo.Abp.Http.Client.DynamicProxying.DynamicHttpProxyInterceptor1.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Validation.ValidationInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at CastandCrew.PSL.AccountsPayableManagement.Samples.SampleAppService.GetVendorFromAPIntegration() in D:\PROJECTS\PSLWEB2\psl-modules\CastandCrew.PSL.AccountsPayableManagement\src\CastandCrew.PSL.AccountsPayableManagement.Application\Samples\SampleAppService.cs:line 23 at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.GlobalFeatures.GlobalFeatureInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Auditing.AuditingInterceptor.ProceedByLoggingAsync(IAbpMethodInvocation invocation, AbpAuditingOptions options, IAuditingHelper auditingHelper, IAuditLogScope auditLogScope) at Volo.Abp.Auditing.AuditingInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Authorization.AuthorizationInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Validation.ValidationInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo) at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) at CastandCrew.PSL.AccountsPayableManagement.Samples.SampleController.GetVendorFromAPIntegration() in D:\PROJECTS\PSLWEB2\psl-modules\CastandCrew.PSL.AccountsPayableManagement\src\CastandCrew.PSL.AccountsPayableManagement.HttpApi\Samples\SampleController.cs:line 44 at lambda_method1762(Closure, Object) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.&lt;InvokeActionMethodAsync&gt;g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.

    10/3/2024 11:25:09 PM [Error] Code:Unauthorized 10/3/2024 11:25:09 PM [Error] Details: 10/3/2024 11:25:09 PM [Information] Executing "ObjectResult", writing value of type '"Volo.Abp.Http.RemoteServiceErrorResponse"'. 10/3/2024 11:25:09 PM [Information] Executed action "CastandCrew.PSL.AccountsPayableManagement.Samples.SampleController.GetVendorFromAPIntegration (CastandCrew.PSL.AccountsPayableManagement.HttpApi)" in 44.5462ms 10/3/2024 11:25:09 PM [Information] Executed endpoint '"CastandCrew.PSL.AccountsPayableManagement.Samples.SampleController.GetVendorFromAPIntegration (CastandCrew.PSL.AccountsPayableManagement.HttpApi)"' 10/3/2024 11:25:09 PM [Information] AUDIT LOG: [401: GET ] /api/ap-management/sample/vendor-from-aPIntegration

    • UserName - UserId : admin - 3a14dab1-0282-f76d-03ab-e4df6ead01b5
    • ClientIpAddress : ::1
    • ExecutionDuration : 46
    • Actions:
      • CastandCrew.PSL.AccountsPayableManagement.Samples.SampleAppService.GetVendorFromAPIntegration (32 ms.) {}
      • CastandCrew.PSL.AccountsPayableManagement.Samples.SampleController.GetVendorFromAPIntegration (35 ms.) {}-

    10/3/2024 11:25:09 PM [Information] Request finished "HTTP/1.1" "GET" "http"://"localhost:44305""""/api/ap-management/sample/vendor-from-aPIntegration""" - 401 null "application/json; charset=utf-8" 84.3498ms

    `

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You should not use Authorize for the integration service.

    If you need to use Authorize, you don't needs to change anything.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    HI,

    BTW, ABP will try to get the token from the cache; it won't get a new token every time. https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs#L84

  • User Avatar
    0
    sgarad created

    HI,

    BTW, ABP will try to get the token from the cache; it won't get a new token every time. https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs#L84

    Hi,

    The whole point is it's not picking it from cache and generating the new one on every request. I hit the service continueously and you can see the tokens are generated for each request.

    I see this tokens are even loaded in my cache but the Payload is null.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I see this tokens are even loaded in my cache but the Payload is null.

    I will check it.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    May I ask, are you running in development env? For development environments, the cache expiration time is 5seconds

    f

  • User Avatar
    0
    sgarad created

    Hi,

    May I ask, are you running in development env? For development environments, the cache expiration time is 5seconds

    f

    Yes it's development environment but I use hosted Auth server and Identity service. Is there any way to change expiration?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can usually ignore it in the development environment, or you can change the current environment.

    Unfortunately there is currently no configuration to change the cache expiration time during development

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09