标签:
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); } } }
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); } } }
3.if sometimes, "
" appears,try applying the following cmdlet in nuget console.
The type initializer for ‘System.Web.Http. GlobalConfiguration‘ threw an exception.
标签:
原文地址:http://www.cnblogs.com/hualiu0/p/5003310.html