优雅的在WinForm/WPF/控制台 中使用特性封装WebApi

说明

在C/S端作为Server,建立HTTP请求,方便快捷。

1.使用到的类库

Newtonsoft.dll

2.封装 HttpListener

HttpApi类
    public class HttpApi
{ private static List<HttpListener> HttpListenerList = new List<HttpListener>(); /// <summary>
/// 初始化服务
/// </summary>
public static void InitService()
{ //获取程序集下面的所有的类
Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
foreach (Type item_type in types)
{
//定义是否有类的特性标识
bool IsHasFormApi = false;
//取类上的自定义特性
object[] objs = item_type.GetCustomAttributes(typeof(FromApi), true);
foreach (object obj in objs)
{
FromApi fromApi = obj as FromApi;
if (fromApi != null)
{
IsHasFormApi = true;
break;
}
}
if (IsHasFormApi)
{
// 获取完全名称
String className = item_type.FullName; // 根据命名空间反射类的Type
Type type = Type.GetType(className);
object objInstance = type.Assembly.CreateInstance(className); // 获取所有的方法
MethodInfo[] info = type.GetMethods(); // 遍历所有的方法
foreach (MethodInfo item in info)
{ // 获取Http请求方法
HttpMethod httpMethod = item.GetCustomAttribute<HttpMethod>();
// 获取Action
ActionName actionName = item.GetCustomAttribute<ActionName>(); // 判断有没有特性
if (httpMethod != null || actionName != null)
{
HttpListener listerner = new HttpListener();
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
string url = "http://127.0.0.1:8011";
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["httpserver"]))
{
url = "http://" + ConfigurationManager.AppSettings["httpserver"];
}
listerner.Prefixes.Add(url + actionName.URL + "/"); //开启服务
if (!listerner.IsListening)
{
listerner.Start();
AsyncCallback ac = new AsyncCallback(GetContextAsyncCallback);
CallbackObject callback = new CallbackObject() { Listerner = listerner, MethodItem = item, ClassInstance = objInstance, HttpMethod = httpMethod.method };
listerner.BeginGetContext(ac, callback);
HttpListenerList.Add(listerner);
}
} }
}
} } /// <summary>
/// 收到监听请求回调
/// </summary>
/// <param name="ia"></param>
private static void GetContextAsyncCallback(IAsyncResult ia)
{
CallbackObject state = ia.AsyncState as CallbackObject;
if (ia.IsCompleted)
{
HttpListenerContext ctx = state.Listerner.EndGetContext(ia); var request = ctx.Request; HttpListenerResponse response = ctx.Response; try
{ //判断 请求 方式
if (request.HttpMethod.ToUpper() == state.HttpMethod.ToString().ToUpper() || Method.All.ToString().ToUpper() == state.HttpMethod.ToString().ToUpper())
{
string rawData; using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
{
rawData = reader.ReadToEnd();
} //获取方法参数列表 ParameterInfo[] parameterInfos = state.MethodItem.GetParameters(); //参数
// List<object> paramters = new List<object>();
object[] paramters = new object[parameterInfos.Length]; for (int i = 0; i < parameterInfos.Length; i++)
{
ParameterInfo item = parameterInfos[i];
if (item.ParameterType == typeof(string) || item.ParameterType == typeof(int) || item.ParameterType == typeof(bool))
{
paramters[i] = JsonHelper.GetJsonValue(rawData, item.Name);
}
else
{
Type t = item.ParameterType;
paramters[i] = JsonConvert.DeserializeObject(rawData, t);
}
} object resobj = state.MethodItem.Invoke(state.ClassInstance, paramters);
if (typeof(string) == resobj.GetType())
{
ResponseWrite(response, resobj.ToString());
}
else
{
ResponseWrite(response, JsonConvert.SerializeObject(resobj));
}
}
else
{
ResponseWrite(response, $"不支持{request.HttpMethod.ToUpper()}方法请求!");
}
}
catch (Exception ex)
{
ResponseWrite(response, $"服务出现异常,异常信息:{ex.Message}");
}
} //重新监听 不写的话只能调用一次
AsyncCallback ac = new AsyncCallback(GetContextAsyncCallback);
state.Listerner.BeginGetContext(ac, state);
} /// <summary>
/// 回写响应
/// </summary>
/// <param name="response"></param>
/// <param name="Content"></param>
private static void ResponseWrite(HttpListenerResponse response, string Content)
{
//使用Writer输出http响应代码
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(response.OutputStream, new UTF8Encoding()))
{
response.ContentType = "application/json; charset=utf-8";
writer.WriteLine(Content);
writer.Close();
response.Close();
}
}
} public enum Method
{
All, Post, Get
} public class CallbackObject
{
/// <summary>
/// 监听
/// </summary>
public HttpListener Listerner { get; set; } /// <summary>
/// 方法
/// </summary>
public MethodInfo MethodItem { get; set; } /// <summary>
/// 调用者 对象
/// </summary>
public object ClassInstance { get; set; } /// <summary>
/// 调用方式 Get Post
/// </summary>
public Method HttpMethod { get; set; }
}
特性类 ActionName
    [AttributeUsage(AttributeTargets.Method)]
