Unity 与 ASP.NET Core 和 MVC6(核心)

2024-03-25

更新 09.08.2018
Unity正在开发中here https://github.com/unitycontainer/container但我还没有时间测试它如何与 ASP.NET Core 框架配合使用。

更新 15.03.2018
此解决方案针对在使用 .NET Framework 4.5.2 时将 ASP.NET Core v1 与 Unity 结合使用的特定问题NOT.NET 核心框架。我必须使用此设置,因为我需要一些 .Net 4.5.2 DLL,但对于任何重新开始的人,我不会推荐此方法。而且 Unity 没有进一步开发(据我所知),因此我建议在新项目中使用 Autofac 框架。看到这个Post https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection#replacing-the-default-services-container有关如何执行此操作的更多信息。

Intro
我正在使用 ASP.NET 和 MVC 构建 Web 应用程序。此应用程序依赖于某些服务(WCF 服务、数据存储服务等)。现在,为了保持良好的状态和解耦,我想使用 DI(依赖注入)框架,特别是 Unity。

初步研究
我找到了这个博客文章 http://www.global-webnet.com/blog/post/2015/08/24/Dependency-Injection-in-ASPNET-vNext-(adding-Unity-container).aspx但遗憾的是它不起作用。不过这个主意不错。
它基本上是说,您不应该将 ServiceCollection 中注册的所有服务注册到您自己的容器中,而应该引用默认的 ServiceProvider。
所以。如果需要解决某些问题,则会调用默认的 ServiceProvider,如果没有解决方案,则将使用您的自定义 UnityContainer 来解决类型问题。

问题
MVC 总是尝试使用默认的 ServiceProvider 来解析 Controller。
另外,我注意到即使控制器能够正确解析,我也永远无法“混合”依赖项。现在,如果我想使用我的服务之一,但也想使用 ASP 的 IOptions 接口,则该类永远无法解析,因为这两个容器中没有一个具有这两种类型的解析。

我需要的
因此,回顾一下我需要以下内容:

  • 我不需要将 ASP.NET 依赖项复制到 Unity 容器中的设置
  • 一个可以解析我的 MVC 控制器的容器
  • 可以解决“混合”依赖关系的容器

EDIT:
那么问题是我怎样才能实现这些目标呢?

Environment
project.json:
enter image description here


因此,经过一些研究,我针对我的问题提出了以下解决方案:

