Search code examples
c#asp.net-core.net-coretcpblazor

TCP Receiver Service with Blazor Web Applicaiton


I have a blazor web application but require a background service that accepts TCP Messages. I have created a Blazor App as well as a TCP Sender and Receiver before. But have not put them together.

General Question

How to implement a TCP Receiver with a Blazor Web App?

Specific Question

  • Do I register it as service with dependency Injection? As far as I am aware these services are only called when needed instead of constantly being active.

  • Would it even be possible or would I need to create two separate applications?

  • Are background services able to act as TCP Receivers, in the sense that they are constantly monitoring? Would I be able to take a console app code and add it via:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =>
          {
              webBuilder.UseStartup<Startup>();
          })
          .ConfigureServices(services =>
          {
              services.AddHostedService<TCPReceiver>();
          });
    
  • Can API Controller Endpoints act as TCP receivers or do they have to be called via Rest Requests?

Solution

  • If you are running on your own computer, then yes, a background service could open a socket and listen for TCP connections. If you are publishing to Azure, which for a TCP service seems quite likely, then you have to split the listener out from the website. An Azure Website can only listen on the standard ports (80 and 443), and those are used by Asp.Net.

    A typical Asp.Net entry point looks like this:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
    

    Calling IHostBuilder.Run() instantiates an instance of each registered IHostedService, starts it, waits for the user to try and exit the application, then calls stop on each service. This is a very good place to inject a TCP listener.

    If you do need to host in Azure, you'll have to host the listener differently. There are a few options:

    • Azure Service Fabric: Recommended by Microsoft for this type of use case, but it has quite a high learning curve
    • Azure Cloud Services: Easier to implement, but deprecated. Each deployment requires a VM to be allocated, so it takes quite a while to publish, scale out, etc.
    • Azure Container Instances: With DotNetCore, it's quite easy to create a console application, create a container image based on that application, and host the image in Azure. There's no support for green/blue deployments, you can manage that manually with Azure Traffic Manager.
    • Azure Kubernetes: I haven't used this one, as again it has a larger learning curve, and for a small project I don't think it is worth it.

    You can also host in AWS, but you'll have to google the hosting options there for yourself as I don't have experience with it.