Starts in:
1 DAY
10 HRS
29 MIN
54 SEC
Starts in:
1 D
10 H
29 M
54 S
Open Closed

AppService Returns Fake or Real Data based on Client Calls #5433


User avatar
0
sukhdeep.dhillon created

If you're creating a bug/problem report, please include the followings:

  • ABP Framework version: v6.0.2
  • UI type: MVC
  • DB provider: EF Core

Hi,

I want my AppServices to return Fake or Real data based on how clients call. Client can either ask for Real or Test data. so what is the best way to implement it because I have multiple AppServices and each has multiple actions.

Should I create 2 AppServices: RealAppService & FakeAppService

plus, what is the best way for client to ask for real or fake data. should client pass parameter in request header and if they do in the request header. how can I intercept the request and based on header value instantiate the real or fake app service.


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can add a parameter on the request header. and inject the IHttpContextAccessor to try to get this parameter.

    I think one AppService is enough.

  • User Avatar
    0
    sukhdeep.dhillon created

    this mean I need to inject IHttpContextAccessor in each AppService and read header value in each app service methods to make decision for fake or real data.

    just wondering, is there any other approach ?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You add consider adding a factory method to resolve the IYourAppService.

    Fake code:

    context.Services.AddTransient<IIdentityUserAppService>(serviceProvider =>
    {
        if (serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext?.Request.IsHttps)
        {
            serviceProvider.GetRequiredService<IdentityUserAppService>();
        }
        return serviceProvider.GetRequiredService<MyIdentityUserAppService>();;
    });
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06