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

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

问题描述

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

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

The web shows examples for ASP.NET Core but I do not have a web server. 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 core 应用程序预先配置为引用许多这些库并公开一个 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.

但如果您的控制台应用程序需要依赖注入,只需引用适当的库即可.这是一个指南:https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/

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

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

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