Skip to content

Commit 4dc910d

Browse files
committed
Add support for MS DI and client factory
1 parent 4eff2fa commit 4dc910d

File tree

18 files changed

+300
-143
lines changed

18 files changed

+300
-143
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<SystemTextJsonVer>9.0.10</SystemTextJsonVer>
1717
</PropertyGroup>
1818
<ItemGroup Label="Runtime dependencies">
19+
<PackageVersion Include="Microsoft.Extensions.Http" Version="10.0.0" />
1920
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
2021
<PackageVersion Include="CsvHelper" Version="33.0.1" />
2122
<PackageVersion Include="System.Text.Json" Version="$(SystemTextJsonVer)" />

RestSharp.sln

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SourceGen", "SourceGen", "{
3737
EndProject
3838
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGenerator", "gen\SourceGenerator\SourceGenerator.csproj", "{FE778406-ADCF-45A1-B775-A054B55BFC50}"
3939
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp.Extensions.DependencyInjection", "src\RestSharp.Extensions.DependencyInjection\RestSharp.Extensions.DependencyInjection.csproj", "{92A6F3CA-100F-4D9D-9742-B62267D445B6}"
41+
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp.Tests.DependencyInjection", "test\RestSharp.Tests.DependencyInjection\RestSharp.Tests.DependencyInjection.csproj", "{602FF788-E926-4404-B3A2-D6B778A5FFB7}"
43+
EndProject
4044
Global
4145
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4246
Debug.Appveyor|Any CPU = Debug.Appveyor|Any CPU
@@ -446,6 +450,66 @@ Global
446450
{FE778406-ADCF-45A1-B775-A054B55BFC50}.Release|x64.Build.0 = Release|Any CPU
447451
{FE778406-ADCF-45A1-B775-A054B55BFC50}.Release|x86.ActiveCfg = Release|Any CPU
448452
{FE778406-ADCF-45A1-B775-A054B55BFC50}.Release|x86.Build.0 = Release|Any CPU
453+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|Any CPU.ActiveCfg = Debug|Any CPU
454+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|Any CPU.Build.0 = Debug|Any CPU
455+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|ARM.ActiveCfg = Debug|Any CPU
456+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|ARM.Build.0 = Debug|Any CPU
457+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|Mixed Platforms.ActiveCfg = Debug|Any CPU
458+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|Mixed Platforms.Build.0 = Debug|Any CPU
459+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|x64.ActiveCfg = Debug|Any CPU
460+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|x64.Build.0 = Debug|Any CPU
461+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|x86.ActiveCfg = Debug|Any CPU
462+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug.Appveyor|x86.Build.0 = Debug|Any CPU
463+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
464+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
465+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|ARM.ActiveCfg = Debug|Any CPU
466+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|ARM.Build.0 = Debug|Any CPU
467+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
468+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
469+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|x64.ActiveCfg = Debug|Any CPU
470+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|x64.Build.0 = Debug|Any CPU
471+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|x86.ActiveCfg = Debug|Any CPU
472+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Debug|x86.Build.0 = Debug|Any CPU
473+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
474+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|Any CPU.Build.0 = Release|Any CPU
475+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|ARM.ActiveCfg = Release|Any CPU
476+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|ARM.Build.0 = Release|Any CPU
477+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
478+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|Mixed Platforms.Build.0 = Release|Any CPU
479+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|x64.ActiveCfg = Release|Any CPU
480+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|x64.Build.0 = Release|Any CPU
481+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|x86.ActiveCfg = Release|Any CPU
482+
{92A6F3CA-100F-4D9D-9742-B62267D445B6}.Release|x86.Build.0 = Release|Any CPU
483+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|Any CPU.ActiveCfg = Debug|Any CPU
484+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|Any CPU.Build.0 = Debug|Any CPU
485+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|ARM.ActiveCfg = Debug|Any CPU
486+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|ARM.Build.0 = Debug|Any CPU
487+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|Mixed Platforms.ActiveCfg = Debug|Any CPU
488+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|Mixed Platforms.Build.0 = Debug|Any CPU
489+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|x64.ActiveCfg = Debug|Any CPU
490+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|x64.Build.0 = Debug|Any CPU
491+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|x86.ActiveCfg = Debug|Any CPU
492+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug.Appveyor|x86.Build.0 = Debug|Any CPU
493+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
494+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
495+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|ARM.ActiveCfg = Debug|Any CPU
496+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|ARM.Build.0 = Debug|Any CPU
497+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
498+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
499+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|x64.ActiveCfg = Debug|Any CPU
500+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|x64.Build.0 = Debug|Any CPU
501+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|x86.ActiveCfg = Debug|Any CPU
502+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Debug|x86.Build.0 = Debug|Any CPU
503+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
504+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|Any CPU.Build.0 = Release|Any CPU
505+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|ARM.ActiveCfg = Release|Any CPU
506+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|ARM.Build.0 = Release|Any CPU
507+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
508+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|Mixed Platforms.Build.0 = Release|Any CPU
509+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|x64.ActiveCfg = Release|Any CPU
510+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|x64.Build.0 = Release|Any CPU
511+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|x86.ActiveCfg = Release|Any CPU
512+
{602FF788-E926-4404-B3A2-D6B778A5FFB7}.Release|x86.Build.0 = Release|Any CPU
449513
EndGlobalSection
450514
GlobalSection(SolutionProperties) = preSolution
451515
HideSolutionNode = FALSE
@@ -463,6 +527,7 @@ Global
463527
{2150E333-8FDC-42A3-9474-1A3956D46DE8} = {8C7B43EB-2F93-483C-B433-E28F9386AD67}
464528
{E6D94FFD-7811-40BE-ABC4-6D6AB41F0060} = {9051DDA0-E563-45D5-9504-085EBAACF469}
465529
{FE778406-ADCF-45A1-B775-A054B55BFC50} = {55B8F371-B2BA-4DEE-AB98-5BAB8A21B1C2}
530+
{602FF788-E926-4404-B3A2-D6B778A5FFB7} = {9051DDA0-E563-45D5-9504-085EBAACF469}
466531
EndGlobalSection
467532
GlobalSection(ExtensibilityGlobals) = postSolution
468533
SolutionGuid = {77FF357B-03FA-4FA5-A68F-BFBE5800FEBA}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="Microsoft.Extensions.Http" />
7+
</ItemGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\RestSharp\RestSharp.csproj" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace RestSharp.Extensions.DependencyInjection;
4+
5+
public static class ServiceCollectionExtensions {
6+
const string DefaultRestClient = "DefaultRestClient";
7+
8+
extension(IServiceCollection services) {
9+
/// <summary>
10+
/// Adds a RestClient to the service collection.
11+
/// </summary>
12+
/// <param name="options">The configuration options for the RestClient.</param>
13+
[PublicAPI]
14+
public void AddRestClient(RestClientOptions options) {
15+
services
16+
.AddHttpClient(DefaultRestClient)
17+
.ConfigureHttpClient(client => RestClient.ConfigureHttpClient(client, options))
18+
.ConfigurePrimaryHttpMessageHandler(() => {
19+
var handler = new HttpClientHandler();
20+
RestClient.ConfigureHttpMessageHandler(handler, options);
21+
return handler;
22+
}
23+
);
24+
25+
services.AddTransient<IRestClient>(sp => {
26+
var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient(DefaultRestClient);
27+
return new RestClient(client, options);
28+
}
29+
);
30+
}
31+
32+
/// <summary>
33+
/// Adds a RestClient to the service collection with default options.
34+
/// </summary>
35+
[PublicAPI]
36+
public void AddRestClient() => services.AddRestClient(new RestClientOptions());
37+
38+
/// <summary>
39+
/// Adds a RestClient to the service collection with a base URL.
40+
/// </summary>
41+
/// <param name="baseUrl">The base URL for the RestClient.</param>
42+
[PublicAPI]
43+
public void AddRestClient(string baseUrl) => services.AddRestClient(new RestClientOptions(baseUrl));
44+
}
45+
}

src/RestSharp/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Runtime.CompilerServices;
22

33
[assembly:
4+
InternalsVisibleTo(
5+
"RestSharp.Extensions.DependencyInjection, PublicKey=0024000004800000940000000602000000240000525341310004000001000100fda57af14a288d46e3efea89617037585c4de57159cd536ca6dff792ea1d6addc665f2fccb4285413d9d44db5a1be87cb82686db200d16325ed9c42c89cd4824d8cc447f7cee2ac000924c3bceeb1b7fcb5cc1a3901785964d48ce14172001084134f4dcd9973c3776713b595443b1064bb53e2eeb924969244d354e46495e9d"
6+
),
47
InternalsVisibleTo(
58
"RestSharp.InteractiveTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100fda57af14a288d46e3efea89617037585c4de57159cd536ca6dff792ea1d6addc665f2fccb4285413d9d44db5a1be87cb82686db200d16325ed9c42c89cd4824d8cc447f7cee2ac000924c3bceeb1b7fcb5cc1a3901785964d48ce14172001084134f4dcd9973c3776713b595443b1064bb53e2eeb924969244d354e46495e9d"
69
),

src/RestSharp/RestClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ public RestClient(
224224
)
225225
: this(new HttpClient(handler, disposeHandler), true, configureRestClient, configureSerialization) { }
226226

227-
static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options) {
227+
internal static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options) {
228228
if (options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = options.Expect100Continue;
229229
}
230230

231231
// ReSharper disable once CognitiveComplexity
232-
static void ConfigureHttpMessageHandler(HttpClientHandler handler, RestClientOptions options) {
232+
internal static void ConfigureHttpMessageHandler(HttpClientHandler handler, RestClientOptions options) {
233233
#if NET
234234
if (!OperatingSystem.IsBrowser()) {
235235
#endif

src/RestSharp/RestSharp.csproj

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,18 @@
1313
<PackageReference Include="Nullable" PrivateAssets="All"/>
1414
</ItemGroup>
1515
<ItemGroup>
16-
<Compile Update="RestClient.Extensions.Params.cs">
17-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
18-
</Compile>
1916
<Compile Update="RestClient.Async.cs">
2017
<DependentUpon>RestClient.cs</DependentUpon>
2118
</Compile>
22-
<Compile Update="Request\PropertyCache.Populator.cs">
23-
<DependentUpon>PropertyCache.cs</DependentUpon>
24-
</Compile>
25-
<Compile Update="Request\PropertyCache.Populator.RequestProperty.cs">
19+
<Compile Update="Request\PropertyCache.*.cs">
2620
<DependentUpon>PropertyCache.cs</DependentUpon>
2721
</Compile>
28-
<Compile Update="RestClient.Extensions.Get.cs">
29-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
30-
</Compile>
31-
<Compile Update="RestClient.Extensions.Post.cs">
32-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
33-
</Compile>
34-
<Compile Update="RestClient.Extensions.Put.cs">
35-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
36-
</Compile>
37-
<Compile Update="RestClient.Extensions.Patch.cs">
38-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
39-
</Compile>
40-
<Compile Update="RestClient.Extensions.Head.cs">
22+
<Compile Update="RestClient.Extensions.*.cs">
4123
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
4224
</Compile>
43-
<Compile Update="RestClient.Extensions.Delete.cs">
44-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
45-
</Compile>
46-
<Compile Update="RestClient.Extensions.Options.cs">
47-
<DependentUpon>RestClient.Extensions.cs</DependentUpon>
48-
</Compile>
49-
<Compile Update="Request\RestRequestExtensions.Headers.cs">
50-
<DependentUpon>RestRequestExtensions.cs</DependentUpon>
51-
</Compile>
52-
<Compile Update="Request\RestRequestExtensions.Url.cs">
25+
<Compile Update="Request\RestRequestExtensions.*.cs">
5326
<DependentUpon>RestRequestExtensions.cs</DependentUpon>
5427
</Compile>
55-
<Compile Update="Request\RestRequestExtensions.Query.cs">
56-
<DependentUpon>RestRequestExtensions.cs</DependentUpon>
57-
</Compile>
58-
<Compile Update="Request\RestRequestExtensions.File.cs">
59-
<DependentUpon>RestRequestExtensions.cs</DependentUpon>
60-
</Compile>
61-
<Compile Update="Request\RestRequestExtensions.Body.cs">
62-
<DependentUpon>RestRequestExtensions.cs</DependentUpon>
63-
</Compile>
64-
<Compile Update="Request\RestRequestExtensions.Object.cs">
65-
<DependentUpon>RestRequestExtensions.cs</DependentUpon>
66-
</Compile>
6728
</ItemGroup>
6829
<ItemGroup Label="Source generator">
6930
<ProjectReference Include="$(RepoRoot)\gen\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using RestSharp.Extensions.DependencyInjection;
3+
using RestSharp.Tests.Shared;
4+
using RestSharp.Tests.Shared.Server;
5+
6+
namespace RestSharp.Tests.DependencyInjection;
7+
8+
public sealed class RequestTests
9+
: RequestTestsBase, IClassFixture<WireMockTestServer>, IDisposable {
10+
readonly ServiceProvider _provider;
11+
12+
public RequestTests(WireMockTestServer server) : base(false) {
13+
var services = new ServiceCollection();
14+
services.AddRestClient(server.Url!);
15+
_provider = services.BuildServiceProvider();
16+
}
17+
18+
public void Dispose() => _provider.Dispose();
19+
20+
protected override IRestClient GetClient() => _provider.GetRequiredService<IRestClient>();
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\RestSharp.Extensions.DependencyInjection\RestSharp.Extensions.DependencyInjection.csproj" />
9+
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

test/RestSharp.Tests.Integrated/Interceptor/InterceptorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// ReSharper disable AccessToDisposedClosure
2+
23
namespace RestSharp.Tests.Integrated.Interceptor;
34

45
public class InterceptorTests(WireMockTestServer server) : IClassFixture<WireMockTestServer> {

0 commit comments

Comments
 (0)