如何从 WPF 应用程序提供 RESTful Web 服务?

如何从 WPF 应用程序提供 RESTful Web 服务?

问题描述:

通常,WPF 应用程序是 Web 服务器上 RESTful 服务的使用者/客户端.我想让它反转 - WPF 应用程序应该能够公开 Web API.这将由网络应用使用.

Typically a WPF application is a consumer/client of a RESTful service(s) on a web server. I would like to have it reversed - WPF application should be able to expose an web API. This would be consumed by an web app.

流程:

web app ---sends a command to--> WPF app
** WPF app makes a 'long running job' until its user decides to stop **
WPF app ---passes data back to--> web app

通信应为 Json 格式.我已经准备好了 OpenAPI(在 YAML) 架构在 http://editor.swagger.io/一>.将来,它可用于更改 WPF 应用程序的 Web API.

The communication should be in Json format. I have prepared OpenAPI (in YAML) schema for it at the http://editor.swagger.io/. In the future it could be used to make changes to the WPF app's web API.

它允许生成 ASP.NET Core 服务器 c# 代码存根.在 WPF 中运行 ASP.NET Core 服务器有什么要求,并且重量足够轻以供使用?

It allows to generate ASP.NET Core server c# code stub. What would be the requirements to run ASP.NET Core server in WPF and weither it would be light weight enough for use?

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build();

// somewhere in the WPF app: BuildWebHost(args).Run();

代码由https://github.com/swagger-api/自动生成swagger-codegen.

有一个 post 未能将 ASP.NET Core 2.x 集成到 WPF 应用程序中.遗憾的是,ASP.NET Core 3.0 及更高版本只能在 .NET Core 上运行.

There is a post which failed to integrate ASP.NET Core 2.x into WPF application. Unfortunatelly, ASP.NET Core 3.0 and later will only run on .NET Core.

我在这里和那里有一些东西,但不是一个可行的概念.我的选择可能是:

I have some bits here and there but not a working concept. My options could be:

  • 使用第 3 方框架或库.应该能够使用 OpenAPI/YAML 模式或 Swagger 生成的服务器存根代码等.
    • ServiceStack 可能是这里缺失的部分吗?

    如何在 WPF 端实现 Web 服务器/服务?也许有一个现有的 3rd 方框架可以让我免于重新发明*?

    附注.有一个类似的问题如何在 WPF 项目中公开一个宁静的服务,但它已经过时了.

    PS. There is a similar question how to expose a restful service inside a WPF project but it is outdated.

这听起来像您的要求:

WPF 应用程序应该能够公开 Web API.这将由网络应用使用.

WPF application should be able to expose an web API. This would be consumed by an web app.

但是您反对唯一可以实现的解决方案:

But then you're against the only solution that would make it possible:

使用来自 WPF 应用程序的 Web 服务启动 Web 服务器.听起来很糟糕.

Launch a web server with web services from the WPF application. Sounds bad.

我不知道您还期望如何在不启动 Web 服务器的情况下公开 Web API?在 UI 应用中,您希望启动自托管服务.

I'm not sure how else you're expecting being able to expose a Web API without launching a web server? Inside a UI App you'd want to launch a self-hosted Service.

在 ServiceStack 中,您可以在 WPF 应用程序中启动一个 自托管服务要么是自托管的 .NET Framework HTTP 侦听器,要么是 .NET Framework 上的 ASP.NET Core 应用程序.微软表示将无限期支持这两个选项,但 .NET Framework 正在逐步淘汰,ASP.NET Core 3.0 只能在 .NET Core 上运行,.NET Framework 停止在 v4.x 上开发,因为 .NET 5 将成为 .NET Core 的下一个版本.

In ServiceStack you can just start a self-hosted Service in your WPF App which can either be a self-hosted .NET Framework HTTP Listener or an ASP.NET Core on .NET Framework App. Both options Microsoft have said are going to be supported indefinitely, but .NET Framework is being phased out with ASP.NET Core 3.0 only going to run on .NET Core and .NET Framework stopping development at v4.x as .NET 5 is just going to be the next version of .NET Core.

但这不会改变您现在可用的解决方案,如果您需要在 WPF .NET Framework 应用程序中运行 Web 服务,您将需要运行自托管的 .NET Framework HTTP 服务器,它可以是自托管的 HTTP 侦听器或 ASP.NET Core(在 .NET FX 上)应用.

But that shouldn't change what solutions are available to you now, if you need to run a Web Service in a WPF .NET Framework App you'll need to run a self-hosted .NET Framework HTTP Server which can either be a self-hosted HTTP Listener or ASP.NET Core (on .NET FX) App.