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.
Describe the bug
When create multiple
RestClientinstances with the same HttpClient instance and default options it addsUserAgentheader each time.In certain scenarios it causes
System.InvalidOperationException: Collection was modifiedTo Reproduce
Expected behavior
Reused httpClient keeps unique
UserAgentheadersDesktop (please complete the following information):
Additional context
I believe the issues starts here:
https://github.com/restsharp/RestSharp/blob/dev/src/RestSharp/RestClient.cs#L131
In case of default
UserAgentvalueProduct?.NameisRestSharp, whileUserAgentisRestSharp/x.x.xso the check always passes.