另外一篇文章
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
)文章来源:https://www.toymoban.com/news/detail-444953.html
同系列
Kestrel封装在WindowService中(.net5,.net6,.net7三个版本的介绍)文章来源地址https://www.toymoban.com/news/detail-444953.html
到了这里,关于Kestrel封装在Winform中的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!