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

通过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. make menuconfig 出错

    运行 #make menuconfig HOSTLD scripts/kconfig/mconf scripts/kconfig/mconf.o: In function `main': mconf. ...

  2. 如何借助于UML完成我们对系统的设计?谈谈我的理解

    首先要说的是我对面向对象的理解,以及设计类的依据: http://www.cnblogs.com/xinchrome/p/4904931.html 理解了这篇文章,也就理解了现在要说的. 在面向对象编 ...

  3. 自动FTP的小脚本

    自动FTP的小脚本 使用以下脚本,可以实现自动FTP,将你需要的文件传送到需要的地方,或者将需要的文件从某个地方抓取下来. cd /PATH_YOU_WANT_TO_UPLOAD(DOWNLOAD) ...

  4. linux面试题3

    1. 下面的网络协议中,面向连接的的协议是: A . A 传输控制协议 B 用户数据报协议 C 网际协议 D 网际控制报文协议 2. 在/etc/fstab文件中指定的文件系统加载参数中, D 参数一 ...

  5. Mac终端编译运行C++

    1.在编辑器中写好C++代码 2.打开终端打开文件对应的地址 3.用g++命令来编译.cpp文件 4.用./文件名来运行 观察文件的目录可发现 g++ 源文件名 编译源文件,产生a.out ./文件名 ...

  6. SharePoint 2013让页面显示错误

    转:http://blog.csdn.net/zmoneyz/article/details/20460263 1. 在网站端口下,如80端口下的Web.config修改 (1)将<custom ...

  7. Storm入门教程 第二章 构建Topology[转]

    2.1 Storm基本概念 在运行一个Storm任务之前,需要了解一些概念: Topologies Streams Spouts Bolts Stream groupings Reliability ...

  8. UI篇--布局问题

    1.android:layout_marginRight 不起作用解决方法 今天想在RelativeLayout的左右分别放上一个按钮, 左边按钮用marginLeft="10dp" ...

  9. 自定义TreeList单元格 z

    DevExpress Treelist自定义单元格,加注释和行序号.以上一节的列表为例,实现以下效果:预算大于110万的单元格突出显示,加上行序号以及注释,如下图: 添加行序号要用到CustomDra ...

  10. 数组乘积--满足result[i] = input数组中除了input[i]之外所有数的乘积(假设不会溢出

    数组乘积(15分) 输入:一个长度为n的整数数组input 输出:一个长度为n的整数数组result,满足result[i] = input数组中除了input[i]之外所有数的乘积(假设不会溢出). ...