将 Unity 与 ASP 结合使用
为了能够将 Unity 与 ASP 结合使用,我需要一个自定义 IServiceProvider (ASP 文档 https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#replacing-the-default-services-container)所以我为 IUnityContainer 编写了一个包装器,如下所示

public class UnityServiceProvider : IServiceProvider
{
    private IUnityContainer _container;

    public IUnityContainer UnityContainer => _container;

    public UnityServiceProvider()
    {
        _container = new UnityContainer();
    }

    #region Implementation of IServiceProvider

    /// <summary>Gets the service object of the specified type.</summary>
    /// <returns>A service object of type <paramref name="serviceType" />.-or- null if there is no service object of type <paramref name="serviceType" />.</returns>
    /// <param name="serviceType">An object that specifies the type of service object to get. </param>
    public object GetService(Type serviceType)
    {
        //Delegates the GetService to the Containers Resolve method
        return _container.Resolve(serviceType);
    }

    #endregion
}

此外,我还必须更改 Startup 类中的ConfigureServices 方法的签名:

public void ConfigureServices(IServiceCollection services)

to this:

public IServiceProvider ConfigureServices(IServiceCollection services)

现在我可以返回我的自定义 IServiceProvider,并且将使用它将代替默认的 IServiceProvider。
完整的配置服务方法显示在底部的连接部分。

解析控制器
我发现这篇博文 https://simpleinjector.org/blog/2016/07/working-around-the-asp-net-core-di-abstraction/。从中我了解到 MVC 使用 IControllerActivator 接口来处理控制器实例化。所以我自己写了一个,看起来像这样:

public class UnityControllerActivator : IControllerActivator
{
    private IUnityContainer _unityContainer;

    public UnityControllerActivator(IUnityContainer container)
    {
        _unityContainer = container;
    }

    #region Implementation of IControllerActivator

    public object Create(ControllerContext context)
    {
        return _unityContainer.Resolve(context.ActionDescriptor.ControllerTypeInfo.AsType());
    }


    public void Release(ControllerContext context, object controller)
    {
        //ignored
    }

    #endregion
}

现在,如果激活一个 Controller 类,它将使用我的 UnityContainer 进行实例化。因此我的 Unity 容器必须知道如何解析任何控制器!

下一个问题:使用默认的 IServiceProvider
现在,如果我在 ASP.NET 中注册 Mvc 等服务,我通常会这样做:

services.AddMvc();

现在,如果我使用 UnityContainer,则所有 MVC 依赖项都无法解析,因为它们未注册。所以我可以注册它们(如 AutoFac),也可以创建一个 UnityContainerExtension。我选择了扩展并提出了以下两个类:
UnityFallbackProvider扩展

public class UnityFallbackProviderExtension : UnityContainerExtension
{
    #region Const

    ///Used for Resolving the Default Container inside the UnityFallbackProviderStrategy class
    public const string FALLBACK_PROVIDER_NAME = "UnityFallbackProvider";

    #endregion

    #region Vars

    // The default Service Provider so I can Register it to the IUnityContainer
    private IServiceProvider _defaultServiceProvider;

    #endregion

    #region Constructors

    /// <summary>
    /// Creates a new instance of the UnityFallbackProviderExtension class
    /// </summary>
    /// <param name="defaultServiceProvider">The default Provider used to fall back to</param>
    public UnityFallbackProviderExtension(IServiceProvider defaultServiceProvider)
    {
        _defaultServiceProvider = defaultServiceProvider;
    }

    #endregion

    #region Overrides of UnityContainerExtension

    /// <summary>
    /// Initializes the container with this extension's functionality.
    /// </summary>
    /// <remarks>
    /// When overridden in a derived class, this method will modify the given
    /// <see cref="T:Microsoft.Practices.Unity.ExtensionContext" /> by adding strategies, policies, etc. to
    /// install it's functions into the container.</remarks>
    protected override void Initialize()
    {
        // Register the default IServiceProvider with a name.
        // Now the UnityFallbackProviderStrategy can Resolve the default Provider if needed
        Context.Container.RegisterInstance(FALLBACK_PROVIDER_NAME, _defaultServiceProvider);

        // Create the UnityFallbackProviderStrategy with our UnityContainer
        var strategy = new UnityFallbackProviderStrategy(Context.Container);

        // Adding the UnityFallbackProviderStrategy to be executed with the PreCreation LifeCycleHook
        // PreCreation because if it isnt registerd with the IUnityContainer there will be an Exception
        // Now if the IUnityContainer "magically" gets a Instance of a Type it will accept it and move on
        Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
    }

    #endregion
}


UnityFallbackProvider策略:

public class UnityFallbackProviderStrategy : BuilderStrategy
{
    private IUnityContainer _container;

    public UnityFallbackProviderStrategy(IUnityContainer container)
    {
        _container = container;
    }

    #region Overrides of BuilderStrategy

    /// <summary>
    /// Called during the chain of responsibility for a build operation. The
    /// PreBuildUp method is called when the chain is being executed in the
    /// forward direction.
    /// </summary>
    /// <param name="context">Context of the build operation.</param>
    public override void PreBuildUp(IBuilderContext context)
    {
        NamedTypeBuildKey key = context.OriginalBuildKey;

        // Checking if the Type we are resolving is registered with the Container
        if (!_container.IsRegistered(key.Type))
        {
            // If not we first get our default IServiceProvider and then try to resolve the type with it
            // Then we save the Type in the Existing Property of IBuilderContext to tell Unity
            // that it doesnt need to resolve the Type
            context.Existing = _container.Resolve<IServiceProvider>(UnityFallbackProviderExtension.FALLBACK_PROVIDER_NAME).GetService(key.Type);
        }

        // Otherwise we do the default stuff
        base.PreBuildUp(context);
    }

    #endregion
}

现在,如果我的 UnityContainer 没有注册某些东西,它只需向默认提供者询问即可。
我从几篇不同的文章中了解到了所有这些

  • MSDN Unity 文章 https://msdn.microsoft.com/en-us/library/dn178462(v=pandp.30).aspx
  • 自动模拟 Unity 容器扩展 http://www.mikevalenty.com/auto-mocking-unity-container-extension/
  • 自定义对象工厂 Unity 扩展 http://mark-dot-net.blogspot.de/2009/09/custom-object-factory-unity-extension.html

这种方法的好处是我现在还可以“混合”依赖项。如果我需要任何服务和 ASP 的 IOptions 接口,我的 UnityContainer 将解析所有这些依赖项并将它们注入我的控制器!
唯一要记住的是,如果我使用任何自己的依赖项,我必须向 Unity 注册我的控制器类,因为默认的 IServiceProvider 无法再解析我的控制器依赖项。

最后:连线
现在,在我的项目中,我使用不同的服务(ASP 选项、带选项的 MVC)。为了使这一切正常工作,我的ConfigureServices方法现在看起来像这样:

public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // Add all the ASP services here
        // #region ASP
        services.AddOptions();
        services.Configure<WcfOptions>(Configuration.GetSection("wcfOptions"));

        var globalAuthFilter = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();

        services.AddMvc(options => { options.Filters.Add(new AuthorizeFilter(globalAuthFilter)); })
                .AddJsonOptions
            (
                options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()
            );
        // #endregion ASP

        // Creating the UnityServiceProvider
        var unityServiceProvider = new UnityServiceProvider();

        IUnityContainer container = unityServiceProvider.UnityContainer;

        // Adding the Controller Activator
        // Caution!!! Do this before you Build the ServiceProvider !!!
        services.AddSingleton<IControllerActivator>(new UnityControllerActivator(container));

        //Now build the Service Provider
        var defaultProvider = services.BuildServiceProvider();

        // Configure UnityContainer
        // #region Unity

        //Add the Fallback extension with the default provider
        container.AddExtension(new UnityFallbackProviderExtension(defaultProvider));

        // Register custom Types here

        container.RegisterType<ITest, Test>();

        container.RegisterType<HomeController>();
        container.RegisterType<AuthController>();

        // #endregion Unity

        return unityServiceProvider;
    }

