托管可选 Web API 的 Dotnet Core 3.1 控制台应用程序用于控制 [英] Dotnet Core 3.1 console app hosting optional Web API for control

查看:26
本文介绍了托管可选 Web API 的 Dotnet Core 3.1 控制台应用程序用于控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Dotnet Core 3.1 编写控制台应用程序.它已配置为使用 Microsoft.Extensions.DependencyInjection 以下列方式使用依赖项注入:

I'm writing a console app in Dotnet Core 3.1. It is already configured to use dependency injection using Microsoft.Extensions.DependencyInjection in the following way:

public static class Program
{
  public static IServiceProvider ServiceProvider { get; private set; }

  public static int Main(string[] args)
  {
    // ...
    ServiceProvider = ConfigureServices().BuildServiceProvider();
    // ...
  }

  public static IServiceCollection ConfigureServices()
  {
    return new ServiceCollection()
      .AddLogging(cfg =>
      {
        // ...
      }
      // ...
  }
}

我正在尝试设置一个简单的 HTTP API 来提供对应用程序的一些基本控制.我想避免 ASP.Net MVC 或任何太重的东西.我只需要能够发出简单的指令并获得基本状态.一切都将是 JSON - 不需要 Razor 或类似的东西.

I'm trying to set up a simple HTTP API to provide some basic control of the app. I'd like to avoid ASP.Net MVC or anything too heavy. I just need to be able to issue simple instructions and get basic status. It will all be JSON - no need for Razor or anything like that.

我还有两门(未完成的)课程:

I have another two (unfinished) classes:

public class ApiRunner
{
  public IWebHost WebHost { get; }

  public ApiRunner()
  {
    WebHost = new WebHostBuilder()
      .UseKestrel()
      .UseUrls("http://*:5000")
      .UseStartup<ApiStartup>()
      .Build();
  }

  public void Start()
  {
    Task.Run(() => WebHost.Run());
  }

  public void Stop()
  {
    WebHost.StopAsync();
  }
}

public class ApiStartup
{
  public void Configure(IApplicationBuilder app)
  {
    app.UseRouter(r =>
    {
      r.MapGet("/", async (request, response, routeData) =>
      {
        response.Headers["content-type"] = "text/plan";
        response.WriteAsync("Hello World!");
      });
    }
  }
}

除非我添加到我的 ApiStartup 类中,否则上述方法不起作用:

The above does not work unless I add to my ApiStartup class:

public void ConfigureServices(IServiceCollection services)
{
  services.AddRouting();
}

但这似乎有两个 DI 堆栈在彼此之上运行:一个用于主程序,另一个用于 API.我确实尝试将 services.AddRouting(); 添加到 Program.cs 中的主要 DI 配置,但是 (1) 没有用 - 我遇到了同样的异常当我根本没有它时,这让我相信 API 想要使用自己的 DI,并且 (2) 我不一定想用我看到的特定于 API 的服务污染我的主 DI作为一个有点独立的模块.

but this seems like there are two DI stacks running on top of one another: one for the main program, and one for the API. I did try to add services.AddRouting(); to the main DI configuration in Program.cs, but (1) that didn't work - I got the same exception as when I didn't have it at all, leading me to believe that the API is wanting to use its own DI, and (2) I don't necessarily want to pollute my main DI with an API-specific service that I see as a somewhat separate module.

我只需要一个在我的控制台应用程序中运行的轻量级 HTTP 服务器,它允许我发出简单的命令并获取状态.我可以请一些指示我如何实现这一目标吗?谢谢.

All I need is a lightweight HTTP server running in my console app that allows me to issue simple commands and get status. Can I please have some pointers how I can achieve this? Thank you.

推荐答案

首先,每个 ASP.NET Core 应用程序都是一个控制台应用程序,只有注册了 DI 和相关服务才成为一个 Web 应用程序.

First, every ASP.NET Core app is a console app and only becomes a web app with DI and relvant services registered.

其次,您没有遵循服务注册的标准模式;无需自己实例化服务集合,WebHostBuilder 已经先完成了.只在 ApiStartup 类中注册服务.所以是的,您在两个地方注册.请参阅具有日志记录配置演示的附加好处的示例:

Second, you are not following the standard pattern for the service registration; there is no need to instantiate a service collection yourself, the WebHostBuilder already does it first. Only register services in the ApiStartup class. So yes, you are registering in two places. See example with the added benefit of logging config demo:

https://github.com/akovac35/Logging.Samples/tree/master/WebApp

这篇关于托管可选 Web API 的 Dotnet Core 3.1 控制台应用程序用于控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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