问题
I am looking at taking the Docker plunge with a Microservices Framework. I have seen numerous examples of exposing an endpoint to be called by something to do something (get me the weather at a location, the exchange rate for currency, etc). I have not seen anything that talks about how to replace my current Windows Service type applications or applications that subscribe to queues to do their work.
For Example, lets say I have a windows service that every day at 2:00 AM zips all of the files in a given directory and puts them into a different directory (then publishes a message that it was completed).
Do I build a asp.net core "console app" and add a startup.cs? Do I need a startup.cs or just a startup method in my main?
Like I said, lots of demos of building a tiny web response but little about what else to do if I do not want/need and endpoint.
回答1:
Startup.cs is quite ASP.NET Core specific, which by itself is a web stack and comes hosted with WebListener or Kestrel behind IIS.
In an console application you wouldn't use a traditional Startup.cs, though you could to have a consistent base, but it would look a bit differently though, because you are in control of creating the IoC container (rather than ASP.NET Core doing it for you in an web application). Normally the Startup.cs is processed by the WebHostBuilder in Program.cs, to allow it to inject it's own stuff before passing the IServiceCollection to the ConfigureServices method.
You're right that you need an console application and there you'd do all the stuff yourself.
public class Program
{
public IConfigurationRoot Configuration { get; private set; }
public IServiceProvider Provider { get; private set; }
public static void Main()
{
var programm = new Programm();
program.Run();
}
private void Run()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
var services = new ServiceCollection();
ConfigureServices(services);
this.Provider = services.BuildServiceProvider();
var host = this.Provider.GetRequiredService<MyHost>();
// blocking operation, the host should be something that keeps the service running
// once it stops, the application stops too as there are no additional
host.Run();
}
private void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IMyService, MyService>();
services.AddSingleton<MyHost>();
...
}
}
What you want to use as host, is up to you. It could be i.e. an Azure Web Task/Host:
using(var jobHost = new JobHost(new JobHostConfiguration(Configuration.GetConnectionString("AzureWebJobsDashboard"))))
{
jobHost.RunAndBlock();
}
which would get started and keep the application running and receiving Azure messages and events registered within it. Or you use some of the other background tasks frameworks like Hangfire.
In ASP.NET Core all of this is handled by the framework, but ASP.NET Core is made for web stuff and hence, depends on an endpoint. Most of the stuff inside Configure method of Startup class used in ASP.NET Core application is about registering middlewares, which work on the HttpContext. You don't have these in a microservice w/o an endpoint. No endpoint, no HttpContext, no need for middlewares.
Of course you also don't have an IApplicationBuilder in an console application, just an IServiceProvider to resolve the services you registered earlier.
来源:https://stackoverflow.com/questions/39604499/what-asp-net-core-project-for-microservice-without-endpoint