MicroService4Net快速搭建微服务(二)
来源:互联网 发布:淘宝结算不了 编辑:程序博客网 时间:2024/06/06 17:02
接上一篇:http://blog.csdn.net/joyhen/article/details/70244666
对于config的配置,进行了自定义处理,主要是控制器的分离,以及控制器和action路由响应处理,如下:
static void Main(string[] args) { const int apiport = 65511; var microService = new MicroService( port: apiport, serviceDisplayName: string.Format("直播系统API服务:{0}", apiport), serviceName: "xxLivePlatform", configure: x => { x.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); x.Services.Replace(typeof(IAssembliesResolver), new Custom.ControllerResolver()); x.Services.Replace(typeof(IHttpControllerSelector), new Custom.CHttpControllerSelector(x)); x.Services.Replace(typeof(IHttpActionSelector), new Custom.CControllerActionSelector()); x.Services.Replace(typeof(IHttpActionInvoker), new Custom.CControllerActionInvoker()); x.MessageHandlers.Add(new Custom.CDelegatingHandler()); }, useCors: true); microService.Run(args); }
ControllerResolver:
using System;using System.Collections.Generic;using System.Reflection;using System.Web.Http.Dispatcher;namespace ApiService.Custom{ /// <summary> /// 装载控制器 /// </summary> internal class ControllerResolver : DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { string[] dll = { "Controller" }; foreach (var d in dll) { try { AppDomain.CurrentDomain.Load(d + ",Version=1.0.0.0,PublicKeyToken=null"); } catch (System.IO.FileNotFoundException e) { Tools.LogHelp.Log(string.Format("未找到{0}.dll,程序集:Exception:{1}", d, e.Message)); } catch (BadImageFormatException e) { Tools.LogHelp.Log(string.Format("{0}.dll不是有效的程序集 或 程序集版本不匹配,Exception:{1}", d, e.Message)); } catch (Exception e) { Tools.LogHelp.Log(e.Message); } } return base.GetAssemblies(); } }}CHttpControllerSelector:
/// <summary> /// 自定义控制器选择 /// </summary> internal class CHttpControllerSelector : DefaultHttpControllerSelector { public CHttpControllerSelector(HttpConfiguration configuration) : base(configuration) { } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { HttpControllerDescriptor decriptor = null; try { decriptor = base.SelectController(request); } catch (HttpResponseException ex) { var code = ex.Response.StatusCode; if (code == HttpStatusCode.NotFound || code == HttpStatusCode.MethodNotAllowed) { //var msg = CMessage.makemsg("请求资源不存在", -1, ex.Response.Content.ReadAsAsync<object>().Result); var msg = CMessage.makemsg("请求资源不存在001", -1); ex.Response.Content = new StringContent(msg); } throw; } return decriptor; } }CControllerActionSelector:
using System.Net;using System.Net.Http;using System.Web.Http;using System.Web.Http.Controllers;namespace ApiService.Custom{ /// <summary> /// 自定义Action选择 /// </summary> internal class CControllerActionSelector : ApiControllerActionSelector { public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext) { HttpActionDescriptor decriptor = null; try { decriptor = base.SelectAction(controllerContext); } catch (HttpResponseException ex) { var code = ex.Response.StatusCode; if (code == HttpStatusCode.NotFound || code == HttpStatusCode.MethodNotAllowed) { //var msg = CMessage.makemsg("请求资源不存在", -1, ex.Response.Content.ReadAsAsync<object>().Result); var msg = CMessage.makemsg("请求资源不存在002", -1); ex.Response.Content = new StringContent(msg); } throw; } return decriptor; } }}CControllerActionInvoker:
using System.Net.Http;using System.Threading;using System.Threading.Tasks;using System.Web.Http.Controllers;namespace ApiService.Custom{ /// <summary> /// 自定义Action调用 /// </summary> internal class CControllerActionInvoker : ApiControllerActionInvoker { public override Task<HttpResponseMessage> InvokeActionAsync(HttpActionContext actionContext, CancellationToken cancellationToken) { var responseMessage = base.InvokeActionAsync(actionContext, cancellationToken); var ex = responseMessage.Exception; if (ex != null) { var baseException = ex.InnerExceptions[0]; var msg = CMessage.makemsg(baseException.Message, -1, ex.InnerExceptions); return Task.Run(() => new HttpResponseMessage() { Content = new StringContent(msg) }, cancellationToken); } return responseMessage; } }}CDelegatingHandler就不贴了
阅读全文
0 0
- MicroService4Net快速搭建微服务(二)
- MicroService4Net快速搭建微服务
- Spring Boot(二):快速搭建web项目或微服务
- 如何快速搭建一个微服务架构?
- 10分钟快速搭建本地微服务
- 如何快速搭建一个微服务架构?
- Spring-boot:快速搭建微服务框架
- 微服务之快速搭建Eureka Server
- 从0开始的微服务架构:(二)如何快速体验微服务架构?
- SpringBoot搭建微服务(二)实现MVC
- SpringCloud微服务架构搭建(二):配置中心
- 微服务(二)
- Spring Cloud 快速入门esclipse快速搭建微服务框架 (一)-注册与发现
- 微服务:dubbo与zookeeper的快速搭建
- spring cloud+dotnet core搭建微服务架构:服务发现(二)
- SpringCloud构建微服务入门架构(二)服务注册与发现Eureka集群搭建
- 使用SpringCloud搭建微服务<二>---------创建微服务的消费者
- SpringBoot搭建微服务(一)HelloWorld!
- H5 Web Notification桌面推送消息
- node.js 安装问题
- Tiles
- Python-selenium(1)环境部署
- 关于位图(Bitmap)
- MicroService4Net快速搭建微服务(二)
- 新篇章,关于Thinkphpu将emoji写入myql
- 自定义maven archetype
- 优秀的日期类工具, 处理各种格式的日期
- jdk 1.5中annotation的简介
- 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求
- HDU 4027
- 汇编语言: 试编制一个程序:从键盘输入一行字符,要求第一个键入的字符必须是空格符,如不 是,则退出程序;如是,则开始接收键入的字符并顺序存放在首地址为buffer的缓冲区中(空 格符不存入),直到接收
- js事件