jsakamoto / Toolbelt.Blazor.HeadElement

Head element support (change the document title, "meta" elements such as OGP, and "link" elements) for Blazor apps.
https://demo-blazor-headelement.azurewebsites.net/
Mozilla Public License 2.0
160 stars 11 forks source link

Unhandled exception in circuit #39

Open Alerinos opened 1 year ago

Alerinos commented 1 year ago

Hi, I have some problem with the application. There is probably something looping in the nginx configuration that caused the application to get ddos/flood.

I got error 500 status probably because of this error.

It could be caused by something else but I figured I'd report the issue.

Json error:

{"Timestamp":"2022-12-10T09:22:18.5782563+01:00","Level":"Error","MessageTemplate":"Unhandled exception in circuit '{CircuitId}'.","RenderedMessage":"Unhandled exception in circuit '\"UPcusR1_t3zFRYb5laCQXsZwm6hJzBtpeFrz52Zy4MU\"'.","Exception":"Microsoft.JSInterop.JSDisconnectedException: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.\n   at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)\n   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)\n   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)\n   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)\n   at Microsoft.JSInterop.Implementation.JSObjectReference.DisposeAsync()\n   at Toolbelt.Blazor.HeadElement.HeadElementHelperService.EnsureScriptEnabledAsync[T]()\n   at Toolbelt.Blazor.HeadElement.HeadElementHelperService.InvokeJSAsync[T](String identifier, Object[] args)\n   at Toolbelt.Blazor.HeadElement.HeadElementHelperService.GetDefaultTitleAsync()\n   at Toolbelt.Blazor.HeadElement.HeadElementHelperService.GetDefaultsAsync()\n   at Toolbelt.Blazor.HeadElement.HeadElementHelperService.ResetIfNeededAsync()\n   at Toolbelt.Blazor.HeadElement.HeadElementHelperService.SetTitleAsync(String title)\n   at Stand.Libraries.Website.AppState.Head.SetTitleAsync(String title) in C:\\Git\\Stand\\Source\\Libraries\\Stand.Libraries.Website\\AppState\\Head.cs:line 24\n   at Grajse.Areas.Web.Pages.Index.OnInitializedAsync() in C:\\Git\\Stand\\Source\\Projects\\Grajse\\Areas\\Web\\Pages\\Index.razor:line 56\n   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()\n   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)","Properties":{"CircuitId":"UPcusR1_t3zFRYb5laCQXsZwm6hJzBtpeFrz52Zy4MU","EventId":{"Id":111,"Name":"CircuitUnhandledException"},"SourceContext":"Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost","TransportConnectionId":"a53Zn0DqAw0MWHbf5vYLrw","RequestId":"0HMMQGRTPT3J1:00000001","RequestPath":"/_blazor","ConnectionId":"0HMMQGRTPT3J1"}}

Parse error:

{
"Timestamp":"2022-12-10T09:22:18.5782563+01:00",
"Level":"Error",
"MessageTemplate":"Unhandled exception in circuit '{CircuitId}'.",
"RenderedMessage":"Unhandled exception in circuit '\"UPcusR1_t3zFRYb5laCQXsZwm6hJzBtpeFrz52Zy4MU\"'.",
"Exception":"Microsoft.JSInterop.JSDisconnectedException: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.\n at 
Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)\n at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)\n at 
Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)\n at 
Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)\n at 
Microsoft.JSInterop.Implementation.JSObjectReference.DisposeAsync()\n at 
Toolbelt.Blazor.HeadElement.HeadElementHelperService.EnsureScriptEnabledAsync[T]()\n at 
Toolbelt.Blazor.HeadElement.HeadElementHelperService.InvokeJSAsync[T](String identifier, Object[] args)\n at 
Toolbelt.Blazor.HeadElement.HeadElementHelperService.GetDefaultTitleAsync()\n at 
Toolbelt.Blazor.HeadElement.HeadElementHelperService.GetDefaultsAsync()\n at 
Toolbelt.Blazor.HeadElement.HeadElementHelperService.ResetIfNeededAsync()\n at 
Toolbelt.Blazor.HeadElement.HeadElementHelperService.SetTitleAsync(String title)\n at 
Stand.Libraries.Website.AppState.Head.SetTitleAsync(String title) in 
C:\\Git\\Stand\\Source\\Libraries\\Stand.Libraries.Website\\AppState\\Head.cs:line 24\n at 
Grajse.Areas.Web.Pages.Index.OnInitializedAsync() in 
C:\\Git\\Stand\\Source\\Projects\\Grajse\\Areas\\Web\\Pages\\Index.razor:line 56\n at 
Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()\n at 
Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState 
owningComponentState)",

