Kestrel封装在Winform中

news/2024/11/15 1:58:12/

Kestrel封装在Winform中

  • 背景
  • 思路
    • 方法1
    • 方法2
    • 方法3(本文使用的方法)
  • 实现
    • 在winform程序中引入几个nuget包
    • 新建一个Startup类(叫什么名字都行)
    • 修改Program文件
    • 创建controller
  • 运行效果(打开浏览器,输入如下地址)
    • 修改地址,调用get方法引起winform变动
  • winform中的message方法
  • 关于配置端口和地址请查看文章
  • 完整代码下载
  • 同系列

另外一篇文章
Kestrel封装在WindowService中(.net5,.net6,.net7三个版本的介绍)

背景

在很久以前为了满足需求,已经开发了一款winform程序,并且是4.6.1版本的,如今为了和第三方对接,需要在这个winform上提供WebAPI的接口。因为第三方的程序是一份没有源码的程序。

思路

方法1

  • 网上有很多自写web服务的功能,个人觉得过于麻烦,而且还要考虑一些路由规则什么的,太难了

方法2

  • 使用iis或者nginx提供服务,一个web程序和一个winform程序实现通信,需要做两个程序,还要交互,麻烦

方法3(本文使用的方法)

考虑4.6.1 版本刚好支持了netcore,所以可以将kestrel服务集成到winform中,直接提供webapi服务

实现

在winform程序中引入几个nuget包

Microsoft.AspNetCore  Version="2.1.7"
Microsoft.AspNetCore.Mvc  Version="2.1.3"

在使用nuget引入的时候会将关联的全部引入进来,完整的packages.config文件如下

