New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient inject isn't working in Blazor 0.8 #1780

Closed
thainbreese opened this Issue Feb 6, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@thainbreese
Copy link

thainbreese commented Feb 6, 2019

Hi

I have created a new blazor component and tried to call get data from json file.

@function {
        protected override async Task OnInitAsync()
        {
           data = await Http.GetJsonAsync<GridItem[]>("sample-data/data.json");
        }
}

And got this error:

Uncaught (in promise) Error: System.InvalidOperationException: Cannot provide a value for property 'Http' on type 'BlazorGrid.App.Components.GridComponent'. There is no registered service of type 'System.Net.Http.HttpClient'.
   at Microsoft.JSInterop.DotNetDispatcher.InvokeSynchronously(String assemblyName, String methodIdentifier, Object targetInstance, String argsJson)
   at Microsoft.JSInterop.DotNetDispatcher.BeginInvoke(String callId, String assemblyName, String methodIdentifier, Int64 dotNetObjectId, String argsJson)
    at endInvokeDotNetFromJS (components.server.js:23)
    at components.server.js:23
    at new Promise (<anonymous>)
    at e.beginInvokeJSFromDotNet (components.server.js:23)
    at components.server.js:16
    at Array.forEach (<anonymous>)
    at e.invokeClientMethod (components.server.js:16)
    at e.processIncomingData (components.server.js:16)
    at e.connection.onreceive (components.server.js:16)
    at WebSocket.i.onmessage (components.server.js:16)

What's wrong here?

This is working in Blazor 0.7, but not working in Blazor 0.8

@danroth27

This comment has been minimized.

Copy link
Member

danroth27 commented Feb 6, 2019

Hi @thainbreese,

It looks like your app is actually setup to use ASP.NET Core Razor Components instead of Blazor. Razor Components apps use the components.server.js script, but for Blazor you use components.webassemlby.js. Blazor Apps register a preconfigured HttpClient as a service that is available through dependency injection, but Razor Component apps do not.

In a Razor Components app you can just create new HttpClient instances, or if you want, you can register HttpClient as a service yourself. See https://github.com/Suchiman/BlazorDualMode for an example of how to do this.

I hope this helps!

@danroth27 danroth27 closed this Feb 6, 2019

@danroth27 danroth27 added the question label Feb 6, 2019

@danroth27 danroth27 added this to the Discussion milestone Feb 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment