对于接口重要的应该是 功能实现,合法性验证,性能监控,日志等模块

通过unity aop功能可以实现统一的日志模块和性能监控。

1、新建mvc4 webapi项目 nuget添加 unity 3.0+版本 和 unity.Interception

2、重置mvc4 和webapi 的ioc容器:

 public class UnityDependencyResolver : System.Web.Mvc.IDependencyResolver, System.Web.Http.Dependencies.IDependencyResolver
{
private readonly IUnityContainer _unityContainer; public UnityDependencyResolver(IUnityContainer container)
{
this._unityContainer = container;
} public object GetService(Type serviceType)
{
try
{
return _unityContainer.Resolve(serviceType);
}
catch
{
return null;
}
} public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _unityContainer.ResolveAll(serviceType);
}
catch
{
return new List<object>();
}
} public IDependencyScope BeginScope()
{
return this;
} public void Dispose()
{
}
}
  public class UnityConfig
{
public static void Register()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>(); //这里最重要
container.LoadConfiguration();
var unity = new UnityDependencyResolver(container);
//设置mvc的依赖管理
DependencyResolver.SetResolver(unity);
//设置mvc的依赖管理
GlobalConfiguration.Configuration.DependencyResolver = unity;
}
}

3、在Global.asax 里调用:

   protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig.Register();
}

4、最后关于 unity的配置

  <configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />
</configSections> <!-- 千万别忘了这里这是实现aop扩展 -->
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" />
<typeAliases>
<!-- Lifetime manager types -->
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity" />
<typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,Microsoft.Practices.Unity" />
<typeAlias alias="perThread" type="Microsoft.Practices.Unity.PerThreadLifetimeManager,Microsoft.Practices.Unity" /> <!-- User-defined type aliases -->
<typeAlias alias="IBMapServer" type="CRM.Phone.Server.IBMapServer,CRM.Phone.Server" />
<typeAlias alias="BMapServer" type="CRM.Phone.Server.BMapServer,CRM.Phone.Server" />
<typeAlias alias="LoggingInterceptionBehavior" type="CRM.Phone.Interface.LoggingInterceptionBehavior,CRM.Phone.Interface" />
</typeAliases>
<container>
<types>
<!--<type type="IBMapServer" mapTo="BMapServer">
<lifetime type="singleton" />
</type>-->
</types>
<register type="IBMapServer" mapTo="BMapServer">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="LoggingInterceptionBehavior"/>
</register>
</container>
</unity>

这里以IBMapserver 接口为例 对IMBapserver 中所有的方法进行拦截。

5、在Controller里使用注入对象 使用Dependecy标签:

  public class BMapController : ApiController
{ [Dependency]
public IBMapServer MapServer { get; set; } public Point GetPointByid(int id)
{
return MapServer.getPoint(id);
}
}

6、如果发现注入对象为空,而且没有报错,请一定注意文中 红色的部分

7、最后贴一下 关于Logging处理代码

mvc4 web-api 与unity搭建接口的更多相关文章

  1. ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题

    原文:ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/MrTra ...

  2. 关于ASP.NET MVC4 Web API简单总结

    原文地址:http://www.cnblogs.com/lei2007/archive/2013/02/01/2888706.html wcf web api 和 asp.net web api , ...

  3. ASP.NET mvc4 WEB API异常处理

    当一个web api抛出一个异常后 此异常会被转化成一个HTTP响应 错误代码为500的服务错误 但是如果你不想让客户端看到500的错误码 你也可以自定义错误码 如下代码当用户输入的ID没有与之相关的 ...

  4. 用ASP.NET Web API技术开发HTTP接口

    开发工具 Visual Studio 2013 SQL Server 2008 R2 准备工作 启动Visual Studio 2013,新建一个ASP.NET Web应用程序,命名为SimpleAP ...

  5. ASP.NET Web API 2系列(一):初识Web API及手动搭建基本框架

    1.导言 随着Web技术的发展,现在各种框架,前端的,后端的,数不胜数.全栈工程师的压力越来越大. PC端,pad端,移动端App(安卓/IOS)的发展,使得前后端一体的开发模式十分笨重.因此,前后端 ...

  6. 用ASP.NET Web API技术开发HTTP接口(一)

    开发工具 Visual Studio 2013 SQL Server 2008 R2 准备工作 启动Visual Studio 2013,新建一个ASP.NET Web应用程序,命名为SimpleAP ...

  7. 用ASP.NET Web API技术开发HTTP接口(二)

    在第一部分,我们创建了一个基本的ASP.NET Web API项目,新建成功了数据表,然后添加了一些测试数据,最后创建了API控制器,用json格式把数据表里面的内容成功输出到浏览器上.接下来我们将继 ...

  8. .net web api ioc unity usage

    1.use nuget to install unity.webapi 2.add configurations in application_start folder using Microsoft ...

  9. Web Api 宿主的搭建

    首先我们要清楚一个概念,宿主.宿主是什么意思?先从了解一下Hosting开始吧! 有关Hosting的基础知识 Hosting是一个非常重要,但又很难翻译成中文的概念.翻译成:寄宿,大概能勉强地传达它 ...

随机推荐

  1. PL/SQL中LOOP循环控制语句

    在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...

  2. Spring各jar包的作用(转载)

    spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...

  3. 常用sql命令

    --1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话 CREATE TABLE student (     [id] [int] IDENTITY(1,1) NOT NU ...

  4. Qt使用Cookies对网站操作之Get和POST

    1.添加QNetwork模块: a.Qt Creator中打开.pro文件添加QT+=Network; b.VS_Qt中项目属性中Qt Project Settings中Qmodules中勾选”QNe ...

  5. Java内部类this$0字段产生的一个bug

    首先查看下面一段代码,我指出了问题代码的所在,读者先自己思考一下这段代码会有什么问题. 这是用clone方法完整拷贝一个二项堆(BinomialHeap)结构的代码.二项堆中包含一个内部类Binomi ...

  6. bzoj 3675 [Apio2014]序列分割(斜率DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3675 [题意] 将n个数的序列分割k次,每次的利益为分割后两部分数值和的积,求最大利益 ...

  7. [转]JQuery.Ajax之错误调试帮助信息

    下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求 ...

  8. Xcode 6 越狱开发基础

    最近接触到XCode越狱开发的问题,越狱开发首先iphone设备得越狱,然后安装Appsync,安装之后,安装ipa将不再验证程序签名的有效性,不签名的程序也可以直接在设备上运行,只需要保证IPA本身 ...

  9. python日志输出

    import logging logger = logging.getLogger() #生成一个日志对象,()内为日志对象的名字,可以不带,名字不给定就是root,一般给定名字,否则会把其他的日志输 ...

  10. Codeforces Educational Codeforces Round 15 A. Maximum Increase

    A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...