-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Closed
Labels
Description
Describe the bug
When create multiple RestClient instances with the same HttpClient instance and default options it adds UserAgent header each time.
In certain scenarios it causes System.InvalidOperationException: Collection was modified
To Reproduce
var httpClient = new HttpClient();
var restClient1 = new RestClient(httpClient);
var restClient2 = new RestClient(httpClient);
Assert.AreEqual(2, httpClient.DefaultRequestHeaders.UserAgent.Count);
Assert.AreEqual(httpClient.DefaultRequestHeaders.UserAgent.First(), httpClient.DefaultRequestHeaders.UserAgent.Last());
Expected behavior
Reused httpClient keeps unique UserAgent headers
Desktop (please complete the following information):
- OS: Windows 11 Home
- .NET version: 7
- Version: 109.0.0-preview.1
Additional context
I believe the issues starts here:
https://github.com/restsharp/RestSharp/blob/dev/src/RestSharp/RestClient.cs#L131
In case of default UserAgent value Product?.Name is RestSharp, while UserAgent is RestSharp/x.x.x so the check always passes.