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.

Provide support for factory method for building a derived instance of ConnectionHandler

See original GitHub issue

I am using ServerBuilder UseConnectionHandler to build my Server class. I would like to resolve connection handler class from an external container (Castle Windsor) in order to be able to inject dependencies in my ConnectionHandler. I cannot find a way in the library’ ServerBuilder API to resolve an instance of ConnectionHandler externally at client connect time

Is it feasible to use a factory method for resolving connection handlers?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JanEggerscommented, Jan 7, 2020

this is the original code: https://github.com/aspnet/AspNetCore/blob/c17ce436b8703470231e7979cead042f5682c3f5/src/Servers/Connections.Abstractions/src/ConnectionBuilderExtensions.cs#L12-L18

you could add your own like:

public static IConnectionBuilder UseConnectionHandler<TConnectionHandler>(this IConnectionBuilder connectionBuilder, Func<TConnectionHandler> factory) where TConnectionHandler : ConnectionHandler
        {
            var handler = factory():

            // This is a terminal middleware, so there's no need to use the 'next' parameter
            return connectionBuilder.Run(connection => handler.OnConnectedAsync(connection));
        }

I think this method does not provide enough value to be in the box.

For my usage, clearly a much better way would be to instantiate ConnectionHandler on each new client connection.

in the end it is just a middleware you can create your own on each connection like:

        public static IConnectionBuilder UseWhateverHandler <TConnectionHandler>(this IConnectionBuilder connectionBuilder, Func<TWhateverHandler > factory) where TWhateverHandler : WhateverHasNext
        {
            return connectionBuilder.Run(connection => factory().Whatever(connection));
        } 
0reactions
davidfowlcommented, Jan 7, 2020

@JanEggers what you suggested in cleaner.

public static IConnectionBuilder UseWhateverHandler<TConnectionHandler>(this IConnectionBuilder connectionBuilder, Func<TWhateverHandler> factory) where TWhateverHandler : WhateverHasNext
{
   return connectionBuilder.Run(connection => factory().Whatever(connection));
} 
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Factory Pattern to build many derived classes
I have a factory object ChallengeManager to generate instances of a Challenge object for a game I'm building. There are many challenges. The ......
Read more >
The Factory Method Pattern and Its Implementation in Python
Factory Method is a creational design pattern used to create concrete implementations of a common interface. It separates the process of creating an...
Read more >
Should I use the Factory Pattern when instantiating objects ...
you want a factory to create IShape objects from this data source (so having one and only one place in code to modify...
Read more >
Factory method for designing pattern
The Factory Method pattern is used to create objects without specifying the exact class of object that will be created. This pattern is...
Read more >
Factory Method Design Pattern
Factory Method is a creational design pattern that provides an interface for ... Creates an instance of several derived classes; Object Pool
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