class ActionName : Attribute
{
public string URL { get; set; }
public ActionName()
{ }
public ActionName(string url)
{
this.URL = url;
}
}
特性类 HttpMethod
    [AttributeUsage(AttributeTargets.Method)]
public class HttpMethod : Attribute
{
public Method method { get; set; }
public HttpMethod()
{
this.method = Method.All;
} public HttpMethod(Method _method)
{
this.method = _method;
} }
特性类 FromApi
    [AttributeUsage(AttributeTargets.Class)]
public class FromApi : Attribute
{
//窗体里的具体方法忽略
}
帮助类 JsonHelper
    public static class JsonHelper
{
public static string GetJsonValue(string json, string key)
{
string value = "";
if (string.IsNullOrEmpty(json)) { return ""; }
JObject jo = (JObject)JsonConvert.DeserializeObject(json);
if (jo.ContainsKey(key))
{
if (jo[key] != null)
{
value = jo[key].ToString();
}
}
return value;
}
public static List<string> GetJsonList(string json, string key)
{
List<string> value = new List<string>();
if (string.IsNullOrEmpty(json)) { return new List<string>(); }
JObject jo = (JObject)JsonConvert.DeserializeObject(json);
if (jo.ContainsKey(key))
{
if (jo[key] != null)
{
foreach (var item in jo[key])
{
value.Add(item.ToString());
}
}
}
return value;
}
}

3.在Web.config中增加

  <appSettings>
<add key="httpserver" value="127.0.0.1:8022"/>
</appSettings>

4.使用方法

  • 4.1在窗体类上增加 [FromApi] 特性
    [FromApi]
public partial class ScoketForm : Form
{ }
  • 4.2Program.cs 中增加 这种初始化方式会对程序集中所有带有 [FromApi] 特性的类进行初始化
    Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//服务初始化
HttpApi.InitService(); //这里是增加初始化的代码
Application.Run(new SocketTest.gg.ScoketForm());
  • 4.3具体使用方法 HttpMethod 后面可以不写,不写的话代表 支持两种请求方式 POST,GET 需要注意命名空间的导入
        /// <summary>
/// 方法说明
/// </summary>
/// <returns></returns>
[HttpMethod(Method.Post), ActionName("/api/index")]
public HttpResult Index(List<string> Ids)
{ HttpResult httpResult = new HttpResult(); //具体方法内容
return httpResult;
}
如有疑问欢迎加入QQ群:765907694 交流!

