csharp MVC和Web Api Unity集成

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp MVC和Web Api Unity集成相关的知识,希望对你有一定的参考价值。

  /// <summary>Provides the bootstrapping for integrating Unity with WebApi when it is hosted in ASP.NET</summary>
    public static class UnityWebApiActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start() 
        {
            // Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution.
            //var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
            var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());

            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }
    }
public static void RegisterTypes(IUnityContainer container)
{
    // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
    // container.LoadConfiguration();

    container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifeTimeManager());

}
// Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution.
var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
//var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
// TODO: Uncomment if you want to use PerRequestLifetimeManager
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(mvc.App_Start.UnityWebActivator), "Start")]

namespace mvc.App_Start
{
    /// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
    public static class UnityWebActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start() 
        {
            var container = UnityConfig.GetConfiguredContainer();

            //FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            //FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
            
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            // TODO: Uncomment if you want to use PerRequestLifetimeManager
            //Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
        }
    }
}
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(mvc.App_Start.UnityWebActivator), "Start")]
public static void RegisterTypes(IUnityContainer container)
{
    // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
    // container.LoadConfiguration();

    container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());

}

以上是关于csharp MVC和Web Api Unity集成的主要内容,如果未能解决你的问题,请参考以下文章

用于依赖注入的 MVC Web API 和 Unity

ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi

csharp ASP.NET Web API:通过提供自己的AssembliesResolver来控制加载的程序集

无法使用 ASP.NET (MVC 4) Web API OData Prerelease 加载文件或程序集 System.Net.Http,版本 = 4.0.0.0

MVC3 - Unity/工作单元模式和 Web 服务实现

csharp Nancy和ASP.NET Web API比较