Skip to content

Commit 2a79e02

Browse files
committed
Revert "Feature: allow developers to inject the MethodInfo as a Property (#1367)"
This reverts commit b06ef7c.
1 parent 772c6a0 commit 2a79e02

6 files changed

+21
-177
lines changed

README.md

+1-49
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ services
5656
* [Removing headers](#removing-headers)
5757
* [Passing state into DelegatingHandlers](#passing-state-into-delegatinghandlers)
5858
* [Support for Polly and Polly.Context](#support-for-polly-and-pollycontext)
59-
* [Target Interface type and method info](#target-interface-type-and-method-info)
59+
* [Target Interface type](#target-interface-type)
6060
* [MethodInfo of the method on the Refit client interface that was invoked](#methodinfo-of-the-method-on-the-refit-client-interface-that-was-invoked)
6161
* [Multipart uploads](#multipart-uploads)
6262
* [Retrieving the response](#retrieving-the-response)
@@ -882,54 +882,6 @@ class RequestPropertyHandler : DelegatingHandler
882882

883883
Note: in .NET 5 `HttpRequestMessage.Properties` has been marked `Obsolete` and Refit will instead populate the value into the new `HttpRequestMessage.Options`. Refit provides `HttpRequestMessageOptions.InterfaceTypeKey` and `HttpRequestMessageOptions.RestMethodInfoKey` to respectively access the interface type and REST method info from the options.
884884

885-
#### MethodInfo of the method on the Refit client interface that was invoked
886-
887-
There may be times when you want access to the `MethodInfo` of the method on the Refit client interface that was invoked. An example is where you
888-
want to decorate the method with a custom attribute in order to control some aspect of behavior in a `DelegatingHandler`:
889-
890-
```csharp
891-
public interface ISomeAPI
892-
{
893-
[SomeCustomAttribute("SomeValue")]
894-
[Get("/{id}")]
895-
Task<ApiResponse<SomeClass>> GetById(int id);
896-
}
897-
```
898-
To make the `MethodInfo` available you need to opt-in via the `RefitSettings` like so:
899-
900-
```csharp
901-
services.AddRefitClient<ISomeAPI>(provider => new RefitSettings
902-
{
903-
InjectMethodInfoAsProperty = true
904-
})
905-
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));
906-
```
907-
908-
You can access the `MethodInfo` for use in a handler and then get the custom attributes:
909-
910-
```csharp
911-
class RequestPropertyHandler : DelegatingHandler
912-
{
913-
public RequestPropertyHandler(HttpMessageHandler innerHandler = null) : base(innerHandler ?? new HttpClientHandler()) {}
914-
915-
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
916-
{
917-
// Get the MethodInfo
918-
MethodInfo methodInfo;
919-
request.Options.TryGetValue(HttpRequestMessageOptions.MethodInfoKey, out methodInfo);
920-
921-
//get the custom attributes
922-
var customAttributes = methodInfo.CustomAttributes;
923-
924-
//insert your logic here
925-
926-
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
927-
}
928-
}
929-
```
930-
931-
Note: for .NET Core 3.1 and lower this will be available via `HttpRequestMessage.Properties[HttpRequestMessageOptions.MethodInfo]`.
932-
933885
### Multipart uploads
934886

935887
Methods decorated with `Multipart` attribute will be submitted with multipart content type.

Refit.Tests/MultipartTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ public async Task MultipartUploadShouldWorkWithHeaderAndRequestProperty()
308308
Assert.Equal(someHeader, message.Headers.Authorization.ToString());
309309

310310
#if NET6_0_OR_GREATER
311-
Assert.Equal(2, message.Options.Count());
311+
Assert.Equal(3, message.Options.Count());
312312
Assert.Equal(someProperty, ((IDictionary<string, object>)message.Options)["SomeProperty"]);
313313
#endif
314314

315315
#pragma warning disable CS0618 // Type or member is obsolete
316-
Assert.Equal(2, message.Properties.Count);
316+
Assert.Equal(3, message.Properties.Count);
317317
Assert.Equal(someProperty, message.Properties["SomeProperty"]);
318318
#pragma warning restore CS0618 // Type or member is obsolete
319319
},

Refit.Tests/RequestBuilder.cs

+11-70
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ public class ComplexQueryObject
288288

289289
public class RestMethodInfoTests
290290
{
291-
292291
[Fact]
293292
public void TooManyComplexTypesThrows()
294293
{
@@ -514,7 +513,7 @@ public void ParameterMappingSmokeTest()
514513
Assert.Empty(fixture.QueryParameterMap);
515514
Assert.Null(fixture.BodyParameterInfo);
516515
}
517-
516+
518517
[Fact]
519518
public void ParameterMappingWithTheSameIdInAFewPlaces()
520519
{
@@ -2175,12 +2174,6 @@ public void InterfaceTypeShouldBeInProperties()
21752174
var factory = fixture.BuildRequestFactoryForMethod(nameof(IContainAandB.Ping));
21762175
var output = factory(Array.Empty<object>());
21772176

2178-
#if NET6_0_OR_GREATER
2179-
Assert.NotEmpty(output.Options);
2180-
output.Options.TryGetValue(HttpRequestMessageOptions.InterfaceTypeKey, out var interfaceType);
2181-
Assert.Equal(typeof(IContainAandB), interfaceType);
2182-
#endif
2183-
21842177
#pragma warning disable CS0618 // Type or member is obsolete
21852178
Assert.NotEmpty(output.Properties);
21862179
Assert.Equal(typeof(IContainAandB), output.Properties[HttpRequestMessageOptions.InterfaceType]);
@@ -2189,75 +2182,23 @@ public void InterfaceTypeShouldBeInProperties()
21892182
}
21902183

21912184
[Fact]
2192-
public void MethodInfoShouldBeInPropertiesIfInjectMethodInfoAsPropertyTrue()
2193-
{
2194-
var fixture = new RequestBuilderImplementation<IContainAandB>(new RefitSettings
2195-
{
2196-
InjectMethodInfoAsProperty = true
2197-
});
2198-
var factory = fixture.BuildRequestFactoryForMethod(nameof(IContainAandB.Ping));
2199-
var output = factory(Array.Empty<object>());
2200-
2201-
RestMethodInfo restMethodInfo;
2202-
#if NET6_0_OR_GREATER
2203-
Assert.NotEmpty(output.Options);
2204-
output.Options.TryGetValue(HttpRequestMessageOptions.RestMethodInfoKey, out restMethodInfo);
2205-
Assert.NotNull(restMethodInfo);
2206-
Assert.Equal(nameof(IContainAandB.Ping), restMethodInfo.Name);
2207-
Assert.Equal(typeof(IAmInterfaceA), restMethodInfo.MethodInfo.DeclaringType);
2208-
#endif
2209-
2210-
#pragma warning disable CS0618 // Type or member is obsolete
2211-
Assert.NotEmpty(output.Properties);
2212-
restMethodInfo = (RestMethodInfo)(output.Properties[HttpRequestMessageOptions.RestMethodInfo]);
2213-
Assert.NotNull(restMethodInfo);
2214-
Assert.Equal(nameof(IContainAandB.Ping), restMethodInfo.Name);
2215-
Assert.Equal(typeof(IAmInterfaceA), restMethodInfo.MethodInfo.DeclaringType);
2216-
#pragma warning restore CS0618 // Type or member is obsolete
2217-
}
2218-
2219-
[Fact]
2220-
public void MethodInfoShouldNotBeInPropertiesIfInjectMethodInfoAsPropertyFalse()
2185+
public void RestMethodInfoShouldBeInProperties()
22212186
{
2187+
var someProperty = new object();
22222188
var fixture = new RequestBuilderImplementation<IContainAandB>();
22232189
var factory = fixture.BuildRequestFactoryForMethod(nameof(IContainAandB.Ping));
2224-
var output = factory(Array.Empty<object>());
2190+
var output = factory(new object[] { });
22252191

2226-
RestMethodInfo restMethodInfo;
22272192
#if NET6_0_OR_GREATER
22282193
Assert.NotEmpty(output.Options);
2229-
output.Options.TryGetValue(HttpRequestMessageOptions.RestMethodInfoKey, out restMethodInfo);
2230-
Assert.Null(restMethodInfo);
2231-
#endif
2232-
2233-
#pragma warning disable CS0618 // Type or member is obsolete
2194+
Assert.True(output.Options.TryGetValue(new HttpRequestOptionsKey<RestMethodInfo>(HttpRequestMessageOptions.RestMethodInfo), out var restMethodInfo));
2195+
#else
22342196
Assert.NotEmpty(output.Properties);
2235-
Assert.False(output.Properties.ContainsKey(HttpRequestMessageOptions.RestMethodInfo));
2236-
#pragma warning restore CS0618 // Type or member is obsolete
2237-
}
2238-
2239-
[Fact]
2240-
public void RestMethodInfoShouldBeInProperties()
2241-
{
2242-
var fixture = new RequestBuilderImplementation<IContainAandB>(new() { InjectMethodInfoAsProperty = true });
2243-
var factory = fixture.BuildRequestFactoryForMethod(nameof(IContainAandB.Ping));
2244-
var output = factory(Array.Empty<object>());
2245-
RestMethodInfo restMethodInfo;
2246-
#if NET6_0_OR_GREATER
2247-
Assert.NotEmpty(output.Options);
2248-
output.Options.TryGetValue(HttpRequestMessageOptions.RestMethodInfoKey, out restMethodInfo);
2249-
Assert.NotNull(restMethodInfo);
2250-
Assert.Equal(nameof(IContainAandB.Ping), restMethodInfo.Name);
2251-
Assert.Equal(typeof(IAmInterfaceA), restMethodInfo.MethodInfo.DeclaringType);
2197+
Assert.True(output.Properties.TryGetValue(HttpRequestMessageOptions.RestMethodInfo, out var restMethodInfoObj));
2198+
Assert.IsType<RestMethodInfo>(restMethodInfoObj);
2199+
var restMethodInfo = restMethodInfoObj as RestMethodInfo;
22522200
#endif
2253-
2254-
#pragma warning disable CS0618 // Type or member is obsolete
2255-
Assert.NotEmpty(output.Properties);
2256-
restMethodInfo = (RestMethodInfo)(output.Properties[HttpRequestMessageOptions.RestMethodInfo]);
2257-
Assert.NotNull(restMethodInfo);
22582201
Assert.Equal(nameof(IContainAandB.Ping), restMethodInfo.Name);
2259-
Assert.Equal(typeof(IAmInterfaceA), restMethodInfo.MethodInfo.DeclaringType);
2260-
#pragma warning restore CS0618 // Type or member is obsolete
22612202
}
22622203

22632204
[Fact]
@@ -2293,12 +2234,12 @@ public void DynamicRequestPropertiesWithDuplicateKeyShouldOverwritePreviousPrope
22932234

22942235

22952236
#if NET6_0_OR_GREATER
2296-
Assert.Equal(2, output.Options.Count());
2237+
Assert.Equal(3, output.Options.Count());
22972238
Assert.Equal(someOtherProperty, ((IDictionary<string, object>)output.Options)["SomeProperty"]);
22982239
#endif
22992240

23002241
#pragma warning disable CS0618 // Type or member is obsolete
2301-
Assert.Equal(2, output.Properties.Count);
2242+
Assert.Equal(3, output.Properties.Count);
23022243
Assert.Equal(someOtherProperty, output.Properties["SomeProperty"]);
23032244
#pragma warning restore CS0618 // Type or member is obsolete
23042245
}

Refit/HttpRequestMessageProperties.cs

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Reflection;
2-
31
namespace Refit
42
{
53
/// <summary>
@@ -12,24 +10,9 @@ public static class HttpRequestMessageOptions
1210
/// </summary>
1311
public static string InterfaceType { get; } = "Refit.InterfaceType";
1412

15-
#if NET6_0_OR_GREATER
16-
/// <summary>
17-
/// A typed key to access the <see cref="System.Type"/> of the top-level interface where the method was called from
18-
/// on the <see cref="System.Net.Http.HttpRequestMessage.Options"/>.
19-
/// </summary>
20-
public static System.Net.Http.HttpRequestOptionsKey<System.Type> InterfaceTypeKey { get; } = new(InterfaceType);
21-
#endif
22-
2313
/// <summary>
24-
/// Returns the <see cref="Refit.RestMethodInfo"/> of the method that was called
14+
/// Returns the <see cref="Refit.RestMethodInfo"/> of the top-level interface
2515
/// </summary>
2616
public static string RestMethodInfo { get; } = "Refit.RestMethodInfo";
27-
28-
#if NET6_0_OR_GREATER
29-
/// <summary>
30-
/// A typed key to access the <see cref="Refit.RestMethodInfo"/> of the method that was called
31-
/// </summary>
32-
public static System.Net.Http.HttpRequestOptionsKey<RestMethodInfo> RestMethodInfoKey { get; } = new(RestMethodInfo);
33-
#endif
3417
}
3518
}

Refit/RefitSettings.cs

+1-26
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,21 @@ public RefitSettings()
2727
ExceptionFactory = new DefaultApiExceptionFactory(this).CreateAsync;
2828
}
2929

30-
31-
#if NET6_0_OR_GREATER
32-
/// <summary>
33-
/// Creates a new <see cref="RefitSettings"/> instance with the specified parameters
34-
/// </summary>
35-
/// <param name="contentSerializer">The <see cref="IHttpContentSerializer"/> instance to use</param>
36-
/// <param name="urlParameterFormatter">The <see cref="IUrlParameterFormatter"/> instance to use (defaults to <see cref="DefaultUrlParameterFormatter"/>)</param>
37-
/// <param name="formUrlEncodedParameterFormatter">The <see cref="IFormUrlEncodedParameterFormatter"/> instance to use (defaults to <see cref="DefaultFormUrlEncodedParameterFormatter"/>)</param>
38-
/// <param name="injectMethodInfoAsProperty">Controls injecting the <see cref="MethodInfo"/> of the method on the Refit client interface that was invoked into the HttpRequestMessage.Options (defaults to false)</param>
39-
#else
4030
/// <summary>
4131
/// Creates a new <see cref="RefitSettings"/> instance with the specified parameters
4232
/// </summary>
4333
/// <param name="contentSerializer">The <see cref="IHttpContentSerializer"/> instance to use</param>
4434
/// <param name="urlParameterFormatter">The <see cref="IUrlParameterFormatter"/> instance to use (defaults to <see cref="DefaultUrlParameterFormatter"/>)</param>
4535
/// <param name="formUrlEncodedParameterFormatter">The <see cref="IFormUrlEncodedParameterFormatter"/> instance to use (defaults to <see cref="DefaultFormUrlEncodedParameterFormatter"/>)</param>
46-
/// <param name="injectMethodInfoAsProperty">Controls injecting the <see cref="MethodInfo"/> of the method on the Refit client interface that was invoked into the HttpRequestMessage.Properties (defaults to false)</param>
47-
#endif
4836
public RefitSettings(
4937
IHttpContentSerializer contentSerializer,
5038
IUrlParameterFormatter? urlParameterFormatter = null,
51-
IFormUrlEncodedParameterFormatter? formUrlEncodedParameterFormatter = null,
52-
bool injectMethodInfoAsProperty = false)
39+
IFormUrlEncodedParameterFormatter? formUrlEncodedParameterFormatter = null)
5340
{
5441
ContentSerializer = contentSerializer ?? throw new ArgumentNullException(nameof(contentSerializer), "The content serializer can't be null");
5542
UrlParameterFormatter = urlParameterFormatter ?? new DefaultUrlParameterFormatter();
5643
FormUrlEncodedParameterFormatter = formUrlEncodedParameterFormatter ?? new DefaultFormUrlEncodedParameterFormatter();
5744
ExceptionFactory = new DefaultApiExceptionFactory(this).CreateAsync;
58-
InjectMethodInfoAsProperty = injectMethodInfoAsProperty;
5945
}
6046

6147
/// <summary>
@@ -103,17 +89,6 @@ public RefitSettings(
10389
/// Optional Key-Value pairs, which are displayed in the property <see cref="HttpRequestMessage.Options"/> or <see cref="HttpRequestMessage.Properties"/>.
10490
/// </summary>
10591
public Dictionary<string, object> HttpRequestMessageOptions { get; set; }
106-
107-
#if NET6_0_OR_GREATER
108-
/// <summary>
109-
/// Controls injecting the <see cref="MethodInfo"/> of the method on the Refit client interface that was invoked into the HttpRequestMessage.Options (defaults to false)
110-
/// </summary>
111-
#else
112-
/// <summary>
113-
/// Controls injecting the <see cref="MethodInfo"/> of the method on the Refit client interface that was invoked into the HttpRequestMessage.Properties (defaults to false)
114-
/// </summary>
115-
#endif
116-
public bool InjectMethodInfoAsProperty { get; set; }
11792
}
11893

11994
/// <summary>

Refit/RequestBuilderImplementation.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -746,21 +746,14 @@ Func<object[], HttpRequestMessage> BuildRequestFactoryForMethod(RestMethodInfo r
746746
#endif
747747
}
748748

749-
// Always add the top-level type of the interface to the options/properties and include the MethodInfo if the developer has opted-in to that behavior
749+
// Always add the top-level type of the interface to the properties
750750
#if NET6_0_OR_GREATER
751-
ret.Options.Set(HttpRequestMessageOptions.InterfaceTypeKey, TargetType);
752-
if (settings.InjectMethodInfoAsProperty)
753-
{
754-
ret.Options.Set(HttpRequestMessageOptions.RestMethodInfoKey, restMethod);
755-
}
751+
ret.Options.Set(new HttpRequestOptionsKey<Type>(HttpRequestMessageOptions.InterfaceType), TargetType);
752+
ret.Options.Set(new HttpRequestOptionsKey<RestMethodInfo>(HttpRequestMessageOptions.RestMethodInfo), restMethod);
756753
#else
757754
ret.Properties[HttpRequestMessageOptions.InterfaceType] = TargetType;
758-
if (settings.InjectMethodInfoAsProperty)
759-
{
760-
ret.Properties[HttpRequestMessageOptions.RestMethodInfo] = restMethod;
761-
}
762-
763-
#endif
755+
ret.Properties[HttpRequestMessageOptions.RestMethodInfo] = restMethod;
756+
#endif
764757

765758
// NB: The URI methods in .NET are dumb. Also, we do this
766759
// UriBuilder business so that we preserve any hardcoded query

0 commit comments

Comments
 (0)