【问题标题】:How to host a REST web api in a .NET core 5 console app如何在 .NET Core 5 控制台应用程序中托管 REST Web api
【发布时间】:2021-07-02 12:04:00
【问题描述】:

我无法让 .NET Core 5 控制台应用托管 Web Api。我的猜测是我可能会以错误的方式进行操作,但我在谷歌上找不到任何特定于 .NET Core 控制台应用程序的解决方案。所以,我很茫然。

目前,我一直在尝试使用 Microsoft.AspNet.WebApi.OwinSelfHost 包,以便不依赖 IIS 或任何其他容器。这个很重要。它必须是一个控制台应用程序。无论如何,我已经按照所有示例进行了操作,但它无法正常工作。

我的症状:

它运行并且启动代码成功执行,但是当我使用客户端向 Api 发出请求时,它只是静止不动并永远旋转。它连接到端口,仅此而已。

我的密码:

首先,我的 Startup 类:

public class StartupApi
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration( IAppBuilder appBuilder )
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        appBuilder.UseWebApi( config );
    }
}

现在我的程序代码:

class Program
{
    //private static readonly Startup _startup = new Startup();

    static int Main( string[] args )
    {
        string baseAddress = "http://localhost:80/";

        using (WebApp.Start<StartupApi>(url: baseAddress))
        {
            Console.WriteLine("Api Started");
            Console.ReadLine();
        }

        return 0;
    }
}

现在是我的控制器:

public class ProductsController : ApiController
{
    Product[] products = new Product[]
    {
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
    };

    public IEnumerable<Product> GetProducts()
    {
        return products;
    }

    public Product GetProductById( int id )
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
            throw new HttpResponseException(HttpStatusCode.NotFound);
        
        return product;
    }

    public IEnumerable<Product> GetProductsByCategory(string category)
    {
        return products.Where( p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
    }
}

我已经使用网络浏览器和 Postman 来测试请求,但没有。

这个包是否可能与 .NET Core 不完全兼容? .NET Core 是否有更好的解决方案?我读过的所有文章都集中在 .NET Framework 上。

【问题讨论】:

  • “Console.ReadLine”会不会有问题?它可能无限期地等待输入。我不知道它是否正确使用,我只是不记得看到它以这种方式使用。
  • 取自网上的一个例子。 WebApp 仅在 using 块内运行。离开使用,web api 将关闭。所以,没关系。事实上,我确实开始使用 Thread.Sleep 让它无限期地休眠。同样的事情也发生了。
  • 疯狂的是,这与网络上的各种示例一模一样。我完全没有偏离。但是这些示例中的每一个都从未说过它是 .NET Core,所以这让我感到奇怪。
  • 另一方面,基于约定的路由如今是如此的 web api 1。较新版本的推荐路由方案是 Attribute based routing 我不确定,但基于约定的路由对于 .Net5 甚至可能已经过时
  • 我想知道这是否是问题的核心。客户端访问 api,但永远不会调用端点。也许 .NET Core 没有处理基于约定的路由。

标签: c# .net-core console-application webapi


【解决方案1】:

我的问题已经解决了!

我使用的 Nuget 包显然主要用于 .NET Framework,我无法让它与 .NET Core 5 一起使用。

相反,我使用了 Kestrel,因为它没有任何 IIS 或容器要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多