不带终结点的MicroService的asp.net Core项目 [英] What asp.net Core project for MicroService without endpoint

查看:97
本文介绍了不带终结点的MicroService的asp.net Core项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑通过微服务框架使Docker陷入困境.我已经看到了无数示例,这些示例暴露了某个端点被某事调用以执行某件事(让我了解某个位置的天气,货币汇率等).我还没有看到有关如何替换当前的Windows Service类型应用程序或订阅队列以完成工作的应用程序的任何讨论.

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.

例如,假设我有一个Windows服务,每天凌晨2:00将所有文件压缩到给定目录中,然后将它们放到另一个目录中(然后发布消息,说明已完成).

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).

我是否构建了一个asp.net核心控制台应用程序"并添加了startup.cs?我是否需要main.start中的startup.cs或只是一种启动方法?

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?

就像我说的那样,很多演示都构建了一个小的Web响应,但是如果我不需要/不需要和终结点,该怎么做就没什么了.

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.

推荐答案

Startup.cs完全是ASP.NET Core特有的,它本身是一个Web堆栈,并由IIS背后的WebListener或Kestrel托管.

Startup.cs is quite ASP.NET Core specific, which by itself is a web stack and comes hosted with WebListener or Kestrel behind IIS.

在控制台应用程序中,虽然可以具有一致的基础,但不会使用传统的Startup.cs,但是看起来却有些不同,因为您可以控制创建IoC容器(而不是ASP) .NET Core在Web应用程序中为您完成此操作).通常,Startup.cs由Program.cs中的WebHostBuilder处理,以允许它在将IServiceCollection传递给ConfigureServices方法之前注入自己的东西.

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>();
        ...
    }
}

您要用作主机的由您决定.可能是Azure Web任务/主机:

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();
}

这将启动并保持应用程序运行,并接收其中已注册的Azure消息和事件.或者,您可以使用其他一些后台任务框架,例如Hangfire.

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.

在ASP.NET Core中,所有这些都是由框架处理的,但是ASP.NET Core是针对Web东西制作的,因此取决于端点. ASP.NET Core应用程序中使用的Startup类的Configure方法内的大多数内容都是关于注册可在HttpContext上运行的中间件的.在没有端点的微服务中没有这些.没有端点,没有HttpContext,不需要中间件.

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.

当然,您在控制台应用程序中也没有IApplicationBuilder,只有一个IServiceProvider可以解决您之前注册的服务.

Of course you also don't have an IApplicationBuilder in an console application, just an IServiceProvider to resolve the services you registered earlier.

这篇关于不带终结点的MicroService的asp.net Core项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