How to write a linux daemon with .Net Core

后端 未结 5 1960
离开以前
离开以前 2021-01-31 05:22

I could just write a long-running CLI app and run it, but I\'m assuming it wouldn\'t comply to all the expectations one would have of a standards-compliant linux daemon (respond

5条回答
  •  遇见更好的自我
    2021-01-31 06:06

    If you're trying to find something more robust, I found an implementation on Github that looks promising: .NET Core Application blocks for message-based communication. It uses Host, HostBuilder, ApplicationServices, ApplicationEnvironment, etc classes to implement a messaging service.

    It doesn't quite look ready for black box reuse, but it seems like it could be a good starting point.

    var host = new HostBuilder()
                .ConfigureServices(services =>
                {
                    var settings = new RabbitMQSettings { ServerName = "192.168.80.129", UserName = "admin", Password = "Pass@word1" };
               })
                .Build();
    
    Console.WriteLine("Starting...");
    await host.StartAsync();
    
    var messenger = host.Services.GetRequiredService();
    
    Console.WriteLine("Running. Type text and press ENTER to send a message.");
    
    Console.CancelKeyPress += async (sender, e) =>
    {
        Console.WriteLine("Shutting down...");
        await host.StopAsync(new CancellationTokenSource(3000).Token);
        Environment.Exit(0);
    };
    ...
    

提交回复
热议问题