自托管.NET Core控制台应用程序中的Startup.cs [英] Startup.cs in a self-hosted .NET Core Console Application

查看:775
本文介绍了自托管.NET Core控制台应用程序中的Startup.cs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自托管的 .NET Core控制台应用程序

网络上显示了 ASP.NET Core 的示例,但我没有Web服务器。只是一个简单的命令行应用程序。

The web shows examples for ASP.NET Core but i do not have a webserver. Just a simple command line application.

是否可以对控制台应用程序执行类似的操作?

Is it possible to do something like this for console applications?

public static void Main(string[] args)
{
    // I don't want a WebHostBuilder. Just a command line

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

我想在ASP.NET Core中使用Startup.cs,但在控制台上。

I would like to use a Startup.cs like in ASP.NET Core but on console.

该怎么做?

推荐答案

所有 .NET Core 应用程序由精心设计的独立库和程序包组成,您可以在任何类型的应用程序中自由引用和使用它们。碰巧的是,已经预先配置了 Asp.net核心应用程序以引用很多这些库并公开了http端点。

All .NET Core applications are composed of well-crafted independent libraries and packages which you're free to reference and use in any type of application. It just so happens that an Asp.net core application comes preconfigured to reference a lot of those libraries and exposes an http endpoint.

但是如果您的控制台应用需要依赖注入,则只需引用相应的库即可。这是一个指南: http://andrewlock.net/在网络核心控制台应用程序中使用依赖注入/

But if it's Dependency Injection you need for your console app, simply reference the appropriate library. Here's a guide: http://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/

这篇关于自托管.NET Core控制台应用程序中的Startup.cs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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