diff --git a/AutoRenderModeServices/AutoRenderModeServices.Client/Components/WebScrape.razor b/AutoRenderModeServices/AutoRenderModeServices.Client/Components/WebScrape.razor index c4756f4..74a5e7f 100644 --- a/AutoRenderModeServices/AutoRenderModeServices.Client/Components/WebScrape.razor +++ b/AutoRenderModeServices/AutoRenderModeServices.Client/Components/WebScrape.razor @@ -1,5 +1,6 @@ -@inject HttpClient Http -@attribute [StreamRendering(true)] +@using AutoRenderModeServices.Client.Features.WebScraper +@inject IWebScraperService _webScraper + @if (Text is null) { @@ -18,7 +19,6 @@ else protected override async Task OnInitializedAsync() { - var response = await Http.GetFromJsonAsync("webscraper/bing"); - Text = response; + Text = await _webScraper.Get(); } } diff --git a/AutoRenderModeServices/AutoRenderModeServices.Client/Features/WebScraper/IWebScraperService.cs b/AutoRenderModeServices/AutoRenderModeServices.Client/Features/WebScraper/IWebScraperService.cs new file mode 100644 index 0000000..f427291 --- /dev/null +++ b/AutoRenderModeServices/AutoRenderModeServices.Client/Features/WebScraper/IWebScraperService.cs @@ -0,0 +1,7 @@ +namespace AutoRenderModeServices.Client.Features.WebScraper +{ + public interface IWebScraperService + { + Task Get(); + } +} \ No newline at end of file diff --git a/AutoRenderModeServices/AutoRenderModeServices.Client/Features/WebScraper/WebScraperServiceWasm.cs b/AutoRenderModeServices/AutoRenderModeServices.Client/Features/WebScraper/WebScraperServiceWasm.cs new file mode 100644 index 0000000..f163f92 --- /dev/null +++ b/AutoRenderModeServices/AutoRenderModeServices.Client/Features/WebScraper/WebScraperServiceWasm.cs @@ -0,0 +1,11 @@ +using System.Net.Http.Json; + +namespace AutoRenderModeServices.Client.Features.WebScraper; + +public class WebScraperServiceWasm(HttpClient _httpClient) : IWebScraperService +{ + public async Task Get() + { + return await _httpClient.GetFromJsonAsync("webscraper/bing") ?? "empty reply"; + } +} diff --git a/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/Home.razor b/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/Home.razor index 1a11923..14295d1 100644 --- a/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/Home.razor +++ b/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/Home.razor @@ -11,4 +11,4 @@ WebScrape component is not working here, but it works in in the Web Scraper page The reason is that the HttpClient needed to call back to the localhost has not been initialized with the BaseAddress yet. Comment out the WebScrape component below and navigate to the Web Scraper page to see it working. -@* *@ + diff --git a/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/ScrapeWebSite.razor b/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/ScrapeWebSite.razor index 99ad228..541d716 100644 --- a/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/ScrapeWebSite.razor +++ b/AutoRenderModeServices/AutoRenderModeServices.Client/Pages/ScrapeWebSite.razor @@ -1,9 +1,12 @@ @page "/scrape" @using AutoRenderModeServices.Client.Components + Scrape Web Site

ScrapeWebSite

+@($"Browser:{OperatingSystem.IsBrowser()}") + @code { diff --git a/AutoRenderModeServices/AutoRenderModeServices.Client/Program.cs b/AutoRenderModeServices/AutoRenderModeServices.Client/Program.cs index b72adaa..2c0fbe4 100644 --- a/AutoRenderModeServices/AutoRenderModeServices.Client/Program.cs +++ b/AutoRenderModeServices/AutoRenderModeServices.Client/Program.cs @@ -1,7 +1,9 @@ +using AutoRenderModeServices.Client.Features.WebScraper; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); +builder.Services.AddScoped(); await builder.Build().RunAsync(); diff --git a/AutoRenderModeServices/AutoRenderModeServices/Features/WebScraper/WebScraperServiceServer.cs b/AutoRenderModeServices/AutoRenderModeServices/Features/WebScraper/WebScraperServiceServer.cs new file mode 100644 index 0000000..81094bd --- /dev/null +++ b/AutoRenderModeServices/AutoRenderModeServices/Features/WebScraper/WebScraperServiceServer.cs @@ -0,0 +1,22 @@ +using AutoRenderModeServices.Client.Features.WebScraper; + +namespace AutoRenderModeServices.Client.Features; + +public class WebScraperServiceServer(IHttpClientFactory _httpClientFactory) : IWebScraperService +{ + public async Task Get() + { + var client = _httpClientFactory.CreateClient(); + + var responseMessage = await client.GetAsync("https://www.bing.com"); + + if (responseMessage.IsSuccessStatusCode) + { + var text = await responseMessage.Content.ReadAsStringAsync(); + return text; + } + + return ""; + } + +} diff --git a/AutoRenderModeServices/AutoRenderModeServices/Program.cs b/AutoRenderModeServices/AutoRenderModeServices/Program.cs index cc12d91..f70d65b 100644 --- a/AutoRenderModeServices/AutoRenderModeServices/Program.cs +++ b/AutoRenderModeServices/AutoRenderModeServices/Program.cs @@ -1,3 +1,5 @@ +using AutoRenderModeServices.Client.Features; +using AutoRenderModeServices.Client.Features.WebScraper; using AutoRenderModeServices.Client.Pages; using AutoRenderModeServices.Components; @@ -10,6 +12,8 @@ builder.Services.AddHttpClient(); +builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -34,19 +38,16 @@ .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(typeof(Counter).Assembly); -app.MapGet("/webscraper/bing", async (IHttpClientFactory httpClientFactory) => +app.MapGet("/webscraper/bing", async (IWebScraperService _webScraper) => { - var client = httpClientFactory.CreateClient(); - - var responseMessage = await client.GetAsync("https://www.bing.com"); - - if (responseMessage.IsSuccessStatusCode) + var responseText = await _webScraper.Get(); + + if (string.IsNullOrWhiteSpace(responseText)) { - var text = await responseMessage.Content.ReadAsStringAsync(); - return Results.Ok(text); + return Results.Problem(detail: "Didn't work"); } - return Results.Problem(detail: "Didn't work"); + return Results.Ok(responseText); }); app.Run();