

Register the two implementations of IWeatherForecastService It just so happens that on the server we can invoke the WeatherForecastService directly (without going via an HTTP request). This controller acts as the API endpoint for these requests, so the client can continue to interact with our application over HTTP. If the component is going to call WeatherForecastService directly you might wonder why we need the controller at all?ĭon’t forget, when our app runs in the browser (the client) it will still be using HTTP to fetch this data. Return await _weatherForecastService.GetForecastAsync() _weatherForecastService = weatherService Public WeatherForecastController(IWeatherForecastService weatherService) Private readonly IWeatherForecastService _weatherForecastService Public class WeatherForecastController : ControllerBase If we abstract away the logic for retrieving weather data we can make sure it works on both the server and the client. NET 5 samples demonstrate a handy alternative way to address this problem. Now we could just go ahead and register HttpClient in the Server project’s Startup.cs file, however then we’d be using HTTP to go from the Server project to itself, just to load the weather data!ĭan Roth’s. The problem is, we haven’t registered HttpClient as a dependency in the Server project, so it falls over.
/Page-1080001.png)
However, this means it will attempt to execute OnInitializedAsync on the server, at which point it will attempt to retrieve the weather data using HttpClient. If you attempt to navigate directly to FetchData your Server app will attempt to prerender it (just as it did with Counter or any of the other components). There is no registered service of type ‘’. InvalidOperationException: Cannot provide a value for property ‘Http’ on type ‘’. When you navigate to the /FetchData page and hit refresh you get an error that HttpClient hasn’t been registered. However, we were left with a bit of a problem. This means you can serve your application nice and quickly to your users without making them wait for Blazor WASM to finish loading.
TRAKAXPC SERVICE COMPONENT MISSING HOW TO
In part 1 we saw how to set up your Blazor WASM site for prerendering.
