jeudi 3 septembre 2020

HttpClient SendAsync Post in a .NET Standard DLL fails for Wasm apps (Blazor) TypeError: Failed to fetch

We have built a .netstandard 2.0 DLL which does all of our data access to a 3rd party rest api. We use this DLL for our WPF and Xamarin apps. Recently I have tried to create an Uno Platform app and Blazor app to make sure the api works and that the service has CORS enabled for our app.

We have wrapped the calls to make it easy for client apps. There are 4 calls that I make to the DLL. All calls have an HttpClient making the calls.

  1. GetConfigurations - HttpClient.GetAsync
  2. OpenSession - HttpClient.PostAsync
  3. Request - HttpClient.SendAsync setting Method = HttpMethod.Post and passing Authorization Header with token
  4. CloseSession - HttpClient.PostAsync

If I run this for our other apps I have a sample call that works for all 4 calls. When I turn on Fiddler and run this from non web clients I see:

  • GetConfigurations - GET
  • OpenSession - POST
  • Request - POST
  • CloseSession - POST

When I run from a web client I see:

  • GetConfigurations - GET
  • OpenSession - POST
  • Request - OPTIONS **
  • CloseSession - POST

Image of Fiddler on Request that fails

Error and Stack trace:

TypeError: Failed to fetch

at System.Net.Http.WebAssemblyHttpHandler.doFetch(System.Threading.Tasks.taskCompletionSource1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x30c8628 + 0x00a30> in <filename unknown>:0 at System.Net.Http.WebAssemblyHttpHandler.SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x309c6f8 + 0x00174> in <filename unkonwn>:0 at System.Net.Http.WebAssemblyHttpClient.FinishSendAsyncBuffered(System.Threading.Tasks.Task1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x32c4fd0 + 0x00278> in :0 at MyDll.Request(System.String query) [0x0011c] in c:... at BlazorApp1.Pages.FetchData.OnInitializedAsync() [0x0031a] in c:...

Why does the Request call fail? There is the method from Request vs OpenSession since OpenSession works and Request fails.

    public async Task<string> OpenSession (string config, string userid, string password)
    {
        var client = new HttpClient();
        JObject jParms = new JObject();
        jParms.Add("Configuration", config);
        jParms.Add("UserName", userid);
        jParms.Add("Password", password ?? "");
        Uri uri = new Uri(Session.URL + "/OpenSession");
        var response = await client.PostAsync(uri, new StringContent(jParms.ToString())).ConfigureAwait(false);
        if (response.IsSuccessStatusCode)
        {
            string data = await response.Content.ReadAsStringAsync();
            //Success Code
        } else
        {
            //Error Code
        }
        return "";
    }





    public async Task<string> Request(string query)
    {
        var client = new HttpClient();
        Uri uri = new Uri(Session.URL + "/Request/" + query);
        var request = new HttpRequestMessage()
        {
            RequestUri = uri
            ,Method = HttpMethod.Post
        };
        request.Headers.Add("Authorization", Session.SessionID);
        var response = await client.SendAsync(request).ConfigureAwait(false);
        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadAsStringAsync();
        }
        else
        {
            //Error Code
        }
        return "";                 
    }



Aucun commentaire:

Enregistrer un commentaire