Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
Calling a non static method using blazor interop from js

Working on a blazor application, it has a method that needs to be called from the JS ! When I try to call the method without static keyword I get this error -

 The assembly 'AssemblyName' does not contain a public invokable method with [JSInvokableAttribute("INIT_COMPLIANCE")].

As soon as I add static it recognizes and loads it fine.

Here's my code in blazor component -

protected override Task OnInitializedAsync()
    {
        jsToDotNetBridgeReference = DotNetObjectReference.Create(this);
        js.InvokeVoidAsync("SetDotNetReference", jsToDotNetBridgeReference);
        return base.OnInitializedAsync();
    }


[JSInvokableAttribute("INIT_COMPLIANCE")]
public async void InitComplianceComponent(string token)
{
    id_token = token;
    Console.WriteLine(token);
    await GetData();
}

JS -

 self.addNewCompliance = function () {
        const url = someurl;
        var resource = window.authContext.getResourceForEndpoint(url);
        window.authContext.acquireToken(resource, function (error, token) {
            // Handle ADAL Error
            if (error || !token) {
                console.log('ADAL Error Occurred: ' + error);
                return;
            } else {
                Blazor.start().then(() => {
                    window.InitComplianceComponent(window.DotNet, token); 
                })
                //window.InitComplianceComponent(window.DotNet, token);
            }
        });
    }

BlazorInterop.js

 InitComplianceComponent = function (dotnetObj, token) {
        dotnetObj.invokeMethod("AssemblyName", "INIT_COMPLIANCE", token);
        
    }

I do not want to use static keyword as the GetData is non-static method and I cannot call it from the static method.

Where am I wrong here.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can do like this:

protected override async Task OnInitializedAsync()
{
    await JSRuntime.InvokeVoidAsync("yourJsFunctionName", 
    DotNetObjectReference.Create(this), nameof(MethodToInvokeFromJs));
}

[JSInvokable]
public void MethodToInvokeFromJs()
{
    //your code
    StateHasChanged();
}

and your js code will look like this:

function yourJsFunctionName(instance, callback) {
    instance.invokeMethodAsync(callback);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda