mvc4 web-api 与unity搭建接口

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

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

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

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

 1 public class UnityDependencyResolver : System.Web.Mvc.IDependencyResolver, System.Web.Http.Dependencies.IDependencyResolver
 2     {
 3         private readonly IUnityContainer _unityContainer;
 4 
 5         public UnityDependencyResolver(IUnityContainer container)
 6         {
 7             this._unityContainer = container;
 8         }
 9 
10         public object GetService(Type serviceType)
11         {
12             try
13             {
14                 return _unityContainer.Resolve(serviceType);
15             }
16             catch
17             {
18                 return null;
19             }
20         }
21 
22         public IEnumerable<object> GetServices(Type serviceType)
23         {
24             try
25             {
26                 return _unityContainer.ResolveAll(serviceType);
27             }
28             catch
29             {
30                 return new List<object>();
31             }
32         }
33 
34         public IDependencyScope BeginScope()
35         {
36             return this;
37         }
38 
39         public void Dispose()
40         {
41         }
42     }

 

 1  public class UnityConfig
 2     {
 3         public static void Register()
 4         {
 5             IUnityContainer container = new UnityContainer();
 6             container.AddNewExtension<Interception>(); //这里最重要  7             container.LoadConfiguration();
 8             var unity = new UnityDependencyResolver(container);
 9             //设置mvc的依赖管理
10             DependencyResolver.SetResolver(unity);
11             //设置mvc的依赖管理
12             GlobalConfiguration.Configuration.DependencyResolver = unity;
13         }
14     }

3、在Global.asax 里调用:

 1   protected void Application_Start()
 2         {
 3             AreaRegistration.RegisterAllAreas();
 4 
 5             WebApiConfig.Register(GlobalConfiguration.Configuration);
 6             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 7             RouteConfig.RegisterRoutes(RouteTable.Routes);
 8             BundleConfig.RegisterBundles(BundleTable.Bundles);
 9             UnityConfig.Register();
10         }

4、最后关于 unity的配置

 1  <configSections>
 2      <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />
 3   </configSections> <!-- 千万别忘了这里这是实现aop扩展 -->
 4 <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
 5     <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" />
 6     <typeAliases>
 7       <!-- Lifetime manager types -->
 8       <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity" />
 9       <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,Microsoft.Practices.Unity" />
10       <typeAlias alias="perThread" type="Microsoft.Practices.Unity.PerThreadLifetimeManager,Microsoft.Practices.Unity" />
11 
12       <!-- User-defined type aliases -->
13       <typeAlias alias="IBMapServer" type="CRM.Phone.Server.IBMapServer,CRM.Phone.Server" />
14       <typeAlias alias="BMapServer" type="CRM.Phone.Server.BMapServer,CRM.Phone.Server" />
15       <typeAlias alias="LoggingInterceptionBehavior" type="CRM.Phone.Interface.LoggingInterceptionBehavior,CRM.Phone.Interface" />
16     </typeAliases>
17     <container>
18       <types>
19         <!--<type type="IBMapServer" mapTo="BMapServer">
20           <lifetime type="singleton" />
21         </type>-->
22       </types>
23       <register type="IBMapServer" mapTo="BMapServer">
24         <interceptor type="InterfaceInterceptor"/>
25         <interceptionBehavior type="LoggingInterceptionBehavior"/>
26       </register>
27     </container>
28   </unity>

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

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

 1  public class BMapController : ApiController
 2     {
 3 
 4         [Dependency]
 5         public IBMapServer MapServer { get; set; }
 6 
 7         public Point GetPointByid(int id)
 8         {
 9             return MapServer.getPoint(id); 
10         }
11    }

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

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

 

你可能感兴趣的:(unity)