gpt4 book ai didi

flurl - Blazor 0.6.0 "wipes"Flurl 兼容性?

转载 作者:行者123 更新时间:2023-12-04 10:33:54 26 4
gpt4 key购买 nike

将 Blazor 从 0.5.1(使用工作 Flurl)更新到 0.6.0 后,通过 flurl 调用会引发异常:

WASM: [Flurl.Http.FlurlHttpException] Call failed. Cannot invoke method 
because it was wiped. See stack trace for details.

该项目创建了一个 HttpClientFactory,它获取 Blazor 的 HttpClient 以供 Flurl 使用:

使用 HttpClientFactoryForBlazor 使用 Blazor 的 HttpClient (http) 创建 FlurlClient:
IFlurlClient c = new FlurlClient() { Settings = new Flurl.Http.Configuration.ClientFlurlHttpSettings { HttpClientFactory = new HttpClientFactoryForBlazor(http) }};

使用 FlurlClient (c) 例如通过 Flurl 的扩展方法“IFlurlRequest.WithClient(c);”
private class HttpClientFactoryForBlazor : Flurl.Http.Configuration.IHttpClientFactory
{
private readonly HttpClient httpClient;

public HttpClientFactoryForBlazor(HttpClient httpClient)
{
this.httpClient = httpClient;
}

public virtual HttpClient CreateHttpClient(HttpMessageHandler handler)
{
return this.httpClient;
}
}

因此,这种方法似乎不再有效。

有人知道如何使用 Blazor 0.6.0 使 Flurl 工作吗?

调用堆栈是:
WASM: [Flurl.Http.FlurlHttpException] Call failed. Cannot invoke method because it was wiped. See stack trace for details. GET http://srv01.servicegrid.eu:4455/API/Status?forceLoadDbs=False blazor.webassembly.js:1:32098
WASM: at Flurl.Http.FlurlRequest.HandleExceptionAsync (Flurl.Http.HttpCall call, System.Exception ex, System.Threading.CancellationToken token) <0x26945b8 + 0x001c2> in <c38761af4558433f81b1691eb86a1548>:0 blazor.webassembly.js:1:32098
WASM: at Flurl.Http.FlurlRequest.SendAsync (System.Net.Http.HttpMethod verb, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken, System.Net.Http.HttpCompletionOption completionOption) <0x2665d30 + 0x005e6> in <c38761af4558433f81b1691eb86a1548>:0 blazor.webassembly.js:1:32098
WASM: at Flurl.Http.FlurlRequest.SendAsync (System.Net.Http.HttpMethod verb, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken, System.Net.Http.HttpCompletionOption completionOption) <0x2665d30 + 0x0079a> in <c38761af4558433f81b1691eb86a1548>:0 blazor.webassembly.js:1:32098
WASM: at Flurl.Http.HttpResponseMessageExtensions.ReceiveJson[T] (System.Threading.Tasks.Task`1[TResult] response) <0x26a2180 + 0x000d6> in <c38761af4558433f81b1691eb86a1548>:0 blazor.webassembly.js:1:32098
WASM: at DotNetFabrik.FlurlExtensions.FlurlRequestExtensions.HandleWebApiExceptions[T] (System.Threading.Tasks.Task`1[TResult] task) <0x26a43f8 + 0x000e2> in <8c1e6df9d3f545cd831ff49915df2d85>:0 blazor.webassembly.js:1:32098
WASM: at DotNetFabrik.FlurlExtensions.FlurlRequestExtensions.HandleWebApiExceptions[T] (System.Threading.Tasks.Task`1[TResult] task) <0x26a43f8 + 0x00264> in <8c1e6df9d3f545cd831ff49915df2d85>:0 blazor.webassembly.js:1:32098
WASM: at BlazorCoreDMSTools.CommunicationService.CommunicationService.SetTokenAsync (System.String token, System.String database, System.String serverUri) <0x260dc60 + 0x00d9e> in <cb925648b50340888772566fbaeac465>:0

最佳答案

只是为了一些背景,Blazor 团队正在显着减少应用程序的占用空间,并求助于一些 unusual measures这样做。简而言之,他们通过“删除”将其减少了约 20% HttpClientHandler .

wipe means "replace specified method bodies with a single throw instruction". Doing this (instead of actually removing the method entirely) means that the assembly retains a completely standard API surface, and if you try to use one of the wiped methods, you get an easy-to-understand exception stack trace that tells you which wiped method you're trying to call.



这就是您遇到的问题:Blazor 仍然知道 HttpClientHandler用于编译目的,但如果您(或在本例中为兼容库)尝试使用它,则会抛出运行时异常。

但是 HttpClient必须包装 HttpMessageHandler 的一些实现Blazor 有自己的: BrowserHttpMessageHandler . Flurl 通过它的 HttpClientFactory 提供了一种简单的方法来交换它。 .但是你不需要传入 HttpClient实例或实现 CreateHttpClient .相反,继承自 DefaultHttpClientFactory并覆盖 CreateMessageHandler :
private class HttpClientFactoryForBlazor : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new BrowserHttpMessageHandler();
}
}

我还建议在应用程序启动时全局注册一次,而不是每次创建 FlurlClient :
FlurlHttp.Configure(settings =>
{
settings.HttpClientFactory = new HttpClientFactoryForBlazor();
});

还应该指出的是,Blazor 仍处于试验阶段, BrowserHttpMessageHandler may be deprecated in a future release ,所以预计这可能只是一个临时的解决方法。

关于flurl - Blazor 0.6.0 "wipes"Flurl 兼容性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52522004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com