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

通过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. 载入在线jQuery库

    以百度库为例 <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script&g ...

  2. 转载RabbitMQ入门(5)--主题

    主题(topic) (使用Java客户端) 在先前的指南中我们改进了我们的日志系统.取代使用fanout类型的交易所,那个仅仅有能力实现哑的广播,我们使用一个direct类型的交易所,获得一个可以有选 ...

  3. delphi 注册 dcc70.dll

    @echo 开始注册copy dcc70.dll %windir%\system32\regsvr32 %windir%\system32\dcc70.dll /s@echo dcc70.dll注册成 ...

  4. jQuery 制作的Tab标签切换选项卡

    基于jQuery实现的一个选项卡效果,重点体现在HTML里没有内联事件处理程序,而是定义在js文件里,做到行为与结构的分离.在实际应用过程中,只要保证选项卡模块结构代码的完整性,就可以任意添加N个同类 ...

  5. MySQL压测中遇到的一些问题

    批量insert http://blog.csdn.net/xiaoxian8023/article/details/20155429 Mysql jdbc 批处理数据,需要给jdbc连接加上rewr ...

  6. C#中嵌入互操作类型的含义

    首先说一下它的含义: 1. ”嵌入互操作类型”中的嵌入就是引进.导入的意思,类似于c#中using,c中include的作用,目的是告诉编译器是否要把互操作类型引入. 2. “互操作类型”实际是指一系 ...

  7. 火狐和google游览器的 hack独有识别 css

    先来看google的: /* 这针对于webkit内核的游览器.包括苹果谷歌游览器等*/ @media screen and (-webkit-min-device-pixel-ratio:0) { ...

  8. Android智能聊天机器人

    http://www.tuling123.com/       注册一个账号,申请一个KEY值.此网站也有文档,可以查看. package com.tulingdemo; import java.te ...

  9. Thrift框架使用C++的一个demo

    Thrift编译器会根据选择的目标语言为server产生服务接口代码,为client产生stubs,参数可以是基本类型和结构体. 代码框架用的Thrift,为了了解结构,学习写了一个thrift的De ...

  10. 《Python CookBook2》 第四章 Python技巧 对象拷贝 && 通过列表推导构建列表

    (先学第四章) 对象拷贝 任务: Python通常只是使用指向原对象的引用,并不是真正的拷贝. 解决方案: >>> a = [1,2,3] >>> import c ...