refit icon indicating copy to clipboard operation
refit copied to clipboard

Refit + Fusillade fails with System.ObjectDisposedException: Cannot access a disposed object

Open vasiliy-s opened this issue 8 years ago • 3 comments

I have a xamarin forms project. I hoped to use both Refit and Fusillade for it. When i use either of them everything works fine but when i start to use them together then requests fails.

Fusillade 1.0.0 Xamarin.Forms 3.0.0.296286 Refit 4.3.0

**Single ** request works fine. Fails only when Fusillade deduplicates queries.

The code is simple. I got an empty ContentPage with button and label. The button click handler contains code:

var api = RestService.For<IScheduleApi>(
	new HttpClient( NetCache.UserInitiated )
	{
		BaseAddress = new Uri( Config.ApiUrl )
	} );
			
var result = await api.GetText();
this.TextLabel.Text = result;

IScheduleApi is the following:

[Headers("Accept: application/json")]
    public interface IScheduleApi
	{
		[Get("/schedule")]
		Task<string> GetText();
	}

The web service just sleeps and returns text:

Thread.Sleep(4000);
return "Text";

When i click on button and wait then i get the response and everything is fine. When i click next time and again wait - everything works. But if i do more that one click then i get the exeption. Though i am not sure which one of the libraries causes it

04-03 19:18:50.144 I/MonoDroid(10742): UNHANDLED EXCEPTION: 04-03 19:18:50.174 I/MonoDroid(10742): System.ObjectDisposedException: Cannot access a disposed object. 04-03 19:18:50.174 I/MonoDroid(10742): Object name: 'System.Net.Http.StreamContent'. 04-03 19:18:50.174 I/MonoDroid(10742): at System.Net.Http.HttpContent+d__18.MoveNext () [0x00027] in :0 04-03 19:18:50.174 I/MonoDroid(10742): --- End of stack trace from previous location where exception was thrown --- 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <fcbf47a04b2e4d90beafbae627e1fca4>:0 04-03 19:18:50.174 I/MonoDroid(10742): at Refit.RequestBuilderImplementation+<>c__DisplayClass11_01+<b__0>d[T].MoveNext () [0x003d2] in :0 04-03 19:18:50.174 I/MonoDroid(10742): --- End of stack trace from previous location where exception was thrown --- 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at TempStaff.SchedulePage+d__3.MoveNext () [0x00051] in D:\Code\Avilum\tempstaff\TempStaff\TempStaff\SchedulePage.xaml.cs:69 04-03 19:18:50.174 I/MonoDroid(10742): --- End of stack trace from previous location where exception was thrown --- 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in :0 04-03 19:18:50.174 I/MonoDroid(10742): at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] in <551e90b840814b76a3d15b7bbaa8a77c>:0 04-03 19:18:50.174 I/MonoDroid(10742): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <551e90b840814b76a3d15b7bbaa8a77c>:0 04-03 19:18:50.174 I/MonoDroid(10742): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <551e90b840814b76a3d15b7bbaa8a77c>:0 04-03 19:18:50.174 I/MonoDroid(10742): at (wrapper dynamic-method) System.Object.aa6e990f-8152-4823-ae62-44e7a273b201(intptr,intptr)

vasiliy-s avatar Apr 03 '18 19:04 vasiliy-s

So accidentally found out that the issue is not in refit. Sorry:)

vasiliy-s avatar Apr 03 '18 20:04 vasiliy-s

@vasiliy-s could what caused the issue for completion sake?

ahmedalejo avatar Apr 04 '18 22:04 ahmedalejo

Well... To bo honest i have no idea what causes the issue. I just managed to get the same error with Fusillade only. Though i know how to make it work without Refit and so i have to go with Fusillade and not Refit because i can write API myself relatively easily but not prioritizing and deduplication :(

So this works fine:

var client = new HttpClient( NetCache.Background )
{
	BaseAddress = new Uri( Config.ApiUrl )
};

var response = await client.GetAsync( "schedule" ).ConfigureAwait(false);
var jsonString = await response.Content.ReadAsStringAsync().ConfigureAwait( false );
return JsonConvert.DeserializeObject<ApiResult<IList<ScheduleEntry>>>(jsonString);

But if i wrap it in using:

using(var client = new HttpClient( NetCache.Background )){..}

then all requests except the first one fails

vasiliy-s avatar Apr 05 '18 08:04 vasiliy-s