Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

InvalidOperationException: Cannot provide a value for property 'QuestionService' on type 'PWAQuestionWASM.Client.Pages.Questions'

$
0
0

I'm trying to make CRUD work on Blazor Webassembly, but absolutely nothing works.

InvalidOperationException: Cannot provide a value for property 'QuestionService' on type 'PWAQuestionWASM.Client.Pages.Questions'. There is no registered service of type 'PWAQuestionWASM.Client.Services.QuestionService'.

Even Github Copilot isn't a viable help, and this part just makes CRUD on Blazor WebAssembly too hard or nearly impossible to do. Here is my Program.cs:

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;using PWAQuestionWASM.Client.Services;using System.Net.Http;var builder = WebAssemblyHostBuilder.CreateDefault(args);builder.Services.AddAuthorizationCore();builder.Services.AddCascadingAuthenticationState();builder.Services.AddAuthenticationStateDeserialization();// Register QuestionServicebuilder.Services.AddScoped<QuestionService>();await builder.Build().RunAsync();

The actual QuestionService.cs:

using System.Net.Http.Json;using PWAQuestionWASM.Shared.Models;namespace PWAQuestionWASM.Client.Services{    public class QuestionService    {        private readonly HttpClient _http;        public QuestionService(HttpClient http)        {            _http = http;        }        public async Task<List<Question>> GetQuestionsAsync()            => await _http.GetFromJsonAsync<List<Question>>("api/questions") ?? new();        public async Task<Question?> GetQuestionAsync(int id)            => await _http.GetFromJsonAsync<Question>($"api/questions/{id}");        public async Task<Question?> CreateQuestionAsync(Question question)        {            var response = await _http.PostAsJsonAsync("api/questions", question);            return await response.Content.ReadFromJsonAsync<Question>();        }        public async Task<bool> UpdateQuestionAsync(Question question)        {            var response = await _http.PutAsJsonAsync($"api/questions/{question.Id}", question);            return response.IsSuccessStatusCode;        }        public async Task<bool> DeleteQuestionAsync(int id)        {            var response = await _http.DeleteAsync($"api/questions/{id}");            return response.IsSuccessStatusCode;        }    }}

I've checked everywhere to make sure everything works, because all this time I've been asking Github Copilot what is wrong, and it's giving me only basic misspells or code errors that doesn't even exist and is fixed. What can I try next?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>