【问题标题】:DryIoC won't inject properties into ASP.NET Core MVC controllerDryIoC 不会将属性注入 ASP.NET Core MVC 控制器
【发布时间】:2017-05-30 11:16:46
【问题描述】:

我使用https://bitbucket.org/dadhi/dryioc/src/589e7c0b356a/NetCore/src/DryIoc.AspNetCore.Sample 作为基线。尝试使用以下内容实现基于属性的属性注入选择器:

private static PropertyOrFieldServiceInfo GetImportedPropertiesAndFields(MemberInfo m, Request req)
    {
        var import = (DependencyAttribute)m.GetAttributes(typeof(DependencyAttribute)).FirstOrDefault();

        return import == null ? null : PropertyOrFieldServiceInfo.Of(m)
            .WithDetails(ServiceDetails.Of(import.ContractType, import.ContractName), req);
    }

DependencyAttribute 标记要注入的属性。 如果不将此解决方案嵌入到 ASP.NET MVC Core 应用程序中,它可以正常工作。 当我尝试使用 .WithDependencyInjectionAdapter(...) 在 ASP.NET Core 应用程序中使用 [Dependency] 属性在控制器中注入属性时,它不起作用,它只注入(并拦截)那些类,接管ConfigureServices(以及之后在.AddDryIoc<TCompositionRoot>)中的服务之后注册的内容。

我使用的代码部分:

public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        return services.AddDryIoc<CompositionRoot>();
    }

DI类:

public static class DI
{
    public static readonly PropertiesAndFieldsSelector SelectPropertiesAndFieldsWithDependencyAttribute = PropertiesAndFields.All(withInfo: GetImportedPropertiesAndFields);

    public static IServiceProvider AddDryIoc<TCompositionRoot>(this IServiceCollection services)
    {
        var logger = InfrastructureFactory.CreateDefaultNLogger().CreateLogger<Startup>();

        var container = new Container()
            .WithDependencyInjectionAdapter(services, throwIfUnresolved: type => type.Name.EndsWith("Controller"))
            .With(rules => rules.With(SelectPropertiesAndFieldsWithDependencyAttribute).WithoutThrowOnRegisteringDisposableTransient());

        container.RegisterMany<TCompositionRoot>();
        container.Resolve<TCompositionRoot>();

        logger.LogInformation("Verifying DryIoC resolutions...");

        var resolutionErrors = container.VerifyResolutions();

        if (resolutionErrors != null && resolutionErrors.Any())
        {
            foreach (var errors in container.VerifyResolutions())
            {
                logger.LogError($"DryIoC resolution error for type {errors.Key.ServiceType} : {errors.Value.Message} ({errors.Value.StackTrace})");
            }

            logger.LogWarning("DryIoC resolutions are WRONG.");
        }
        else
        {
            logger.LogInformation("DryIoC resolutions are OK.");
        }

        return container.Resolve<IServiceProvider>();
    }

    #region DryIoc Property Dependency Resolver helper
    private static PropertyOrFieldServiceInfo GetImportedPropertiesAndFields(MemberInfo m, Request req)
    {
        var import = (DependencyAttribute)m.GetAttributes(typeof(DependencyAttribute)).FirstOrDefault();

        return import == null ? null : PropertyOrFieldServiceInfo.Of(m)
            .WithDetails(ServiceDetails.Of(import.ContractType, import.ContractName), req);
    }
    #endregion

}

补充资料:

  1. 构造函数注入正在控制器中工作。
  2. 彻底搜索了DryIOC Container configuration for property injection,但它是关于一个WebApi应用程序,而不是我尝试的情况。
  3. 我尝试切换.WithDependencyInjectionAdapter(...).With(rules =&gt; ...) 的顺序,但没有成功。
  4. 拦截也不适用于控制器。我正在使用@dadhi 的拦截建议:https://bitbucket.org/dadhi/dryioc/wiki/Interception。顺便说一句,您如何使用 Castle 和 DryIoC 为 Controller 注册拦截器?
  5. 这里我不会复制CompositionRoot类;它很无聊、冗长且无关紧要。

获得控制器属性注入和控制器方法拦截工作的任何想法?

【问题讨论】:

  • DryIoC.Microsoft.DependencyInjection 1.2.2 Castle.Core 4.0.0
  • 您是否在配置的服务集合上指定了AddControllersAsServices()
  • @dadhi AddControllersAsServices() 成功了,属性注入和拦截现在正在起作用。非常感谢。
  • 我已将属性注入示例添加到示例 Startup.cs (bitbucket.org/dadhi/dryioc/src/…) 中。您可以检查 LoggingController

标签: c# asp.net-core-mvc interceptor dryioc property-injection


【解决方案1】:

请在配置的服务集合上指定AddControllersAsServices()This 解释了原因。

Sample 已经包含了这个,以及属性注入示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多