接上一篇: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就不贴了