因为我在过去的一周里学到了关于 DI 的大部分知识,所以我希望我没有打破任何大的原则/模式,如果是的话请告诉我!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity 与 ASP.NET Core 和 MVC6(核心) 的相关文章

  • 检查两个数是否是彼此的排列?

    给定两个数字 a b 使得 1 例如 123 是 312 的有效排列 我也不想对数字中的数字进行排序 如果您指的是数字的字符 例如 1927 和 9721 则 至少 有几种方法 如果允许排序 一种方法是简单地sprintf将它们放入两个缓冲
  • C# 和 Javascript SHA256 哈希的代码示例

    我有一个在服务器端运行的 C 算法 它对 Base64 编码的字符串进行哈希处理 byte salt Convert FromBase64String serverSalt Step 1 SHA256Managed sha256 new S
  • 获取按下的按钮的返回值

    我有一个在特定事件中弹出的表单 它从数组中提取按钮并将标签值设置为特定值 因此 如果您要按下或单击此按钮 该函数应返回标签值 我怎样才能做到这一点 我如何知道点击了哪个按钮 此时代码返回 DialogResult 但我想从函数返回 Tag
  • 将数组向左或向右旋转一定数量的位置,复杂度为 o(n)

    我想编写一个程序 根据用户的输入 正 gt 负 include
  • 如何避免情绪低落?

    我有一个实现状态模式每个状态处理从事件队列获取的事件 根据State因此类有一个纯虚方法void handleEvent const Event 事件继承基础Event类 但每个事件都包含其可以是不同类型的数据 例如 int string
  • 使闭包捕获的变量变得易失性

    闭包捕获的变量如何与不同线程交互 在下面的示例代码中 我想将totalEvents 声明为易失性的 但C 不允许这样做 是的 我知道这是错误的代码 这只是一个例子 private void WaitFor10Events volatile
  • 为什么#pragma optimize("", off)

    我正在审查一个 C MFC 项目 在某些文件的开头有这样一行 pragma optimize off 我知道这会关闭所有以下功能的优化 但这样做的动机通常是什么 我专门使用它来在一组特定代码中获得更好的调试信息 并在优化的情况下编译应用程序
  • C 预处理器库

    我的任务是开发源分析工具C程序 并且我需要在分析本身之前预处理代码 我想知道什么是最好的图书馆 我需要一些重量轻 便于携带的东西 与其推出自己的 为什么不使用cpp这是的一部分gcc suite http gcc gnu org onlin
  • Cython 和类的构造函数

    我对 Cython 使用默认构造函数有疑问 我的 C 类 Node 如下 Node h class Node public Node std cerr lt lt calling no arg constructor lt lt std e
  • 如何返回 json 结果并将 unicode 字符转义为 \u1234

    我正在实现一个返回 json 结果的方法 例如 public JsonResult MethodName Guid key var result ApiHelper GetData key Data is stored in db as v
  • 如何将图像路径保存到Live Tile的WP8本地文件夹

    我正在更新我的 Windows Phone 应用程序以使用新的 WP8 文件存储 API 本地文件夹 而不是 WP7 API 隔离存储文件 旧的工作方法 这是我如何成功地将图像保存到 共享 ShellContent文件夹使用隔离存储文件方法
  • C# 中的递归自定义配置

    我正在尝试创建一个遵循以下递归结构的自定义配置部分
  • 在数据库中搜索时忽略空文本框

    此代码能够搜索数据并将其加载到DataGridView基于搜索表单文本框中提供的值 如果我将任何文本框留空 则不会有搜索结果 因为 SQL 查询是用 AND 组合的 如何在搜索 从 SQL 查询或 C 代码 时忽略空文本框 private
  • 如何将单个 char 转换为 int [重复]

    这个问题在这里已经有答案了 我有一串数字 例如 123456789 我需要提取它们中的每一个以在计算中使用它们 我当然可以通过索引访问每个字符 但是如何将其转换为 int 我研究过 atoi 但它需要一个字符串作为参数 因此 我必须将每个字
  • 当操作繁忙时,表单不执行任何操作(冻结)

    我有一个使用 C 的 WinForms 应用程序 我尝试从文件中读取一些数据并将其插入数据表中 当此操作很忙时 我的表单冻结并且无法移动它 有谁知道我该如何解决这个问题 这可能是因为您在 UI 线程上执行了操作 将文件和数据库操作移至另一个
  • 如何在 VBA 中声明接受 XlfOper (LPXLOPER) 类型参数的函数?

    我在之前的回答里发现了问题 https stackoverflow com q 19325258 159684一种无需注册即可调用 C xll 中定义的函数的方法 我之前使用 XLW 提供的注册基础结构 并且使用 XlfOper 类型在 V
  • C++ 复制初始化和直接初始化,奇怪的情况

    在继续阅读本文之前 请阅读在 C 中 复制初始化和直接初始化之间有区别吗 https stackoverflow com questions 1051379 is there a difference in c between copy i
  • 控制到达非 void 函数末尾 -wreturn-type

    这是查找四个数字中的最大值的代码 include
  • 在 Dynamics CRM 插件中访问电子邮件发件人地址

    我正在编写一个 Dynamics CRM 2011 插件 该插件挂钩到电子邮件实体的更新后事件 阶段 40 pipeline http msdn microsoft com en us library gg327941 aspx 并且在此阶
  • Process.Start 阻塞

    我正在调用 Process Start 但它会阻止当前线程 pInfo new ProcessStartInfo C Windows notepad exe Start process mProcess new Process mProce