<?xml version="1.0" encoding="utf-8"?>
<packages><package id="Microsoft.AspNetCore" version="2.1.7" targetFramework="net461" /><package id="Microsoft.AspNetCore.Antiforgery" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Authentication.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Authentication.Core" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Authorization" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Authorization.Policy" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Connections.Abstractions" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Cors" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Cryptography.Internal" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.DataProtection" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.DataProtection.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Diagnostics" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Diagnostics.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.HostFiltering" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Hosting" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Hosting.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Hosting.Server.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Html.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Http" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Http.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Http.Extensions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Http.Features" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.HttpOverrides" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.HttpsPolicy" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.JsonPatch" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Localization" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Abstractions" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.ApiExplorer" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Core" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Cors" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.DataAnnotations" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Formatters.Json" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Localization" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Razor" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.Razor.Extensions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.RazorPages" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.TagHelpers" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Mvc.ViewFeatures" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Razor" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Razor.Design" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Razor.Language" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Razor.Runtime" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.ResponseCaching.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Routing" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Routing.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.AspNetCore.Server.IISIntegration" version="2.1.7" targetFramework="net461" /><package id="Microsoft.AspNetCore.Server.Kestrel" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Server.Kestrel.Core" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Server.Kestrel.Https" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" version="2.1.3" targetFramework="net461" /><package id="Microsoft.AspNetCore.WebUtilities" version="2.1.1" targetFramework="net461" /><package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" /><package id="Microsoft.CodeAnalysis.Common" version="2.8.0" targetFramework="net461" /><package id="Microsoft.CodeAnalysis.CSharp" version="2.8.0" targetFramework="net461" /><package id="Microsoft.CodeAnalysis.Razor" version="2.1.1" targetFramework="net461" /><package id="Microsoft.CSharp" version="4.5.0" targetFramework="net461" /><package id="Microsoft.DiaSymReader.Native" version="1.7.0" targetFramework="net461" /><package id="Microsoft.DotNet.PlatformAbstractions" version="2.1.0" targetFramework="net461" /><package id="Microsoft.Extensions.Caching.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Caching.Memory" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.Binder" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.CommandLine" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.EnvironmentVariables" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.FileExtensions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.Json" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Configuration.UserSecrets" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.DependencyInjection" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.DependencyModel" version="2.1.0" targetFramework="net461" /><package id="Microsoft.Extensions.FileProviders.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.FileProviders.Composite" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.FileProviders.Physical" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.FileSystemGlobbing" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Hosting.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Localization" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Localization.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Logging" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Logging.Abstractions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Logging.Configuration" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Logging.Console" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Logging.Debug" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.ObjectPool" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Options" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Options.ConfigurationExtensions" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.Primitives" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Extensions.WebEncoders" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Net.Http.Headers" version="2.1.1" targetFramework="net461" /><package id="Microsoft.Win32.Registry" version="4.5.0" targetFramework="net461" /><package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" /><package id="Newtonsoft.Json.Bson" version="1.0.1" targetFramework="net461" /><package id="System.AppContext" version="4.3.0" targetFramework="net461" /><package id="System.Buffers" version="4.5.0" targetFramework="net461" /><package id="System.Collections" version="4.3.0" targetFramework="net461" /><package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net461" /><package id="System.Collections.Immutable" version="1.5.0" targetFramework="net461" /><package id="System.ComponentModel.Annotations" version="4.5.0" targetFramework="net461" /><package id="System.Console" version="4.3.0" targetFramework="net461" /><package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net461" /><package id="System.Diagnostics.DiagnosticSource" version="4.5.1" targetFramework="net461" /><package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net461" /><package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net461" /><package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net461" /><package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net461" /><package id="System.Globalization" version="4.3.0" targetFramework="net461" /><package id="System.IO.Compression" version="4.3.0" targetFramework="net461" /><package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" /><package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" /><package id="System.IO.Pipelines" version="4.5.3" targetFramework="net461" /><package id="System.Linq" version="4.3.0" targetFramework="net461" /><package id="System.Linq.Expressions" version="4.3.0" targetFramework="net461" /><package id="System.Memory" version="4.5.2" targetFramework="net461" /><package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" /><package id="System.Reflection" version="4.3.0" targetFramework="net461" /><package id="System.Reflection.Metadata" version="1.6.0" targetFramework="net461" /><package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net461" /><package id="System.Runtime" version="4.3.0" targetFramework="net461" /><package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net461" /><package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net461" /><package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net461" /><package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" /><package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net461" /><package id="System.Security.AccessControl" version="4.5.0" targetFramework="net461" /><package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" /><package id="System.Security.Cryptography.Cng" version="4.5.0" targetFramework="net461" /><package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" /><package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" /><package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" /><package id="System.Security.Cryptography.Xml" version="4.5.0" targetFramework="net461" /><package id="System.Security.Permissions" version="4.5.0" targetFramework="net461" /><package id="System.Security.Principal.Windows" version="4.5.1" targetFramework="net461" /><package id="System.Text.Encoding" version="4.3.0" targetFramework="net461" /><package id="System.Text.Encoding.CodePages" version="4.3.0" targetFramework="net461" /><package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net461" /><package id="System.Text.Encodings.Web" version="4.5.0" targetFramework="net461" /><package id="System.Threading" version="4.3.0" targetFramework="net461" /><package id="System.Threading.Tasks" version="4.3.0" targetFramework="net461" /><package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net461" /><package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net461" /><package id="System.Threading.Thread" version="4.3.0" targetFramework="net461" /><package id="System.ValueTuple" version="4.3.0" targetFramework="net461" /><package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net461" /><package id="System.Xml.XDocument" version="4.3.0" targetFramework="net461" /><package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net461" /><package id="System.Xml.XPath" version="4.3.0" targetFramework="net461" /><package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net461" />
</packages>

新建一个Startup类(叫什么名字都行)

代码如下

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace 测试一个winform
{public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseHsts();}app.UseHttpsRedirection();app.UseMvc();}}
}

修改Program文件

