在Windows Forms应用程序中托管ASP.NET Core API [英] Hosting ASP.NET Core API in a Windows Forms Application

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

问题描述

背景:我正在开发一个涉及WinForms应用程序的项目.客户端希望公开仅本地的HTTP服务器,以允许其他应用通过REST API(或类似方法)在WinForms应用的运行实例上触发功能.首选是使用ASP.NET Core来实现上述API.

Background: I am working on a project that involves a WinForms app. The client wants to expose a local-only HTTP server to allow other apps to trigger functionality on a running instance of the WinForms app via a REST API (or similar). The preference is to implement the aforementioned API using ASP.NET Core.

因此,我的问题是:如何在同一过程中构造一个同时具有ASP.NET Core API和WinForms GUI的项目?我有什么要提防的陷阱吗?

My question is thus: How do I structure a project to have both an ASP.NET Core API and a WinForms GUI in the same process? Are there any pitfalls I'd have to be wary of?

推荐答案

在Windows Forms应用程序中托管ASP.NET CORE API并与Form交互

这是一个基本的分步示例,说明如何创建一个项目以在Windows Forms应用程序内部托管ASP.NET CORE API并与Form进行一些交互.

Here is a basic step by step example about how to create a project to host ASP.NET CORE API inside a Windows Forms Application and perform some interaction with Form.

要这样做,请按照下列步骤操作:

To do so, follow these steps:

  1. 创建一个Windows Forms应用程序,将其命名为 MyWinFormsApp
  2. 在设计模式下打开 Form1 ,然后在其上放一个 TextBox .
  3. 将设计器中 textBox1 Modifiers 属性更改为 Public 并保存.
  4. 安装 Microsoft.AspNetCore.Mvc 软件包
  5. 安装 Microsoft.AspNetCore 软件包
  6. 在项目的根目录中创建一个 Startup.cs 文件,然后复制以下代码:

  1. Create a Windows Forms Application name it MyWinFormsApp
  2. Open Form1 in design mode and drop a TextBox on it.
  3. Change the Modifiers property of the textBox1 in designer to Public and save it.
  4. Install Microsoft.AspNetCore.Mvc package
  5. Install Microsoft.AspNetCore package
  6. Create a Startup.cs file in the root of the project, and copy the following code:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace MyWinFormsApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMvc();
        }
    }
}

  • 将以下代码复制到 Program.cs 中:

    using System;
    using System.Threading;
    using System.Windows.Forms;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    
    namespace MyWinFormsApp
    {
        public class Program
        {
            public static Form1 MainForm { get; private set; }
    
            [STAThread]
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().RunAsync();
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MainForm = new Form1();
                Application.Run(MainForm);
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();
        }
    }
    

  • 在项目的根目录中创建一个名为 Controllers 的文件夹.

    Controllers 文件夹中创建 ValuesController.cs ,并将以下代码复制到文件:

    Create ValuesController.cs in the Controllers folder and copy the following code to file:

    using System;
    using Microsoft.AspNetCore.Mvc;
    
    namespace MyWinFormsApp.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            [HttpGet]
            public ActionResult<string> Get()
            {
                string text = "";
                Program.MainForm.Invoke(new Action(() =>
                {
                    text = Program.MainForm.textBox1.Text;
                }));
                return text;
            }
    
            [HttpGet("{id}")]
            public ActionResult Get(string id)
            {
                Program.MainForm.Invoke(new Action(() =>
                {
                    Program.MainForm.textBox1.Text = id;
                }));
                return Ok();
            }
        }
    }
    

  • 运行应用程序.

  • Run the application.

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

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