码迷,mamicode.com
首页 > 编程语言 > 详细

.net web api ioc unity usage

时间:2015-11-28 19:53:35      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

1.use nuget to install unity.webapi

 

2.add configurations in application_start folder

技术分享
using Microsoft.Practices.Unity;
using PatV2Tool.Bussiness.BLL;
using PatV2Tool.Bussiness.Contract;
using PatV2Tool.Bussiness.DAL;
using PatV2Tool.Framework.Contract;
using System.Web.Http;
using Unity.WebApi;

namespace PtvV2ToolWebApi
{
    public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();

            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // e.g. container.RegisterType<ITestService, TestService>();

            container.RegisterType<IServerService, ServerService>(new HierarchicalLifetimeManager());

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
    }
}
View Code

 

 

3.add to global.ascx something to 

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace PtvV2ToolWebApi
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            UnityConfig.RegisterComponents();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}
View Code

 

3.if sometimes, "

The type initializer for ‘System.Web.Http. GlobalConfiguration‘ threw an exception.

" appears,try applying the following cmdlet in nuget console.

技术分享
The type initializer for System.Web.Http. GlobalConfiguration threw an exception.
View Code

 

.net web api ioc unity usage

标签:

原文地址:http://www.cnblogs.com/hualiu0/p/5003310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!