实现 WebApi 自托管服务宿主于 WinForms 及其交互
2019-12-31 00:18
1551 查看
实现 WebApi 自托管服务宿主于 WinForms 及其交互
在 Windows 平台 Web 服务一般托管于 IIS. 在开发中, 会遇到 WinForms 或 WPF 服务端程序需要提供对外 API 作为服务. 在本文详细介绍 WebApi 自托管于 WinForms 中, WPF 或 Console 程序实现类似.
0. 完整示例演示
1. 新建解决方案以及 WinForms 工程
1.1 新建解决方案及工程
如下图所示:
1.2 拖拽控件
绘制必要控件, 布局如下:
备注: 绘制一个 NumericUpDown 和两个 Button 控件.
2. 开发 HTTP 服务类
/// <summary> /// HTTP service. /// </summary> public class HttpService { /// <summary> /// HTTP self hosting. /// </summary> private HttpSelfHostServer _server; #region HTTP Service /// <summary> /// start HTTP server. /// </summary> /// <param name="port">the port of the http server</param> public void StartHttpServer(string port) { var config = new HttpSelfHostConfiguration($"http://0.0.0.0:{port}"); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}"); this._server = new HttpSelfHostServer(config); this._server.OpenAsync().Wait(); } /// <summary> /// Close HTTP server. /// </summary> public void CloseHttpServer() { this._server.CloseAsync().Wait(); this._server.Dispose(); } #endregion }
WebApi 自托管服务主要由
HttpSelfHostServer实现.
config.MapHttpAttributeRoutes();
可以在
Controller中使用路由特性.
3. 调用 HTTP 服务
在 MainForm 窗体程序中引用 HTTP 服务:
public class MainForm:Form { /// <summary> /// Http service. /// </summary> private readonly HttpService _http; public MainForm() { /** * initialize http service. */ _http = new HttpService(); } }
3.1 编写开启 HTTP 服务代码
/// <summary> /// start the http server. /// </summary> private void StartButton_Click(object sender, EventArgs e) { /** * start. */ try { var port = this.PortNum.Value; _http.StartHttpServer($"{port}"); } catch (Exception exception) { MessageBox.Show($"{exception.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
3.2 编写关闭 HTTP 服务代码
/// <summary> /// close the http server. /// </summary> private void CloseButton_Click(object sender, EventArgs e) { /** * close. */ try { _http.CloseHttpServer(); } catch (Exception exception) { MessageBox.Show($"{exception.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
4. 开发控制器
/// <summary> /// Home controller. /// </summary> [RoutePrefix("api/home")] public class HomeController : ApiController { /// <summary> /// Print the greetings /// </summary> /// <param name="name">visitor</param> /// <returns>greetings</returns> [Route("echo")] [HttpGet] public IHttpActionResult Echo(string name) { return Json(new {Name = name, Message = $"Hello, {name}"}); } }
5. 合在一起
相关文章推荐
- asp.net web api的自托管模式HttpSelfHostServer可以以控制台程序或windows服务程序为宿主,不单单依赖于IIS web服务器
- C# 通过服务启动窗体(把窗体添加到服务里)实现用户交互的windows服务
- 用ServerSocket类与Socket类实现客户端与服务端的交互
- WCF+EF 实战系列三 WCF服务的实现与托管
- WCF宿主与服务托管
- WCF服务怎样与宿主程序交互
- 讨论创建基于WSE的报表打印服务及其实现
- 微服务--webapi实现,脱离iis,脱离tomcat
- 声明式服务调用的实现(Feign)及其优化(Ribbon)
- 设置Windows服务允许进行桌面交互,实现屏幕监控
- 讨论创建基于WSE的报表打印服务及其实现
- C# 通过服务启动窗体(把窗体添加到服务里)实现用户交互的windows服务[转发]
- nodeJS的原生http服务利用插件实现前后端交互
- .NET实现可交互的WINDOWS服务介绍
- 运用Smark.SocketAsyncs方便实现数据交互服务
- 浅谈delphi创建Windows服务程序与窗体实现交互
- WCF宿主与服务托管
- 讨论创建基于WSE的报表打印服务及其实现
- 在系统服务中与桌面进行交互编程实现
- 爱思华宝邮件服务器协同工作服务 功能介绍及其实现方法