随机推荐

  • Inno Setup - 更新时删除旧的/过时的文件

    所以 我意识到这个问题以前已经被问过 事实上 在写这篇文章之前 我读了其中的 10 篇文章 但没有一个有适用的解决方案 我希望现在有人已经找到了一些东西 问题 我的程序是使用脚本构建的 在单个文件夹中创建所有最终文件 这些文件包含在 inn
  • 使用 VBA 访问 Outlook 中的文件夹

    我正在使用以下命令将邮件移动到 Outlook 中的文件夹 Dim chemin as String chemin Split path Set myNameSpace Application GetNamespace MAPI Set m
  • 将 Image 数据类型转换为 Base64 并将 Base64 转换为 Image 数据类型

    我在 MS SQL 2012 中使用数据类型 Image 来存储图像 问题 我在 C 中的 BASE64 字符串中有一个图像 9j 4AAQSkZJRgABAQEASABIAAD 4SKhRXhpZgAATU0AKgAAAAgABwESAA
  • 有没有办法让一个对象在 Windows UA 中占据多个网格?

    我正在尝试制作我的第一个应用程序 但我在网格方面遇到了一些问题 我试图将屏幕的左侧设为地图 将右侧设为 2 个框 网格 我不确定是否有办法在多个网格中拥有一个对象 或者如何设置这样的布局 基本上是一个 左线消失了 到目前为止 这是我获得的布
  • 如何创建弹跳div动画

    我正在尝试重新创建弹跳箭头动画 如下所示 http www codecomputerlove com http www codecomputerlove com 但进展并不顺利 我尝试使用 Layerslider 中的内置动画最接近的内容可
  • 带有 标签的 SVG 不可见

    我对 SVG 还比较陌生 我一直在探索各种在线呈现它们的方法 每个似乎都有自己的优点和缺点 但
  • 将文本添加到行尾而不加载文件

    我需要将信息以许多字典的形式存储到一个非常大的文件中 这并不重要 只是说我试图首先将所有数据放入这些字典中 但我耗尽了内存 60Gb 很好 所以我想通过在行上执行循环并向每行附加一些文本来将数据添加到文件中 而不实际将其加载到内存中 那可能
  • Async/Await 是否使用 Task.Run 异步启动新线程?

    我读了很多文章 但仍然无法理解这部分 考虑这段代码 private async void button1 Click object sender EventArgs e await Dosomething private async Tas
  • Matlab中imagesc()的非均匀轴

    问题 是否可以在非均匀轴上说明图像 Details 我需要用图像来说明多维时间序列 但这个时间序列的时间网格非常不均匀 这是一个例子 m 10 n 3 t sort rand m 1 non uniform time values rand
  • :before/:after 伪元素的内容垂直居中

    我正在尝试实现类似于这张图片的效果 我有一个图像 作为幻灯片的一部分 包裹在 div 中 并使用 before 和 after 伪元素 我显示两个控件以移动到幻灯片的下一个 gt gt 或上一个 到目前为止 我有这个 div positio
  • 如何检查 emacs 是在框架中还是在终端中?

    基于这个问题 如何将emacsclient背景设置为Emacs背景 https stackoverflow com questions 9271930 how to set emacsclient background as emacs b
  • PHP内存限制

    在 PHP 5 0 4 中 如果您don t配置 enable memory limit 时 将忽略 memory limit 指令 在推荐的 php ini 文件中它被设置为 8M 但文档说它被忽略 那么在这种情况下 是否存在每个脚本的内
  • 使用客户端指纹对 JWT 令牌进行编码?

    我想知道是否会是最佳实践使用客户端指纹作为 JWT 秘密进行编码 然而 我在 WWW 上找不到有关这个问题的任何内容 但到目前为止 我这样做是有意义的 我正在考虑使用 JavaScript 生成指纹客户端 并在每次调用时将其发送到 API
  • 如何添加tailwindcss到vite?

    我在用着vite https github com vuejs vite 0 16 6并想将 vuepress 站点迁移到使用 vite 但是我不确定如何配置 vite 以使用 tailwindcss in my index css tai
  • 如何让3个textView控件大小相同

    在我的活动中我定义了 3 个 textView 控件 所有这些 textView 都一个挨着一个出现 我需要做一些事情 使它们始终具有相同的大小 假设第一个 textView 控件是小时 第二个 textView 控件是分钟 第三个 tex
  • 多个 Asp.Net 项目之间的共享代码 [重复]

    这个问题在这里已经有答案了 在同一服务器上的多个 Web 应用程序之间共享 bin 文件夹和 dll 以及其他资源文件 如 css 的最佳实践是什么 我已经将通用代码分离到它们自己的程序集中 但我想知道部署等 我基本上希望将所有通用文件位于
  • Meteor 需要时间才能知道是否存在 {{currentUser}}

    我有一些代码 我只想在存在时运行noUser和一些当有currentUser 所有这些都在导航模板内 就像这样 if currentUser li class nav a href Post a li li class nav a Ola
  • 如何在 Chrome 扩展程序中录制音频?

    设置 Chrome 扩展程序以从麦克风录制音频的最简单方法是什么 我看到有一个工作实验性语音输入API http code google com chrome extensions trunk experimental speechInpu
  • 将参数传递给函数以使用 ggplot stat_function 进行绘图

    我有一个函数和一个参数列表 F lt function a b a b b a L lt list a 5 b 2 c 0 我想用未知的 x 或 x 替换参数之一 a b 或 c 并使用 ggplot 的 stat function 进行绘
  • Unity 与 ASP.NET Core 和 MVC6(核心)

    更新 09 08 2018Unity正在开发中here https github com unitycontainer container但我还没有时间测试它如何与 ASP NET Core 框架配合使用 更新 15 03 2018此解决方