问题
I am getting a System.PlatformNotSupportedException
from the mono platform when trying to run my blazor app on webassembly.
I autogenerated a web api client using Autorest. Everything is compiling fine but when I load the code in the browser I get the error below in the browser console.
Using a preview build of 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 ---
...
回答1:
Yes it is possible. But you have to use the HttpClient that is injected by the blazor framework as described here:
https://learn-blazor.com/architecture/rest-api/
(thanks for the link Flores!)
The HttpClient is marked as protected in the Microsoft.Rest.ServiceClient that is used by Autorest. So to inject the HttpClient from blazor, you can create a new partial of the autogenerated client class and add a SetHttpClient
method:
The class generated by autorest:
public partial class YourApi : ServiceClient<YourApi>, IYourApi
{
...
}
Your new partial:
public partial class YourApi
{
public void SetHttpClient(HttpClient client) {
this.HttpClient = client;
}
}
Nice and simple!
回答2:
Blazor/webassembly apps run in the browser sandbox and therefore are limited to the browsers network possibilities. This means that all network traffic needs to go through the browser network request system.
It looks like it tries to use a proxy.. which is not supported.
回答3:
It's much easier now, using Refit.
const string baseUri = "http://localhost:55088/";
services.AddRefitClient<IApiService>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri(baseUri))
.ConfigurePrimaryHttpMessageHandler(() => new BrowserMessageHandler());
Of course, you still have to do your own serialization/deserialization but I use these extensions so that I can also access the HttpResponseMessage when necessary.
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);
}
I hope this makes somebody's life a little easier than mine was before I got all this working!
来源:https://stackoverflow.com/questions/49949539/can-i-use-an-autorest-client-in-blazor