因此,经过一些研究,我针对我的问题提出了以下解决方案:
将 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 的大部分知识,所以我希望我没有打破任何大的原则/模式,如果是的话请告诉我!