Skip to content

Restsharp adds UserAgent headers to HttpClient instance that is passed explicitly #1990

@p-skakun

Description

@p-skakun

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions