-
-
Notifications
You must be signed in to change notification settings - Fork 747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the RestMethodInfo
available in the request options
#1317
Make the RestMethodInfo
available in the request options
#1317
Conversation
63b5054
to
a3c0a70
Compare
While having the interface type available is nice, it might not be enough if using reflection on the called method is desired. Providing the `RestMethodInfo` opens new possibilities to introspect the called method. Currently, to workaround this missing information, I'm using a `methodName` parameter decorated with a `[Property]` attribute and a default value to make it possible to introspect the return type of the called method. At the interface method: ```csharp Task<ApiResponse<User>> GetUserAsync(string id, [Property] string methodName = nameof(GetUserAsync)); ``` Inside the HTTP message handler: ```csharp request.Options.TryGetValue(new HttpRequestOptionsKey<Type>(HttpRequestMessageOptions.InterfaceType), out var refitInterfaceType) request.Options.TryGetValue(new HttpRequestOptionsKey<string>("methodName"), out var methodName) var methodReturnType = refitInterfaceType.GetMethod(methodName).ReturnType; ``` With the new `RestMethodInfo`, it becomes possible to access the method without having to pollute the interface definition: ```csharp request.Options.TryGetValue(HttpRequestMessageOptions.RestMethodInfoKey, out var restMethodInfo); var methodReturnType = restMethodInfo.MethodInfo.ReturnType; ``` Also, the new `HttpRequestMessageOptions.InterfaceTypeKey` and `HttpRequestMessageOptions.RestMethodInfoKey` (available on .NET 5 onwards) make it easier to access the request options.
a3c0a70
to
53ab900
Compare
Please can someone review this pull request? @clairernovotny @hamidrezakp @dannyBies |
I really want to see the I've submitted a PR for this prior that was rejected for being too fancy, but part of my motivation for that particular (flexible) solution I came up with was not wanting to have the Previously the author/maintainers have expressed a desire to simply include the method name as a string, but from my point of view once you have an overloaded method on your interface then all bets are off and I would expect that to cause bugs and support issues somewhere down the line. Passing the I might submit a PR for that particular implementation as seeing renewed interest in it has sparked my desire to see this particular feature get across the line in some format. |
I would love to have this to be able to override services
.AddRefitClient()
.ConfigureHttpClient(httpClient => httpClient.Timeout = TimeSpan.FromSeconds(30)); public class HttpMessageHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var timeoutSeconds = 15; // ToDo: read configuration for operation identified by request.Options[HttpRequestMessageOptions.RestMethodInfoKey]
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds));
return await base.SendAsync(request, timeoutTokenSource.Token);
}
} |
@james-s-tayler when dou you plan to handle this pr there are lots of demands on this subject passing method info to delegating handler. Its needed most cases; passing timeout, getting some kind of caching behaviour or return type per operation. |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
While having the interface type available is nice, it might not be enough if using reflection on the called method is desired. Providing the
RestMethodInfo
opens new possibilities to introspect the called method.Currently, to workaround this missing information, I'm using a
methodName
parameter decorated with a[Property]
attribute and a default value to make it possible to introspect the return type of the called method.At the interface method:
Inside the HTTP message handler:
With the new
RestMethodInfo
, it becomes possible to access the method without having to pollute the interface definition:Also, the new
HttpRequestMessageOptions.InterfaceTypeKey
andHttpRequestMessageOptions.RestMethodInfoKey
(available on .NET 5 onwards) make it easier to access the request options.