Nswag: Set HttpRequestMessage.Properties in C#

Created on 19 Sep 2019  路  5Comments  路  Source: RicoSuter/NSwag

Hi,

Apologies if I've missed something obvious, but there doesn't currently appear to be a way to set values into HttpRequestMessage.Properties from NSwag-generated C# code layers.

This seems to be an existing pattern for controlling specifics of outgoing requests into various instances of DelegatingHandler and the HttpClientFactory chain, that are outside the behaviour of APIs as Swagger would see them, but very much tied into how clients make their actual HTTP calls. Per-request timeouts, OAuth scopes; I've come across a variety of handlers that expect Properties to be accessible to the caller.

Is this something that could be considered, pull-requested etc?

Thanks in advance for your assistance.

Most helpful comment

Thanks @YZahringer for the reply. If I am not mistaken, this only allows static configuration at client construction time. That is a good start, for sure, and helps with some scenarios; much appreciated for the idea!

Our use-case is more aimed at per-request properties, where one single request has a specific and specialised need to set some properties for a given invocation, and a different invocation has a different need (contextual calls, essentially). Managing N different copies of a client, each with unique configuration for that, gets complicated fast in dependency injection.

All 5 comments

You can use ClientBaseClass and ConfigurationClass to pass configuration. Then use UseHttpRequestMessageCreationMethod to create HttpRequestMessage and apply properties.

Example of client base class:
````cs
internal class RestClientBase
{
private MyConfig Configuration { get; }

public RestClientBase(MyConfig configuration)
{
    Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}

public Task<HttpRequestMessage> CreateHttpRequestMessageAsync(CancellationToken _)
{
    var request = new HttpRequestMessage();
    request.Properties[nameof(MyConfig)] = Configuration;
    return Task.FromResult(request);
}

}
````

Thanks @YZahringer for the reply. If I am not mistaken, this only allows static configuration at client construction time. That is a good start, for sure, and helps with some scenarios; much appreciated for the idea!

Our use-case is more aimed at per-request properties, where one single request has a specific and specialised need to set some properties for a given invocation, and a different invocation has a different need (contextual calls, essentially). Managing N different copies of a client, each with unique configuration for that, gets complicated fast in dependency injection.

Thanks you life saver @YZahringer
That is exactly what I need.

@RicoSuter it would have been nice to know why this was closed? Just no plans to support this?

You can do that with CreateHttpRequestMessageAsync, we dont want to add a config for every possibility - would lead to a maintenance nightmare

Was this page helpful?
0 / 5 - 0 ratings