C
C#9mo ago
Demonik

RestSharp multipart request in from Unity fails with NRE after successfully sending request

With this stack trace:
NullReferenceException: Object reference not set to an instance of an object
System.Net.Http.MultipartContent.Dispose (System.Boolean disposing) (at <92a43029e91441ee9df5714eb5dcd209>:0)
System.Net.Http.HttpContent.Dispose () (at <92a43029e91441ee9df5714eb5dcd209>:0)
RestSharp.RequestContent.Dispose () (at <dfd03b4ab8614e549933c37db1618c06>:0)
RestSharp.RestClient.ExecuteRequestAsync (RestSharp.RestRequest request, System.Threading.CancellationToken cancellationToken) (at <dfd03b4ab8614e549933c37db1618c06>:0)
RestSharp.RestClient.ExecuteAsync (RestSharp.RestRequest request, System.Threading.CancellationToken cancellationToken) (at <dfd03b4ab8614e549933c37db1618c06>:0)
TestComponent.TestWebhookSend () (at Assets/Runtime/Scripts/TestComponent.cs:106)
NullReferenceException: Object reference not set to an instance of an object
System.Net.Http.MultipartContent.Dispose (System.Boolean disposing) (at <92a43029e91441ee9df5714eb5dcd209>:0)
System.Net.Http.HttpContent.Dispose () (at <92a43029e91441ee9df5714eb5dcd209>:0)
RestSharp.RequestContent.Dispose () (at <dfd03b4ab8614e549933c37db1618c06>:0)
RestSharp.RestClient.ExecuteRequestAsync (RestSharp.RestRequest request, System.Threading.CancellationToken cancellationToken) (at <dfd03b4ab8614e549933c37db1618c06>:0)
RestSharp.RestClient.ExecuteAsync (RestSharp.RestRequest request, System.Threading.CancellationToken cancellationToken) (at <dfd03b4ab8614e549933c37db1618c06>:0)
TestComponent.TestWebhookSend () (at Assets/Runtime/Scripts/TestComponent.cs:106)
This is what I'm doing:
request.Method = Method.Post;
request.AddFile("file1", file.FullName);
request.AddParameter("payload_json", message.ToJsonString(), ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
request.Method = Method.Post;
request.AddFile("file1", file.FullName);
request.AddParameter("payload_json", message.ToJsonString(), ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
It actually successfully sends request but immediately throws exception inside. Without attachment it works fine. Am I doing something wrong or is this a bug?
1 Reply
Demonik
DemonikOP9mo ago
Exactly same request but without multipart works RestSharp 110.2.0 I'll just post since I figured out what issue was - it's with the System.Net.Http bundled with unity, it has a bug where double dispose on MultipartContent causes NRE because it sets internal list to null in dispose and doesn't check for null before enumerating; this issue isn't present in the reference .NET code and I found it in Unity one by debugging with dnspy the only way to avoid issue it to directly use HttpClient for multipart requests and avoid doing double dispose (it disposes request itself on send) this is a bug with assembly bundled with Unity because double dispose is supposed to be safe to do
Want results from more Discord servers?
Add your server