Cách duy nhất tôi có thể lấy Docker/Linux để giữ ứng dụng .NET Core của tôi còn sống là giả mạo ASP.NET vào lưu trữ nó cho tôi ... Đây là một hack xấu xí như vậy !!
Làm theo cách này sẽ chạy trong Docker bằng cách sử dụng tùy chọn docker run -d
, do đó bạn không cần phải có kết nối trực tiếp để giữ luồng STDIN còn sống.
tôi tạo một giao diện điều khiển ứng dụng .NET Core (không phải là một ứng dụng ASP.NET) và lớp Chương trình của tôi trông như thế này:
public class Program
{
public static ManualResetEventSlim Done = new ManualResetEventSlim(false);
public static void Main(string[] args)
{
//This is unbelievably complex because .NET Core Console.ReadLine() does not block in a docker container...!
var host = new WebHostBuilder().UseStartup(typeof(Startup)).Build();
using (CancellationTokenSource cts = new CancellationTokenSource())
{
Action shutdown =() =>
{
if (!cts.IsCancellationRequested)
{
Console.WriteLine("Application is shutting down...");
cts.Cancel();
}
Done.Wait();
};
Console.CancelKeyPress += (sender, eventArgs) =>
{
shutdown();
// Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
};
host.Run(cts.Token);
Done.Set();
}
}
}
Các Startup lớp:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServer, ConsoleAppRunner>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
}
}
Lớp ConsoleAppRunner
public class ConsoleAppRunner : IServer
{
/// <summary>A collection of HTTP features of the server.</summary>
public IFeatureCollection Features { get; }
public ConsoleAppRunner(ILoggerFactory loggerFactory)
{
Features = new FeatureCollection();
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
}
/// <summary>Start the server with an application.</summary>
/// <param name="application">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.Server.IHttpApplication`1" />.</param>
/// <typeparam name="TContext">The context associated with the application.</typeparam>
public void Start<TContext>(IHttpApplication<TContext> application)
{
//Actual program code starts here...
Console.WriteLine("Demo app running...");
Program.Done.Wait(); // <-- Keeps the program running - The Done property is a ManualResetEventSlim instance which gets set if someone terminates the program.
}
}
Điều tuyệt vời duy nhất là bạn có thể sử dụng DI trong ứng dụng của mình (nếu bạn muốn) - vì vậy trong trường hợp sử dụng của tôi, tôi đang sử dụng ILoggingFactory để xử lý việc ghi nhật ký của mình.
Tôi đang cố gắng tìm hiểu bản thân mình: Tôi nghĩ rằng bạn muốn chạy nó trong chế độ tương tác và có thể muốn thêm một nhiệm kỳ. 'docker run -it --name demo Demo' – hdz
bạn cũng có thể đính kèm vào nó' docker attach {container} 'để quay lại nó nếu bạn chạy nó ở chế độ nền (-d). Bạn sẽ không nhìn thấy đầu ra vì nó đã xuất ra nhưng bạn sẽ có thể nhấn enter cho vùng chứa để thoát khỏi – hdz