title: (How) Can I use ASP.NET Core for custom TCP packet communication?
body: <p>I have a dated game server with typical TCP packet protocol (length / type / data[length]), and my task is to find modern frameworks to rewrite it in.</p>
  
  <p>Having written a (small) RESTish API in ASP.NET Core 2.1, I know that it provides DI / logging / configuration features which could perfectly replace similar functionality in the old server. I thought I could use ASP.NET Core, making it handle said TCP packets instead of HTTP requests.</p>
  
  <p>Now I'm a bit stuck in where to start and if / how it would fit into ASP.NET Core.
  So far I've RTFM'ed about inner ASP.NET Core workings and found some things that confused me:</p>
  
  <ul>
  <li><a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1" rel="nofollow noreferrer">Official docs</a> state Kestrel only supports HTTP based scenarios (don't wanna use IIS). So does the <a href="https://stackoverflow.com/questions/tagged/kestrel-http-server">tag</a> here on SO.</li>
  <li>Then I found a <a href="https://github.com/davidfowl/MultiProtocolAspNetCore" rel="nofollow noreferrer">project</a> claiming to add TCP support to Kestrel, yet I don't fully understand how it's done. It seems to be done by implementing a <code>ConnectionHandler</code>, but I can't see, for example, where it starts a <code>TcpListener</code> to accept new continuous connections with clients.</li>
  <li>With Kestrel seemingly out of the question, I thought about writing an <code>IHostedService</code> or <code>BackgroundService</code> to do all TCP communication in.</li>
  <li>Then I didn't know if I could use Middleware to pipe my TCP packets through (thought of an authorization and game logic middleware). <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1" rel="nofollow noreferrer">Official docs</a> quickly talk about HTTP or "web" requests, so I thought a typical middleware pipeline is out of the question too.</li>
  </ul>
  
  <p>To sum it up, my research resulted in the following two questions:</p>
  
  <ul>
  <li>Can I actually use Kestrel for continuous TCP connections with binary communication?</li>
  <li>What is the relation between the middleware pipeline and HTTP functionality / Kestrel? Can I use a middleware pipeline in custom services for TCP communication?</li>
  </ul>