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.

Add ConnectionContext.Items to ConnectionInfo

See original GitHub issue

Is your feature request related to a problem? Please describe.

I added middleware to ConnectionBuilder in order to detect request source. Later I expected to find my data at HttpContext.Items. But I found that ConnectionContext.Items and HttpContext.Items are different objects.

webHostBuilder.ConfigureKestrel((context, options) =>
    {
        options.Listen(IPAddress.Parse("0.0.0.0"), 3301, listenOptions =>
        {
            listenOptions.Use((connectionContext, next) =>
              {
                  connectionContext.Items.Add("kestrelEndpoint", "3301");
                  return next();
              });
        });
      File.Delete("/tmp/api.socket");
      options.ListenUnixSocket("/tmp/api.socket", listenOptions =>
      {
           listenOptions.Use((connectionContext, next) =>
           {
               connectionContext.Items.Add("kestrelEndpoint", "unixSocket");
               return next();
           });
      });
 });

Describe the solution you’d like

Add ConnectionContext.Items to ConnectionInfo along with ConnectionId and some other fields. So, ConnectionContext.Items will be accessible over HttpContext.Connection in middlewares and controllers. And it will be possible to pass any needed data from connection processing pipeline to request pipeline.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
halter73commented, Dec 8, 2020

Neither Kestrel nor HttpSys will be affected by passing already existed data from connection context (which is not Kestrel or HttpSys specific) to http context.

ConnectionContext is a Kestrel-specific API today. Other servers could theoretically implement the abstraction, but I’m not aware of any others that do today. As @Tratcher points out, the HTTP.Sys doesn’t give you direct control over the underlying connection making implementing the abstraction in HttpSysServer impossible.

Even in Kestrel’s case, I don’t we should combine ConnectionContext.Items and HttpContext.Items because it’s a pretty niche use case and it would technically breaking. There is a pretty easy workaround though:

To access ConnectionContext.Items and ConnectionContext.Id from request middleware, you can do something like the following:

IDictionary<object, object> connectionItems = httpContext.Features.Get<IConnectionItemsFeature>().Items;
string connectionId = httpContext.Connection.Id;

Does this work for you @coderuid?

1reaction
davidfowlcommented, Dec 15, 2020

It’s not available in the enumeration currently but maybe it should be.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ConnectionContext Class
Items. Gets or sets a key/value collection that can be used to share data within the scope of this connection.
Read more >
How to add custom attributes to SQL connection string?
Method 1: Use the Application Name keyword in the connection string to pass up to 128 characters and retrieve with the APP_NAME() T-SQL...
Read more >
Injecting connection context information using a persistent ...
Upon establishing the connection, you just need to create the table. Then insert whatever Key/Value pairs you like. The table will automatically ...
Read more >
Connection Context Concepts
Connection Contexts. SQLJ supports connection contexts for connecting to different types of database schemas from the same application.
Read more >
Connection Context | Advanced | Collaboration Kit | Tools
You can create your own connection context by implementing the ConnectionContext interface, or by using one of the provided implementations. A ...
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