當我嘗試在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 ---
...
對的,這是可能的。但是你必須使用由blazor框架注入的HttpClient,如下所述:
https://learn-blazor.com/architecture/rest-api/
(感謝弗洛雷斯的鏈接!)
HttpClient在Autorest使用的Microsoft.Rest.ServiceClient中標記為受保護。因此,要從blazor注入HttpClient,您可以創建自動生成的客戶端類的新部分並添加SetHttpClient
方法:
autorest生成的類:
public partial class YourApi : ServiceClient<YourApi>, IYourApi
{
...
}
你的新部分:
public partial class YourApi
{
public void SetHttpClient(HttpClient client) {
this.HttpClient = client;
}
}
好又簡單!