C# Breeze和Unity容器(热毛巾角形和#x2B;Breeze)

C# Breeze和Unity容器(热毛巾角形和#x2B;Breeze),c#,inversion-of-control,unity-container,breeze,hottowel,C#,Inversion Of Control,Unity Container,Breeze,Hottowel,我们正在尝试创建一个基于HotTower+Breeze的配送配方。SPA应用程序中的依赖项注入部分已被很好地介绍,但试图通过Unity解决BreezeController是一个挑战。我可能缺少一件事,但还没有弄明白 到目前为止,我们已经完成了以下工作: 已安装适用于ASP.NET MVC 3.5.1404的Unity引导程序 添加了一个名为LoggingController的新控制器,该控制器应用BreezeController属性 在此控制器上,通过构造函数设置存储库依赖项 一切正常且干净

我们正在尝试创建一个基于HotTower+Breeze的配送配方。SPA应用程序中的依赖项注入部分已被很好地介绍,但试图通过Unity解决BreezeController是一个挑战。我可能缺少一件事,但还没有弄明白

到目前为止,我们已经完成了以下工作:

  • 已安装适用于ASP.NET MVC 3.5.1404的Unity引导程序
  • 添加了一个名为LoggingController的新控制器,该控制器应用BreezeController属性
  • 在此控制器上,通过构造函数设置存储库依赖项
一切正常且干净,Unity容器通过UnityMvcActivator.cs激活,如下所示:

    using System.Linq;
    using System.Web.Mvc;
    using Microsoft.Practices.Unity.Mvc;

    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(CC.Web.App_Start.UnityWebActivator), "Start")]
    [assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(CC.Web.App_Start.UnityWebActivator), "Shutdown")]

    namespace CC.Web.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));
        }

        /// <summary>Disposes the Unity container when the application is shut down.</summary>
        public static void Shutdown()
        {
            var container = UnityConfig.GetConfiguredContainer();
            container.Dispose();
        }
    }
}
使用System.Linq;
使用System.Web.Mvc;
使用Microsoft.Practices.Unity.Mvc;
[assembly:WebActivatorEx.PreApplicationStartMethod(typeof(CC.Web.App\u Start.UnityWebActivator),“Start”)]
[程序集:WebActivatorEx.ApplicationShutdownMethod(typeof(CC.Web.App_Start.UnityWebActivator),“Shutdown”)]
命名空间CC.Web.App\u开始
{
///提供用于将Unity与ASP.NET MVC集成的引导。
公共静态类UnityWebActivator
{
///在应用程序启动时集成Unity。
公共静态void Start()
{
var container=UnityConfig.GetConfiguredContainer();
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType().First());
FilterProviders.Providers.Add(新UnityFilterAttributeFilterProvider(容器));
SetResolver(新UnitedDependencyResolver(容器));
//TODO:如果要使用PerRequestLifetimeManager,请取消注释
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
///在应用程序关闭时处置Unity容器。
公共静态无效关机()
{
var container=UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
}
容器是通过UnityConfig.cs配置的

using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;
using System.Web.Http;

namespace CC.Web.App_Start
{
    /// <summary>
    /// Specifies the Unity configuration for the main container.
    /// </summary>
    public class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer(); 
            RegisterTypes(container);
            return container;
        });

        /// <summary>
        /// Gets the configured Unity container.
        /// </summary>
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        #endregion

        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        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();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();
        }
    }
}
使用系统;
使用Microsoft.Practices.Unity;
使用Microsoft.Practices.Unity.Configuration;
使用System.Web.Mvc;
使用Microsoft.Practices.Unity.Mvc;
使用System.Web.Http;
命名空间CC.Web.App\u开始
{
/// 
///指定主容器的统一配置。
/// 
公共类UnityConfig
{
#区域统一容器
私有静态惰性容器=新惰性(()=>
{
var container=new UnityContainer();
注册类型(容器);
返回容器;
});
/// 
///获取已配置的Unity容器。
/// 
公共静态IUnityContainer GetConfiguredContainer()
{
返回容器。值;
}
#端区
///向Unity容器注册类型映射。
///要配置的unity容器。
///不需要注册控制器或API控制器等具体类型(除非您想注册)
///更改默认值),因为Unity允许解析具体类型,即使它以前没有注册。
公共静态无效注册表类型(IUnityContainer容器)
{
//注意:若要从web.config加载,请取消对以下行的注释。请确保将Microsoft.Practices.Unity.Configuration添加到using语句中。
container.LoadConfiguration();
//TODO:在此处注册您的类型
//container.RegisterType();
}
}
}
调用控制器时,Unity似乎不负责,因为它抛出了一个关于找不到空构造函数的错误

我假设
dependencysolver.SetResolver(新的UnitedPendencySolver(容器))
会正确地告诉你应该使用哪个解析器,就像Steve在Mike Wasson博客中提到的:
config.dependencySolver=new UnityResolver(container)。UnitedPendencyResolver不实现IDependencyResolver

所以我要问的是,假设安装Nuget包只是将Unity作为解析器挂钩所需的,那么有没有办法将Unity用作Breeze控制器的解析器,或者我们应该简单地将Breeze控制器用作正确构建的组件的门面?我希望通过Unity透明地进行解析,而不是通过门面。

您需要用于解析WebApi控制器(包括Breeze控制器)的包

您还需要告诉WebApi使用Unity来解决依赖关系。通常,这是通过从
Global.asax.cs
调用的某个静态方法完成的。把一切都摆出来


建议使用
UnityConfig.RegisterComponents()
方法注册您的存储库。

谢谢Steve,我将尝试改用Unity.AspNet.WebApi引导程序。