增加一个启动web的方法,并且在main函数中将方法启动
为了在后面能够调用form1,所以我还将form1的对象改成了静态的,代码如下

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 测试一个winform
{internal static class Program{//声明一个静态的public static Form1 mainForm;/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){//启动Task.Run(() => { CreateWebHostBuilder(new string[] { }).Build().Run(); }); Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);mainForm = new Form1();Application.Run(mainForm);}//新增加的代码public static IWebHostBuilder CreateWebHostBuilder(string[] args){return WebHost.CreateDefaultBuilder(args).UseUrls("http://0.0.0.0:7001", "https://0.0.0.0:7002").UseStartup<Startup>();}}
}

创建controller

文件结构如下
在这里插入图片描述

代码内容如下

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using 测试一个winform;namespace 测试一个winform集成web.Controllers
{[Route("api/[controller]")][ApiController]public class ValuesController : ControllerBase{// GET api/values[HttpGet]public ActionResult<IEnumerable<string>> Get(){return new string[] { "value1", "value2" };}// GET api/values/5[HttpGet("{id}")]public ActionResult<string> Get(int id){Program.mainForm.Message(id.ToString());return "value";} }
}

运行效果(打开浏览器,输入如下地址)

在这里插入图片描述

修改地址,调用get方法引起winform变动

在这里插入图片描述

winform中的message方法

public void Message(string message){//MessageBox.Show(message);this.Invoke(new Action(() => { button1.Text = message; }));}

关于配置端口和地址请查看文章

https://blog.csdn.net/iml6yu/article/details/100692488

https://blog.csdn.net/iml6yu/article/details/100692488

完整代码下载

https://download.csdn.net/download/iml6yu/87726309

如果无法下载可以 QQ和我说 (复制下面给内容到浏览器地址栏,按下回车 tencent://message/?uin=646007589&Site=&Menu=yes

同系列

Kestrel封装在WindowService中(.net5,.net6,.net7三个版本的介绍)


http://www.ppmy.cn/news/56723.html

相关文章

新老stp的配置和安全总结部分

老stp只有根桥没有备份桥 老stp的五种接口状态&#xff1a; disable 接口down没开stp blocking 阻塞 listening 发bpdu&#xff0c;比较bpdu优劣 leraning 开始学习mac地址表 forwardding 转发 老stp直接拓扑变化30秒&#xff0c;间接拓扑变化50秒 RSTP只有3种端口状态&#…

ArduPilot之开源代码LibrarySketches设计

ArduPilot之开源代码Library&Sketches设计 1. 简介1.1 Core libraries1.2 Sensor libraries1.3 Other libraries 2. 源由3. Library Sketches设计3.1 设计框架3.2 Example Sketches3.3 AP_Common Sketches3.3.1 配置sitl环境3.3.2 编译AP_Common3.3.3 运行AP_Common3.3.4 代…

四和能聚分析做直播带货的商家通常发布什么类型的短视频

大家应该都知道&#xff0c;短视频能够为直播间进行预热、引流&#xff0c;短视频做得好&#xff0c;能够积累到大量粉丝&#xff0c;也能够为直播间获取到更多流量&#xff0c;帮助直播间获取到更多流量和转化。做直播带货的商家发布的短视频和以积累粉丝为主的运营者内容上有…

python模块与包

文章目录 模块自定义模块及导入 模块 1.什么是模块? 模块就是一个Python代码文件&#xff0c;内含类、函数、变量等&#xff0c;我们可以导入进行使用。 2.如何导入模块 [from 模块名]import [模块 类变量 函数] [as 别名] 3.注意事项 from可以省略&#xff0c;直接import即可…

【Python入门篇】——PyCharm的基础使用

作者简介&#xff1a; 辭七七&#xff0c;目前大一&#xff0c;正在学习C/C&#xff0c;Java&#xff0c;Python等 作者主页&#xff1a; 七七的个人主页 文章收录专栏&#xff1a; Python入门&#xff0c;本专栏主要内容为Python的基础语法&#xff0c;Python中的选择循环语句…

mulesoft MCIA 破釜沉舟备考 2023.04.29.28 (易错题)

mulesoft MCIA 破釜沉舟备考 2023.04.29.28 (易错题) 1. According to MuleSoft, what Action should an IT organization take regarding its technology assets in order to close the IT delivery.2. According to MuleSofts IT delivery and operating model, which appr…

ES6 基础复习

变量声明 变量可能具有的特性 块级作用域&#xff0c;变量提升&#xff08;变量声明之前使用该变量&#xff09;&#xff0c;全局属性&#xff0c;重复声明&#xff0c;暂时性死区&#xff08;变量声明之前的区域无法使用该变量&#xff09;&#xff0c;初始值设置&#xff0…

老外从神话原型中提取的12个品牌个性

老外从神话原型中提取的12个品牌个性 也是西方视角&#xff0c;需要本土化 参照心理学大师荣格的理论&#xff1a;心理学潜意识派 趣讲大白话&#xff1a;品牌的调调是啥 【趣讲信息科技151期】 **************************** 12种原型又归属于4种人性动机。 1、稳定&#xff0…