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

通过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. 查看buffer cache命中率

    SQL> select name,value from v$sysstat where name in('db block gets','consistent gets','physical r ...

  2. linux面试题1

    一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统.3. Linux文件系统中每个文件用 i节点 来标识. ...

  3. 深入浅出ClassLoader

    你真的了解ClassLoader吗? 这篇文章翻译自zeroturnaround.com的 Do You Really Get Classloaders? ,融入和补充了笔者的一些实践.经验和样例.本 ...

  4. 【转】ACE开发环境搭建

    Windows平台 1)        下载ACE源码 ACE官方网址:http://www.cs.wustl.edu/~schmidt/ACE.html ACE下载地址:http://downloa ...

  5. replace() MySQL批量替换指定字段字符串

    mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...

  6. UI篇--Android中3种方法实现back键动作

    方法一:重写onBackPressed方法 @Override public void onBackPressed() { // do something what you want super.on ...

  7. IOS 给图片添加水印 打印文字

    1.加文字   -(UIImage *)addText:(UIImage *)img text:(NSString *)text1  {           //get image width and ...

  8. Python pycurl

    常用方法: pycurl.Curl() #创建一个pycurl对象的方法 pycurl.Curl(pycurl.URL, http://www.google.com.hk) #设置要访问的URL py ...

  9. Linux下用Intel编译器编译安装NetCDF-Fortan库(4.2以后版本)

    本来这个问题真的没必要写的,可是真的困扰我太久%>_<%,决定还是记录一下. 首先,最权威清晰的安装文档还是官方的: Building the NetCDF-4.2 and later F ...

  10. [Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

    Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Crea ...