How to enable gzip compression in Simple OData Client?

I am trying to execute a query that retrieves information from CRM Dynamics using a Simple OData Client library like this (C #):

var settings = new ODataClientSettings(resource + "/api/data/v8.0/"); settings.BeforeRequest = (request) => { request.Headers.Clear(); request.Headers.Add("Authorization", accesstoken.AccessTokenType + " " + accesstoken.AccessToken); }; settings.PayloadFormat = ODataPayloadFormat.Json; var client = new ODataClient(settings); var annotations = new ODataFeedAnnotations(); var transactions = await client.For("mss_transaccions").FindEntriesAsync(annotations); while (annotations.NextPageLink != null) { transactions = transactions.Union(await client.For("mss_transaccions").FindEntriesAsync(annotations.NextPageLink, annotations)); } 

While this works, it is very slow, because my query in the mss_transaccions table has 7200 objects. I look at the output in Fiddler, and I see that it is trying to download about 20 MB of information.

I tried to run the same request in Google Chrome, and I realized that by default the received response is compressed in gzip format, starting from 20 MB to several 500 KB. Therefore, I conclude that Simple OData Client does not do any compression, and why it slows down so much.

In addition, a request from OData Simple Client requests metadata information that adds another 4 MB, while Chrome or a simple HttpClient request should not make this call.

Is there anything I can do to improve this and enable compression?

Thanks.

+5
source share
3 answers

I was able to finally enable compression and speed up the overall process. All discussion can be found here: https://github.com/object/Simple.OData.Client/issues/238

To accomplish this simply and quickly, you just need to modify the message handler in the ODataSettings instance with the following code snippet:

 settings.OnApplyClientHandler = handler => { handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; }; 

Now httpRequests are sent as gzip, deflated and decompressed with the correct answer.

+1
source

In your BeforeRequest action BeforeRequest add the Accept-Encoding header as follows:

 settings.BeforeRequest = (request) => { // ... other headers as above request.Headers.Add("Accept-Encoding", "gzip"); }; 
+2
source

If you check which headers are sent by Chrome and try to replicate them in C #?

Also, if I need to access CRM from C #, I would use Microsoft.Xrm.Sdk, not OData. You have many types of proxies and requests that will allow you to write code much cleaner. OData has other limitations that QueryExpressions / CRM LINQ / FetchXml do not have.

OData will make more sense for JS code (i.e. queries from a CRM form).

+1
source

Source: https://habr.com/ru/post/1247869/


All Articles