question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set HttpRequestMessage.Properties in C#

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
hotchkjcommented, Feb 29, 2020

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.

2reactions
YZahringercommented, Feb 29, 2020

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

Example of client base class:

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);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

HttpRequestMessage.Properties Property (System.Net.Http)
Gets a set of properties for the HTTP request.
Read more >
What is HttpRequestMessage.Properties?
The HttpRequestMessage class has a Properties property, which is a dictionary in which we can put whatever we need.
Read more >
HttpRequestMessage Property
Gets object of object.
Read more >
C# : What is HttpRequestMessage.Properties? - YouTube
C# : What is HttpRequestMessage. Properties ? To Access My Live Chat Page, On Google, Search for "hows tech developer connect" As I...
Read more >
C# (CSharp) Microsoft.Http HttpRequestMessage Examples
Http HttpRequestMessage - 35 examples found. These are the top rated real world C# (CSharp) examples of Microsoft.Http.HttpRequestMessage extracted from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found