Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The current stack frame was not found in a loaded module. Source cannot be shown for this location"


This issue happend whenever calls the api

Actually whenever the api calls at the time monitor the response because If

api failed or resource not found at the time we are using

HttpClientInterceptor concept in blazor webassembly. This can be used for if

response not successed

HttpInterceptor :

C#
public class HttpInterceptorService
{
    private readonly HttpClientInterceptor _interceptor;
    private readonly NavigationManager _navManager;

    public HttpInterceptorService(HttpClientInterceptor interceptor, NavigationManager navManager)
    {
        _interceptor = interceptor;
        _navManager = navManager;
    }

    public void RegisterEvent() => _interceptor.AfterSend += InterceptResponse;

    privatevoid InterceptResponse(object sender, HttpClientInterceptorEventArgs e)
    {
        string message = string.Empty;
        if (!e.Response.IsSuccessStatusCode)
        {
            var statusCode = e.Response.StatusCode;

            switch (statusCode)
            {
                case HttpStatusCode.NotFound:
                    _navManager.NavigateTo("/404");
                    message = "The requested resorce was not found.";
                    break;
                case HttpStatusCode.Unauthorized:
                    _navManager.NavigateTo("/unauthorized");
                    message = "User is not authorized";
                    break;
                default:
                    _navManager.NavigateTo("/500");
                    message = "Something went wrong, please contact Administrator";
                    break;
            }

            throw new HttpResponseException(message);
        }
    }

    public void DisposeEvent() => _interceptor.AfterSend -= InterceptResponse;
}


Api calling:

C#
 [inject]
HttpInterceptorService intercept;

intercept.RegisterEvent();

 var response= httpclient.Getasync(api/getproducts)



in above before api calling I had tried to Execute the below line for monitor api

response at the time I got the issue while debug the code "Frame not in module" - "The current stack frame was not found in a loaded module. Source cannot be shown for this location"
due to this i didn't possible to check the httpsatus code if api not successed using interceptor


C#
intercept.RegisterEvent();


I had checked many solutions on search engine.Those are not worked

I had used visual studio 2022

please help me.

Thanks

What I have tried:

response at the time I got the issue while debug the code "Frame not in module" - "The current stack frame was not found in a loaded module. Source cannot be shown for this location"
due to this i didn't possible to check the httpsatus code using interceptor
Posted
Updated 9-Dec-22 8:22am
v2

1 solution

HttpClientInterceptor is not native to DotNet. Normally, you would inherit HttpClientHandler or DelegatingHandler to implement a HttpClient Interceptor that wraps the SendAsync method.

Are you using this 3rd party library: GitHub - jsakamoto/Toolbelt.Blazor.HttpClientInterceptor: Intercept all of the sending HTTP requests on a client side Blazor application.[^]. If yes, you need to raise a ticket and ask your question there.

UPDATE: Here is an example for you...

1. Server:
C#
// sample endpoints with different test responses
app.MapGet("/ok", () => Results.Ok());
app.MapGet("/bad_request", () => Results.BadRequest());
app.MapGet("/unauthorized", () => Results.Unauthorized());

2. Blazor Middleware:
C#
public class MyHttpClientHandler : DelegatingHandler 
{
    private readonly NavigationManager navigationManager;

    public MyHttpClientHandler(NavigationManager navigationManager)
    {
        this.navigationManager = navigationManager;
    }

    protected override async Task<HttpResponseMessage> SendAsync
    (
        HttpRequestMessage request,
        CancellationToken cancellationToken
    )
    {

        // Before call

        var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

        // After call
        Console.WriteLine($"[MyHttpClientHandler]
                            {(int)response.StatusCode} -
                            {response.StatusCode}");

        return response;
    }
}

3. Blazor Index.razor page:
HTML
@page "/"
@inject HttpClient httpClient

<h1>Hello, world!</h1>
<button @onclick="OKRequest">OKRequest Test</button>
<button @onclick="BadRequest">BadRequest Test</button>
<button @onclick="Unauthorized">Unauthorized Test</button>
<p>@status</p>
@code
{

    private string status = "--";

    Task OKRequest() => ProcessRequest("/ok");
    Task BadRequest() => ProcessRequest("/bad_request");
    Task Unauthorized() => ProcessRequest("/unauthorized");

    async Task ProcessRequest(string uri)
    {
        var result = await httpClient.GetAsync(uri);
        status = $"{(int)result.StatusCode} - {result.StatusCode}";
    }
}


4. Program.cs - register middleware
C#
builder.Services.AddTransient<MyHttpClientHandler>();

builder.Services.AddHttpClient
    (
        "HttpClientInterceptor.ServerAPI",
        client => client.BaseAddress = new 
            Uri(builder.HostEnvironment.BaseAddress)
    )
    .AddHttpMessageHandler<MyHttpClientHandler>();

When you run and click the buttons, watch either the Browser Developer Console window or VS Output window to see the console logging being produced by the MyHttpClientHandler Middleware.

Hope this helps!
 
Share this answer
 
v4
Comments
Krishna Veni 10-Dec-22 6:09am    
In this how DelegatingHandler used in index.razor file. There is no clarity about MyHttpClientHandler that is register program file but how will be used MyHttpClientHandler using DelegatingHandler in index.razor page i.e while call the api index page where

Please give clearly...
Graeme_Grant 10-Dec-22 9:13am    
It is bound to the HttpClient in the Program.cs class - see part 4. above.
.AddHttpMessageHandler<MyHttpClientHandler>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900