OData / odata.net

ODataLib: Open Data Protocol - .NET Libraries and Frameworks
https://docs.microsoft.com/odata
Other
687 stars 349 forks source link

`DataServiceQuery.ExecuteAsync()` triggers exception 'System.Net.Requests is not supported on this platform' #2678

Open kgorking opened 1 year ago

kgorking commented 1 year ago

I'm writing a Blazor webapp using SAP B1 through OData, and using serviceLayer.HttpRequestTransportMode = HttpRequestTransportMode.HttpClient so it can work in a wasm environment. It seems something unsupported slipped through, however.

System.Net.Requests is not supported on this platform is triggered on calls to DataServiceQuery.ExecuteAsync().

If I use the code from Use HttpClient in OData Client, the call works fine and the data returned correctly.

Assemblies affected

OData client 7.0.5

Reproduce steps

The exception is triggered by a cors failure, due to the insertion of 'odata-(max)version)' headers which I didn't have allowed. Doing the exact same request manually using a HttpClient.SendAsync() works fine and returns the requested data.

Expected result

Requested data is returned.

Actual result

An exception is raised due to use of unsupported code.

Additional detail

I am hitting the following exception

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: An error occurred while processing this request.
System.InvalidOperationException: An error occurred while processing this request.
 ---> System.PlatformNotSupportedException: System.Net.Requests is not supported on this platform.
   at System.Net.WebException..ctor(String message)
   at Microsoft.OData.Client.HttpClientRequestMessage.UnwrapAggregateException[HttpWebResponseMessage](Func`1 action)
   at Microsoft.OData.Client.HttpClientRequestMessage.EndGetResponse(IAsyncResult asyncResult)
   at Microsoft.OData.Client.ODataRequestMessageWrapper.EndGetResponse(IAsyncResult asyncResult)
   at Microsoft.OData.Client.DataServiceContext.GetResponseHelper(ODataRequestMessageWrapper request, IAsyncResult asyncResult, Boolean handleWebException)
   at Microsoft.OData.Client.DataServiceContext.EndGetResponse(ODataRequestMessageWrapper request, IAsyncResult asyncResult)
   at Microsoft.OData.Client.RequestInfo.EndGetResponse(ODataRequestMessageWrapper request, IAsyncResult asyncResult)
   at Microsoft.OData.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at Microsoft.OData.Client.BaseAsyncResult.EndExecute[QueryResult](Object source, String method, IAsyncResult asyncResult)
   at Microsoft.OData.Client.QueryResult.EndExecuteQuery[SQLQuery](Object source, String method, IAsyncResult asyncResult)
   at Microsoft.OData.Client.DataServiceRequest.EndExecute[SQLQuery](Object source, DataServiceContext context, String method, IAsyncResult asyncResult)
   at Microsoft.OData.Client.DataServiceQuery`1[[SAPB1.SQLQuery, WebclientExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].EndExecute(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1[[System.Collections.Generic.IEnumerable`1[[SAPB1.SQLQuery, WebclientExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
(my code)

which seems to be triggered on the following line of code https://github.com/OData/odata.net/blob/30f4e3aaecafa8554f20a62a755e7875aab7c88c/src/Microsoft.OData.Client/Serialization/HttpClientRequestMessage.cs#L473

vifani commented 1 year ago

I have the same issue. For exceptions occurring during a request executed via DataServiceQuery class, I have the same error. I think the WebException class should not be used inside the HttpClientRequestMessage class

bytefish commented 1 year ago

I get the same exception when using Blazor Wasm. I narrowed it down to the same line as identified by @kgorking. If you accept PRs, what would be an appropriate Exception here?

vifani commented 1 year ago

I get the same exception when using Blazor Wasm. I narrowed it down to the same line as identified by @kgorking. If you accept PRs, what would be an appropriate Exception here?

Exception raised by HttpClient are HttpRequestException (https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestexception?view=net-7.0). So, I think this should be more appropriate class to use

bytefish commented 12 months ago

It’s not relevant to the issue, but I think it’s quite an undertaking to make this client compatible to Blazor WASM and not introduce breaking changes. I have used Kiota instead, which generates a nice client, that also works with Blazor WASM: https://www.bytefish.de/blog/odata_openapi_kiota_client_generation.html