nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
764 stars 313 forks source link

GraphService.SendAsync resulting in Cannot access a disposed object. #1080

Open Diceble opened 4 months ago

Diceble commented 4 months ago

Hi,

With my project I want to use the GraphAPI to fetch products, orders, etc. ShopifySharp has a GraphService which would be able to fullfill this purpose (as far as my understanding goes). I'm using ShopifySharp 6.17 and .net framework 4.8.

I have a test method to try out fetching products. Yet when the graphservice does it's SendAsync(request), I get an exception which states the following; Cannot access a disposed object. Object name: 'System.Net.Http.StringContent'.

Am I doing something wrong here? Is this a bug within the framework? I am a bit lost. I appreciate the help.

this is the code example that I use. (mind you my graphql query could be garbage, since I am trying to understand how it works);

        public async Task FetchAllProductsAfterDateTests()
        {
            GraphRequest request = new GraphRequest
            {
                query = @"
                query {
                    products(first: 10) {
                        edges {
                            node {
                                id
                                title
                                description
                                createdAt
                                updatedAt
                            }
                        }
                    }
                }"
            };
            try
            {
                var graphService = new GraphService(_shopifyDomain, _accessToken);
                var result = await graphService.SendAsync(request);
            }
            catch(Exception ex)
            {
            }
        }
Diceble commented 4 months ago

@nozzlegear Do you see yourself having some time in the near future to share your thoughts on what a potential cause could be?

nozzlegear commented 4 months ago

Hey, sorry @Diceble! I meant to reply yesterday. I don't see anything sticking out that looks like it could be the direct cause here. Have you set any global ShopifySharp settings, e.g. ShopifySharp.SetGlobalExecutionPolicy or ShopifySharp.SetGlobalHttpClientFactory? Let me know which ones if you have, but if not let's try the following:

  1. Update to the latest version of ShopifySharp, which should be 6.18.0 at the moment.
  2. If that doesn't fix the problem, create a new .NET 6, 7 or 8 project (whichever one you can), install ShopifySharp 6.18.0, and run exactly this code to see if it throws the same exception. If it works, we'll know there might be an issue with the .NET Framework version of ShopifySharp.

If you get any exceptions, try to copy and paste the stack traces here! They'll be helpful for debugging this.

Diceble commented 4 months ago

Hi @nozzlegear,

Thanks for the reply! I have set no global settings like you described, neither have I set any other global settings regarding to shopifysharp

After an update to 6.18 of shopifysharp within my net 4.8 framework the exception stays the same. This is the stacktrace;

   at System.Net.Http.HttpContent.CheckDisposed()
   at System.Net.Http.HttpContent.ReadAsStringAsync()
   at ShopifySharp.Infrastructure.CloneableRequestMessage.<GetRequestInfo>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ShopifySharp.ShopifyService.<>c__DisplayClass29_0`1.<<ExecuteRequestCoreAsync>b__0>d.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ShopifySharp.DefaultRequestExecutionPolicy.<Run>d__0`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ShopifySharp.ShopifyService.<ExecuteRequestCoreAsync>d__29`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ShopifySharp.GraphService.<SendAsync>d__10`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ShopifySharp.GraphService.<PostAsync>d__9`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ShopifySharp.GraphService.<SendAsync>d__8.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Shopify.Test.ShopifyGraphQlServiceTests.<FetchAllProductsAfterDateTests>d__4.MoveNext() in C:\Repositories\sherpa\Tests\Shopify.Test\ShopifyGraphQlServiceTests.cs:line 52

As you requested I created a .net 8 project with shopifysharp 6.18, I ran the exact same unit test. This time it did work. So it seems the .NET framework is the root cause of the issue here. Unfortunately, I am not in the position to upgrade my project to any of the newer .NET version since the project is too large and depends on too much of non .net 8 compatible packages that this takes quite some time to refactor, time we unfortunately don't have to spend :'( .

Is this a bug that is in the scope of the project to get solved or will this not get resolved? Also if it is the case it will get resolved, is it possible to give a time window on resolving this bug (like a week, month, multiple months). Asking this so I know what I can expect and what my options since this is part of a bigger project within our organization. So it's not to put pressure on you resolving the issue, but rather to see if our planning needs to be adjusted

Many thanks!

nozzlegear commented 4 months ago

Perfect, thanks for the stack trace and thanks for trying the .net 8 app! I'm on a Mac so it's a little difficult for me to test dotnet framework out.

No worries about not being able to upgrade right now, we support dotnet framework as one of the official targets for ShopifySharp, so I'm going to fix this bug. I'm pretty sure I know what the issue is thanks to your stack trace. I can't promise an exact timeline for when it'll be fixed, but I'm getting started right now. I don't see it taking longer than a day or two.

Diceble commented 4 months ago

Thank you for the information about your estimate. It's enough for me to make my decision on how to continue our project :) GL fixing the bug if any help/further details is needed let me know.

Volampus commented 3 months ago

Hi @nozzlegear!

I'm using ShopifySharp 6.18 and .net standard 2.0 and facing the same issue. I've done some debugging, hopefully this is helpful:

In ShopifyService.cs, the baseRequestMessage.GetRequestInfo() call in this line is throwing the Exception CheckResponseExceptions(await baseRequestMessage.GetRequestInfo(), response, rawResult); It looks like using var response = await _Client.SendAsync(requestMessage, cancellationToken); causes the request HttpContent to be disposed: image

I think the reason why it gets disposed is here: https://stackoverflow.com/questions/25495394/why-do-httpclient-postasync-and-putasync-dispose-the-content.

Let me know if there is any more detail you'd like me to share 😄

judoscott commented 3 months ago

I went from 4.6 to 4.8 in a project and update the nuget package and started seeing this issue as well.

nozzlegear commented 3 months ago

Thanks for the report @judoscott. Just want to confirm quick, you upgraded your project to .NET 4.8, and you updated ShopifySharp at the same time?

judoscott commented 3 months ago

Thanks for the report @judoscott. Just want to confirm quick, you upgraded your project to .NET 4.8, and you updated ShopifySharp at the same time?

Yes thats correct. I am going to have a go at upgrading to .NET 8 and will report back the results. Its a rather large project though so might take a bit

judoscott commented 3 months ago

.Net 8 proved to much for this on short notice. Reverted to .net 4.6.1 and Shopifysharp 6.17

nozzlegear commented 1 month ago

I'm fixing this right now, will have a new build within the next couple of days!