Open Closed

Call Api Without Dynamic Proxy Client From Blazor Wasm & Http Dynamic Proxy Error #1222


User avatar
1
murat.yuceer created
  • ABP Framework version: v4.2.2
  • UI type: Blazor Wasm
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

Hi, how can i get current user bearer token to call remote api without using dynamic proxy client. Can you provide full example with HttpClient object

I try to get options from IHttpClientProxy but its just have service info. I wish we could get ready to use HttpClient object for make custom call..

I try to implement devexpress DataGrid CustomData option. Its provide to call api with paging,sorting,group by,summary.. options. And it can modify IQuarable object by your input object (await DataSourceLoader.LoadAsync(query, input))

For that you have to create method with special signature (how devex DataGrid like) async Task<LoadResult> GetData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken) But dynamic proxy couldnt serialize DataSourceLoadOptionsBase object why i dont know, already devex have helper method for that options.ConvertToGetRequestUri("http://apiurl")


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

    hi

    I think you can use IAccessTokenProvider to get access token.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Http.Client.IdentityModel.WebAssembly/Volo/Abp/Http/Client/IdentityModel/WebAssembly/AccessTokenProviderIdentityModelRemoteServiceHttpClientAuthenticator.cs

  • User Avatar
    0
    murat.yuceer created

    Thanks @maliming its worked, by the way IHttpClientProxy could share current httpclient object to us (ready to use) if we need call custom api.. i need set base url and auth

  • User Avatar
    0
    murat.yuceer created

    Hi @maliming how get i get token from blazor server ? IAccessTokenProvider not avaible there

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    how get i get token from blazor server ?

    hi

    Can you explain why you want to get acces token in blazor server?

  • User Avatar
    0
    murat.yuceer created

    Hi,

    I moved us project from blazor wasm to server.

    We use devexpress datagrid for some pages => https://demos.devexpress.com/blazor/BindGridtoData#CustomData

    For implement server side paging, sort, group by, summary we use https://github.com/DevExpress/DevExtreme.AspNet.Data as devex suggested

    Basically DevExtreme.AspNet.Data manipulate IQueryable object by input object (take,skip,where,select,group by etc..)

    To provide that, my api have DataSourceLoadOptionsBase input object => https://github.com/DevExpress/DevExtreme.AspNet.Data/blob/master/net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs

    you can see in the example https://docs.devexpress.com/Blazor/DevExpress.Blazor.DataSourceLoadOptionsExtensions.ConvertToGetRequestUri%28DevExtreme.AspNet.Data.DataSourceLoadOptionsBase-System.String%29#remarks

    using var response = await Client.GetAsync(options.ConvertToGetRequestUri(dataEndpointUri), cancellationToken);

    options type is DataSourceLoadOptionsBase, extension method convert it GetRequestUri format.

    But i can not intervene dynamic http proxy system before send request to api.. because of that i call api with http client manual way. You can see example below from devex

    protected async Task<LoadResult> LoadCustomData(string dataEndpointUri, DataSourceLoadOptionsBase options, CancellationToken cancellationToken) {
            using var response = await Client.GetAsync(options.ConvertToGetRequestUri(dataEndpointUri), cancellationToken);
            response.EnsureSuccessStatusCode();
            using var responseStream = await response.Content.ReadAsStreamAsync();
            return await JsonSerializer.DeserializeAsync<LoadResult>(responseStream, cancellationToken: cancellationToken);
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    When you use blazor server mode, you should not use HttpClient manually.

    Maybe you can take a look at this https://github.com/DevExpress/Blazor#devexpress-ui-for-blazor

  • User Avatar
    0
    murat.yuceer created

    Hi @maliming, i have information about devex blazor. Did you understand my example ?

    If you want to use full server side skills of dxdatagrid, your api have to request DataSourceLoadOptionsBase object (contains some array types, custom IList Filter vb.. look https://github.com/DevExpress/DevExtreme.AspNet.Data/blob/master/net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs)

    But when i use dynamic http proxy, its not serialize get url correct for DataSourceLoadOptionsBase object type, already devex have extension method for that dataSourceLoadOptionsBase.ConvertToGetRequestUri(uri) because of that i need to do custom Get request (I need to determine the query part of the address)

    Also i have IModelBinder on httpapi side (implemented from devex document)

        public class DataSourceLoadOptionsBinder : IModelBinder
        {
            public Task BindModelAsync(ModelBindingContext bindingContext)
            {
                var model = Activator.CreateInstance(bindingContext.ModelType);
                DataSourceLoadOptionsParser.Parse((DataSourceLoadOptionsBase)model, key => bindingContext.ValueProvider.GetValue(key).FirstOrDefault());
                bindingContext.Model = model;
                bindingContext.Result = ModelBindingResult.Success(model);
                return Task.CompletedTask;
            }
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can u try to get token from the HttpConext?

  • User Avatar
    0
    murat.yuceer created

    Hi mailming i found that from abp source code, i guess its work independent (wasm or server)

        public async Task&lt;string&gt; SendAsync(string url)
        {
            var client = HttpClientFactory.CreateClient();
            var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
            AddHeaders(requestMessage);
    
            var uri = new Uri(url, UriKind.RelativeOrAbsolute);
            if (!uri.IsAbsoluteUri)
            {
                var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Default");
                client.BaseAddress = new Uri(remoteServiceConfig.BaseUrl);
                await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, requestMessage, new RemoteServiceConfiguration(remoteServiceConfig.BaseUrl), string.Empty));
            }
    
            var response = await client.SendAsync(requestMessage);
            
            return await response.Content.ReadAsStringAsync();
        }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Yes, but it is suitable for a console-like environment. You can try.

  • User Avatar
    0
    murat.yuceer created

    Just i try to find interface in abp to get token independent wasm/server/console

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    independent wasm/server/console

    For mvc https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Http.Client.IdentityModel.Web/Volo/Abp/Http/Client/IdentityModel/Web/HttpContextIdentityModelRemoteServiceHttpClientAuthenticator.cs#L45

    For WASM https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Http.Client.IdentityModel.WebAssembly/Volo/Abp/Http/Client/IdentityModel/WebAssembly/AccessTokenProviderIdentityModelRemoteServiceHttpClientAuthenticator.cs#L50

    For console https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs#L21 https://github.com/abpframework/abp/blob/7574dc088d7ea3127aaecca92e3a7dc3bdc430f7/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs#L171

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54