实例介绍
【实例截图】

【源码目录】
WebApiHostingOld
├── WebApiHosting
│ ├── App.config
│ ├── ConfigureForm.Designer.cs
│ ├── ConfigureForm.cs
│ ├── ConfigureForm.resx
│ ├── Controllers
│ │ └── HomeController.cs
│ ├── MainForm.Designer.cs
│ ├── MainForm.cs
│ ├── MainForm.resx
│ ├── Models
│ │ ├── Data.cs
│ │ └── DataRecord.cs
│ ├── Program.cs
│ ├── Properties
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ ├── Resources.resx
│ │ ├── Settings.Designer.cs
│ │ └── Settings.settings
│ ├── Services
│ │ └── HttpService.cs
│ ├── View.ico
│ ├── WebApiHosting.csproj
│ ├── WebApiHosting.csproj.user
│ ├── bin
│ │ ├── Debug
│ │ │ ├── Newtonsoft.Json.dll
│ │ │ ├── Newtonsoft.Json.xml
│ │ │ ├── System.Net.Http.Formatting.dll
│ │ │ ├── System.Net.Http.Formatting.xml
│ │ │ ├── System.Web.Http.SelfHost.dll
│ │ │ ├── System.Web.Http.SelfHost.xml
│ │ │ ├── System.Web.Http.dll
│ │ │ ├── System.Web.Http.xml
│ │ │ ├── WebApiHosting.exe
│ │ │ ├── WebApiHosting.exe.config
│ │ │ └── WebApiHosting.pdb
│ │ └── Release
│ ├── obj
│ │ └── Debug
│ │ ├── DesignTimeResolveAssemblyReferences.cache
│ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ ├── TempPE
│ │ ├── WebApiHosting.ConfigureForm.resources
│ │ ├── WebApiHosting.MainForm.resources
│ │ ├── WebApiHosting.Properties.Resources.resources
│ │ ├── WebApiHosting.csproj.CopyComplete
│ │ ├── WebApiHosting.csproj.CoreCompileInputs.cache
│ │ ├── WebApiHosting.csproj.FileListAbsolute.txt
│ │ ├── WebApiHosting.csproj.GenerateResource.cache
│ │ ├── WebApiHosting.exe
│ │ └── WebApiHosting.pdb
│ └── packages.config
├── WebApiHosting.sln
└── packages
├── Microsoft.AspNet.WebApi.Client.5.2.7
│ ├── Microsoft.AspNet.WebApi.Client.5.2.7.nupkg
│ └── lib
│ ├── net45
│ │ ├── System.Net.Http.Formatting.dll
│ │ └── System.Net.Http.Formatting.xml
│ ├── netstandard2.0
│ │ ├── System.Net.Http.Formatting.dll
│ │ └── System.Net.Http.Formatting.xml
│ └── portable-wp8 netcore45 net45 wp81 wpa81
│ ├── System.Net.Http.Formatting.dll
│ └── System.Net.Http.Formatting.xml
├── Microsoft.AspNet.WebApi.Core.5.2.7
│ ├── Content
│ │ └── web.config.transform
│ ├── Microsoft.AspNet.WebApi.Core.5.2.7.nupkg
│ └── lib
│ └── net45
│ ├── System.Web.Http.dll
│ └── System.Web.Http.xml
├── Microsoft.AspNet.WebApi.SelfHost.5.2.7
│ ├── Microsoft.AspNet.WebApi.SelfHost.5.2.7.nupkg
│ └── lib
│ └── net45
│ ├── System.Web.Http.SelfHost.dll
│ └── System.Web.Http.SelfHost.xml
└── Newtonsoft.Json.6.0.4
├── Newtonsoft.Json.6.0.4.nupkg
├── lib
│ ├── net20
│ │ ├── Newtonsoft.Json.dll
│ │ └── Newtonsoft.Json.xml
│ ├── net35
│ │ ├── Newtonsoft.Json.dll
│ │ └── Newtonsoft.Json.xml
│ ├── net40
│ │ ├── Newtonsoft.Json.dll
│ │ └── Newtonsoft.Json.xml
│ ├── net45
│ │ ├── Newtonsoft.Json.dll
│ │ └── Newtonsoft.Json.xml
│ ├── netcore45
│ │ ├── Newtonsoft.Json.dll
│ │ └── Newtonsoft.Json.xml
│ ├── portable-net40 sl5 wp80 win8 wpa81
│ │ ├── Newtonsoft.Json.dll
│ │ └── Newtonsoft.Json.xml
│ └── portable-net45 wp80 win8 wpa81
│ ├── Newtonsoft.Json.dll
│ └── Newtonsoft.Json.xml
└── tools
└── install.ps1
34 directories, 74 files
【核心代码】
using System;
using System.Linq;
using System.ServiceModel;
using System.Threading;
using System.Windows.Forms;
using WebApiHosting.Services;
namespace WebApiHosting
{
public partial class MainForm : Form
{
/// <summary>
/// Http服务
/// </summary>
private HttpService _http;
public static MainForm mainForm;
public MainForm()
{
InitializeComponent();
mainForm = this;
/**
* 启用“开始”按钮并禁用“关闭”按钮
*/
this.StartButton.Enabled = true;
this.CloseButton.Enabled = false;
}
/// <summary>
/// 启动http服务
/// </summary>
private async void StartButton_Click(object sender, EventArgs e)
{
/**
* 启动.
*/
try
{
var port = Convert.ToInt32(this.PortNum.Value);
/**
* 初始化http服务
*/
_http = new HttpService(port);
await _http.StartHttpServer();
MessageBox.Show("启动成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
/**
* 禁用“开始”按钮并启用“关闭”按钮.
*/
this.StartButton.Enabled = false;
this.PortNum.Enabled = false;
this.CloseButton.Enabled = true;
}
catch (AggregateException exception)
{
/**
* 提示以管理员身份运行.
*/
if (exception.Flatten().InnerExceptions.Any(ex => ex is AddressAccessDeniedException))
{
MessageBox.Show("权限被拒绝,请使用管理员权限启动。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exception)
{
MessageBox.Show($"{exception.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 关闭http服务器
/// </summary>
private async void CloseButton_Click(object sender, EventArgs e)
{
/**
* close.
*/
try
{
await _http.CloseHttpServer();
_http.Dispose();
MessageBox.Show("关闭成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
/**
* 禁用“关闭”按钮并启用“开始”按钮.
*/
this.StartButton.Enabled = true;
this.PortNum.Enabled = true;
this.CloseButton.Enabled = false;
}
catch (Exception exception)
{
MessageBox.Show($"{exception.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void SetResult(object text)
{
TB_Data.Text = text.ToString();
}
private void ConfigureButton_Click(object sender, EventArgs e)
{
ConfigureForm configureForm = new ConfigureForm();
configureForm.ShowDialog();
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)