"Properties":{
"CircuitId":"UPcusR1_t3zFRYb5laCQXsZwm6hJzBtpeFrz52Zy4MU",
"EventId":{
"Id":111,
"Name":"CircuitUnhandledException"
},
"SourceContext":"Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost",
"TransportConnectionId":"a53Zn0DqAw0MWHbf5vYLrw",
"RequestId":"0HMMQGRTPT3J1:00000001",
"RequestPath":"/_blazor",
"ConnectionId":"0HMMQGRTPT3J1"
}
}

my provider:


namespace Stand.Libraries.Website.AppState;

public class Head
{
    public readonly IHeadElementHelper _headElementHelper;

    public List<MetaElement> MetaElements = new();
    public List<LinkElement> LinkElements = new();

    public static string? TitlePrefix { get; set; }

    public Head(IHeadElementHelper HeadElementHelper)
        => _headElementHelper = HeadElementHelper;

    public async Task SetTitleAsync(string title)
        => await _headElementHelper.SetTitleAsync($"{TitlePrefix}{title}");

    public async Task SetDescriptionAsync(string description)
        => await _headElementHelper.SetMetaElementsAsync(
            ByName("description", description)
        );

    public async Task SetMetaAsync(string property, string content)
        => await _headElementHelper.SetMetaElementsAsync(
            ByProp(property, content)
        );

    public async Task SetLinkAsync(List<Link> links)
    {
        links.ForEach(x => LinkElements.Add(new LinkElement(x.Rel, x.Href)));   
        await _headElementHelper.SetLinkElementsAsync(LinkElements.ToArray());
    }

    public async Task SetMetaAsync(List<Meta> metas)
    {
        var element = new List<MetaElement>();
        metas.ForEach(x => element.Add(ByProp(x.Property, x.Content)));
        await _headElementHelper.SetMetaElementsAsync(element.ToArray());
    }

    public class Meta
    {
        public string Property { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
    }

    public class Link
    {
        public string Rel { get; set; } = string.Empty;
        public string Href { get; set; } = string.Empty;
    }

}

Index:

    protected override async Task OnInitializedAsync()
        => await Head.SetTitleAsync(Culture.Translate("Home Page"));

Details of my problem described elsewhere: https://github.com/dotnet/aspnetcore/issues/45522#issuecomment-1345131356

jsakamoto commented 1 year ago

@Alerinos Could you try the latest version?

I hope the commit below fixes the problem.

Alerinos commented 1 year ago

@jsakamoto Thank you for the quick fix, unfortunately this is a difficult bug to trigger due to the large number of users and ddos attack. I'll let you know if it happens again, thanks again.

mysteryx93 commented 1 year ago

I'm also getting this error when publishing the app on the server with .NET 7 and all packages updated. It works locally but I'm unable to deploy on Ubuntu.

Microsoft.JSInterop.JSDisconnectedException: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed. Mar 02 20:06:00 localhost kestrel-store[5806]: Unhandled exception in circuit 'Z91CCAci1dQxIe0FpYEmGdDetGmo6AVhTCMJw_QYo48'. Mar 02 20:06:00 localhost kestrel-store[5806]: fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]

mysteryx93 commented 1 year ago

Found my problem, there are files to deploy under wwwroot/_content, copied those and it works