我可以帮您翻译成中文:我能在Blazor中使用Autorest客户端吗?

4
我在尝试在WebAssembly上运行我的Blazor应用程序时,从mono平台收到了System.PlatformNotSupportedException。我使用Autorest自动生成了一个Web API客户端。一切都编译得很好,但当我在浏览器中加载代码时,我会在浏览器控制台中收到下面的错误信息。
使用VS2017的预览版本。
module.printErr @ MonoPlatform.ts:192
WASM: [System.PlatformNotSupportedException] Operation is not supported on this platform.
WASM:   at System.Net.WebProxy.CreateDefaultProxy () <0x204ed08 + 0x00004> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Configuration.DefaultProxySectionInternal.GetSystemWebProxy () <0x204ebc0 + 0x00000> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Configuration.DefaultProxySectionInternal.GetDefaultProxy_UsingOldMonoCode () <0x204ea80 + 0x00000> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Configuration.DefaultProxySectionInternal.GetSection () <0x204e8c8 + 0x00022> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.WebRequest.get_InternalDefaultWebProxy () <0x204e610 + 0x0002c> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.HttpWebRequest..ctor (System.Uri uri) <0x2043eb0 + 0x000d2> in <1c80af700ca2462a80a92d89ad803d6a>:0 
WASM:   at System.Net.Http.HttpClientHandler.CreateWebRequest (System.Net.Http.HttpRequestMessage request) <0x20434d0 + 0x00016> in <3a9393eaef104ec489024eb855a8f163>:0 
WASM:   at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () <0x203ea60 + 0x00076> in <3a9393eaef104ec489024eb855a8f163>:0 
WASM: --- End of stack trace from previous location where exception was thrown ---
...
3个回答

4
是的,这是可能的。但是你需要使用由Blazor框架注入的HttpClient,如此处所述:https://learn-blazor.com/architecture/rest-api/(感谢Flores提供的链接!) Microsoft.Rest.ServiceClient中标记为受保护的HttpClient被Autorest使用。因此,要从blazor注入HttpClient,您可以创建一个自动生成的客户端类的新部分,并添加SetHttpClient方法:生成的类:
public partial class YourApi : ServiceClient<YourApi>, IYourApi
{
    ...
}

您的新部分如下:
public partial class YourApi
{
    public void SetHttpClient(HttpClient client) {
        this.HttpClient = client;
    }
}

简单易懂,非常好!


我相信这可能是我在这里提出的问题的同样问题 https://stackoverflow.com/questions/68225808/error-using-presigned-url-for-uploading-data-from-blazor-wasm-app-to-aws-s3-buck 但我不理解您提供的解决方案。您能帮助回答我的问题吗? - Pablo

0

Blazor/WebAssembly应用程序在浏览器沙盒中运行,因此受限于浏览器网络的可能性。这意味着所有网络流量都需要通过浏览器网络请求系统处理。

看起来它尝试使用代理..但不支持。


那么你认为这是一个安全问题吗?听起来更像是平台的限制。堆栈跟踪中的这一部分有点有趣:GetDefaultProxy_UsingOldMonoCode - Nikolaj
这是WebAssembly的限制。在我看来,这是完全可以理解的。如果WebAssembly能够超越JS,那将带来重大的潜在安全漏洞。 - Flores
此外,查看此处如何在Blazor中进行网络请求:https://learn-blazor.com/architecture/rest-api/ - Flores

-1

现在使用 Refit 要容易得多。

const string baseUri = "http://localhost:55088/";
services.AddRefitClient<IApiService>()
   .ConfigureHttpClient(c => c.BaseAddress = new Uri(baseUri))
   .ConfigurePrimaryHttpMessageHandler(() => new BrowserMessageHandler());

当然,您仍然需要自己进行序列化/反序列化,但我使用这些扩展程序,以便在必要时也可以访问HttpResponseMessage。

    public static async Task<T> As<T>(this Task<HttpResponseMessage> task)
    {
        var response = await task.ConfigureAwait(false);
        return await response.As<T>();
    }

    public static async Task<T> As<T>(this HttpResponseMessage message)
    {
        var json = await message.Content.ReadAsStringAsync();
        return Json.Deserialize<T>(json);
    }

我希望这能让某个人的生活比我在这之前更容易!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接