优雅的在WinForm/WPF/控制台 中使用特性封装WebApi的更多相关文章

  1. Winform/WPF中内嵌BeetleX的HTTP服务

    在新版本的BeetleX.FastHttpApi加入了对netstandard2.0支持,如果程序基于.NetFramework4.6.1来构建WinForm或WPF桌面程序的情况下可以直接把Beet ...

  2. 提供PPT嵌入Winform/WPF解决方案,Winform/WPF 中嵌入 office ppt 解决方案

    Winform/WPF 中嵌入 office ppt(powerpoint)解决方案示: 1. 在winform中操作ppt,翻页.播放.退出:显示 总页数.当前播放页数 2. 启动播放ppt时录制视 ...

  3. 在WinForm应用程序中嵌入WPF控件

    我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...

  4. 把演讲人的桌面、头像、声音合成后推送到 指定的直播流平台上; 录制电脑桌面、摄像头头像、声音保存为本地视频; 适用于讲课老师、医生等演讲内容保存为视频; 提供PPT嵌入Winform/WPF解决方案,Winform/WPF 中嵌入 office ppt 解决方案

    提供PPT嵌入Winform/WPF解决方案,Winform/WPF 中嵌入 office ppt 解决方案 Winform/WPF 中嵌入 office ppt(powerpoint)解决方案示: ...

  5. 后续来啦:Winform/WPF中快速搭建日志面板

    后续来啦:Winform/WPF中快速搭建日志面板 继昨天发文ASP.NET Core 可视化日志组件使用(阅读文章,查看视频)后,视频下有朋友留言 "Winform客户端的程序能用它不?& ...

  6. 在WPF程序中打开网页:使用代理服务器并可进行JS交互

    本项目环境:使用VS2010(C#)编写的WPF程序,通过CefSharp在程序的窗体中打开网页.需要能够实现网页后台JS代码中调用的方法,从网页接收数据,并能返回数据给网页.运行程序的电脑不允许上网 ...

  7. 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常

    毕竟人不是神,谁写的程序都会有bug,有了bug不可怕,可怕的是出错了,你却不知道错误在哪里.所以我们需要将应用程序中抛出的所有异常都记录起来,不然出了错,找问题就能要了你的命.下面我们主要讨论的是如 ...

  8. Winform/WPF国际化处理

    1.Winfrom国际化处理方式 ==> Winform中国际化支持可将UI页面和.cs文件分开处理 处理窗体方式如下:1.选择Form窗体设置其--Language(默认中文--Default ...

  9. 初识MFC,WinForm,WPF,Q't

    MFC和QT是C++中常见的GUI框架,而WinForm和WPF是C#中常用的框架,不过我们一般很少叫WinForm框架,可能直接叫图形控件类库更多点.反正只是个称呼罢了,爱咋叫就咋叫.另外WinFo ...

随机推荐

  1. Java - 格式化输出JSON字符串的两种方式

    目录 1 使用阿里的fastjson 1.1 项目的pom.xml依赖 1.2 Java示例代码 2 使用谷歌的gson 2.1 项目的pom.xml依赖 2.2 Java示例代码 1 使用阿里的fa ...

  2. Android Native 内存泄漏系统化解决方案

    导读:C++内存泄漏问题的分析.定位一直是Android平台上困扰开发人员的难题.因为地图渲染.导航等核心功能对性能要求很高,高德地图APP中存在大量的C++代码.解决这个问题对于产品质量尤为重要和关 ...

  3. 20141126-传智播客.NET3.2版

  4. 程序员到sql笔记

    1最近准备面试,总结一下之前学过到东西.

  5. Excel催化剂开源第46波-按行列排列多个图形技术要点

    此篇对应功能出自:第10波-快速排列工作表图形对象 - 简书 https://www.jianshu.com/p/eab71f2969a6 在Excel的对象模型中,列的宽度不是一般所期待的和行高一样 ...

  6. 个人永久性免费-Excel催化剂功能第30波-工作表快捷操作(批量创建、命名、排序、工作表目录)

    日常使用Excel过程中,最多的操作无外乎单元格和工作表的操作,单元格的操作在前面已经有详细的辅助功能提供,此篇提供工作表相关的操作.这两项的操作若能有提速,日常大量的工作叠加起来真是省下不少时间. ...

  7. Git远程版本库

    目前为止,所有的Git操作都是在一个本地版本库中.现在是时候来体验Git分布式的特性了. 说到远程版本库,大家最为熟悉的就是GitHub了,它实际上就相当于一个远程版本库,托管着所有的本地版本库的提交 ...

  8. 绿色版的mysql 下载安装配置方式

    解压下载好的压缩包 下载地址 mysql-5.6.26-win64 绿色版 copy 一份my-default.ini改名字为my.ini为mysql的配置文件 打开my.ini 修改配置文件 默认的 ...

  9. 戴尔PowerEdge T110 Ⅱ服务器U盘安装Windows Server 2019 DataCenter

    一. 下载准备 准备工作——下载Microsoft Windows Server 2019 官方简体中文激活版 (MSDN)原版iso镜像 准备工作——安装刻录软件UltraISO,单文件绿色版就够用 ...

  10. linux杂货铺

    vmware虚拟机克隆后网卡不能使用 解决方法如下 cat /etc/udev/rules.d/70-persistent-net.rules1.将eth0这行注释掉或者删除,这里记载的还是克隆系统时 ...