michielpost / MetaMask.Blazor

Use MetaMask with Blazor WebAssembly
MIT License
44 stars 21 forks source link

Blazor server crashing on UserDeniedException #23

Closed JapstersCavern closed 6 months ago

JapstersCavern commented 6 months ago

I'm just trying out your library, and really appreciate you making this available!

I'm experiencing a confusing problem. When I try to connect to Metamask in a Blazor server app (.Net 8), and the user clicks cancel, then the Blazor server app terminates even though I have a 'catch' block to catch all exceptions.

image

This is my code:

I don't understand how this is crashing the server - Do you have any suggestions please?

@using Blazorise
@using MetaMask.Blazor
@inject IMetaMaskService MetaMaskService

@code {

    bool toastWSuccessVisible = false;
    bool toastWFailedVisible = false;

    private string toastWUnknownMessage = "";

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (!firstRender)
        {
            return;
        }

        bool hasMetaMask = await MetaMaskService.HasMetaMask();

        if (hasMetaMask)
        {
            try
            {
                await MetaMaskService.ConnectMetaMask();
                toastWSuccessVisible = true;
            }
            catch (Exception ex)
            {
                toastWFailedVisible = true;
                toastWUnknownMessage = ex.Message;
            }
        }
    }
}
michielpost commented 6 months ago

MetaMask.Blazor was primarily designed to work with Blazor WASM. The ConnectMetaMask invokes JavaScript on the client to see if the MetaMask plugin is installed. This is not something that can run on the server. So I think you need to make sure all of this code runs on the client side.

mrpmorris commented 6 months ago

Doesn't it use JSRuntime.InvokeAsync to call through to the browser?

That should work on Blazor Server once OnAfterRender has executed.

michielpost commented 6 months ago

I added a new sample to show usage when using RenderMode InteractiveWebAssembly located here: https://github.com/michielpost/MetaMask.Blazor/tree/master/Metamask.Blazor.SampleWebApp

Can you try it?

Maybe the problem is only in the debugger detachting and Visual Studio stopping the web app. I've seen that before, for example when triggering file downloads.

JapstersCavern commented 6 months ago

Thanks for this help, Michiel - I'll take a look and see if it works for my scenario!

Once again, many thanks!

JapstersCavern commented 6 months ago

After forking your repo and putting breakpoints into the js and cs it seems it crashes after Blazor invokes requestAccounts.

https://github.com/michielpost/MetaMask.Blazor/blob/c0c16bd8b319093430855ce05f67d07fef4f3957/MetaMask.Blazor/wwwroot/metaMaskJsInterop.js#L33

If I connect and allow access to a wallet then the Visual Studio debugger session just stops immediately. Oddly though, if the server is run with dotnet run then there are no problems at all. So it looks like it might be a problem with the Visual Studio debugger.

Thanks for your time!!