ringcentral / RingCentral.Net

RingCentral SDK for .NET
MIT License
19 stars 26 forks source link

'Cannot access a disposed object' error #10

Closed anne-a closed 5 years ago

anne-a commented 5 years ago

For certain error cases, we're getting the error - 'Cannot access a disposed object' instead of the real error message. For example, when trying to authorize with the code below:

RestClient restClient = new RestClient(clientID, clientSecret, server);
TokenInfo tokenInfo = await restClient.Authorize(username, extension, password);

If there is a problem with the credentials, e.g. an incorrect password, the SDK throws the following exception:

Cannot access a disposed object.
Object name: 'System.Net.Http.FormUrlEncodedContent'.

Stack Trace: 
   at System.Net.Http.HttpContent.CheckDisposed()
   at System.Net.Http.HttpContent.ReadAsStringAsync()
   at RingCentral.Utils.FormatHttpMessage(HttpResponseMessage httpResponseMessage, HttpRequestMessage httpRequestMessage)
   at RingCentral.RestException..ctor(HttpResponseMessage httpResponseMessage, HttpRequestMessage httpRequestMessage)
   at RingCentral.RestClient.<Request>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RingCentral.RestClient.<Request>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RingCentral.RestClient.<Request>d__5`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RingCentral.RestClient.<Post>d__7`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RingCentral.Paths.Restapi.Oauth.Token.Index.<Post>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RingCentral.RestClient.<Authorize>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()`

Similarly, when trying to make a call using Call Sessions (code sample below), if any of the input parameters are invalid (device ID or phone number), we get the same exception thrown - 'Cannot access a disposed object'

MakeCallOutRequest makeCallOutRequest = new MakeCallOutRequest();
makeCallOutRequest.from = new MakeCallOutCallerInfoRequestFrom { deviceId = fromDeviceId };
makeCallOutRequest.to = new MakeCallOutCallerInfoRequestTo { phoneNumber = numberToDial };
CallSession callSession = await restClient.Restapi().Account().Telephony().CallOut().Post(makeCallOutRequest);

This exception being thrown is preventing us from being able to display a meaningful error to the user. Would it be possible to get a more specific error returned?

vyshakhbabji commented 5 years ago

@tylerlong can u look into this

tylerlong commented 5 years ago

Yes. I am aware of this issue and I am working on the fix.

tylerlong commented 5 years ago

Root cause: https://github.com/dotnet/corefx/issues/1794#issuecomment-333193361

So .net core doesn't auto dispose it while .net framework does. So this is a .net framework only issue.

tylerlong commented 5 years ago

Released version 2.0.2