我正在针对 Blazor 3.0.0-preview4-19216-03 的客户端 Blazor 应用程序进行测试
剃须刀页面:
@page "/counter"
@using BlazorServiceTest
@inject IWebCrawlServiceAsync WebCrawler
<h1>Counter</h1>
<p>Current count: @debug</p>
<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>
@functions {
string debug = "";
async void IncrementCount()
{
debug = await WebCrawler.GetWeb();
}
}
依赖注入:
using BlazorServiceTest;
using Microsoft.AspNetCore.Components.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace BlazorServicesTest
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IWebCrawlServiceAsync, WebCrawlServiceAsync>();
}
public void Configure(IComponentsApplicationBuilder app)
{
app.AddComponent<App>("app");
}
}
}
服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace BlazorServiceTest
{
public interface IWebCrawlServiceAsync
{
Task<string> GetWeb();
}
public class WebCrawlServiceAsync : IWebCrawlServiceAsync
{
private HttpClient _client;
public WebCrawlServiceAsync(HttpClient client)
{
_client = client;
}
public async Task<string> GetWeb()
{
var response = await _client.GetAsync("https://postman-echo.com/response-headers?foo1=bar1&foo2=bar2");
var result = await response.Content.ReadAsStringAsync();
return result;
}
}
}
每当我单击增量计数时,什么都没有发生,并且GetWeb
对它的服务调用卡在GetAsync
调用中。
更新
如果我在 Chrome 的 WASM 调试器中调试服务 WebCrawler,他的请求正在发出
但是响应部分是空的: