为什么Autofac无法解决此依赖关系?

时间:2016-09-15 08:33:46

标签: c# dependency-injection autofac

我收到的错误消息是

  

2016-09-14 18:55:12,276 INFO   NServiceBus.Unicast.Transport.TransportReceiver - 无法处理   消息Autofac.Core.DependencyResolutionException:无   建设者发现   ' Autofac.Core.Activators.Reflection.DefaultConstructorFinder'在类型上   ' ProjectServices.EsbHandlers.NewProcessMessageHandler'可以调用   使用可用的服务和参数:无法解析参数   ' ProjectServices.Cache.ICachedNotificationsStorage   cachedNotificationsStorage'构造函数' Void   .ctor(Project.Model.IRepository,   Project.Model.Services.ICaseDelegationService,   Priject.Model.Services.IDocumentDelegationService,   ETeismasServices.Cache.ICachedNotificationsStorage)'

依赖关系在Autofac中注册:

builder.RegisterType<CachedNotificationsStorage>().As<ICachedNotificationsStorage>();

这是CachedNotificationStorage构造函数

public class CachedNotificationsStorage : ICachedNotificationsStorage
    {
        private readonly int itemsExpireMinutes;
        private const string NotificationsCacheKey = "Notifications";
        private readonly ObjectCache cache;

        public CachedNotificationsStorage()
        {
            itemsExpireMinutes = int.Parse(ConfigurationManager.AppSettings["notificationsListItemsCahceExpirationInMinutes"]);
            cache = MemoryCache.Default;
            cache.Add(NotificationsCacheKey, new List<CachedNotificationsListItem>(), new CacheItemPolicy());
        }
        //.... logic etc
    }

因此Autofac正在尝试创建NewProcessMessageHandler 为此,Autofac需要解决ICachedNotificationStorage的实施 注册为ICachedNotificationStorage的{​​{1}}具体实现。

为什么Autofac无法解决它? 在该构造函数中没有魔法,它只是将一个初始的空列表添加到缓存中。

NewProcessMessageHandler类:

ICachedNotificationStorage

及其基础

public class NewProcessMessageHandler : BaseHandler<NewProcessMessage>
    {
        public NewProcessMessageHandler(IRepository repository, ICaseDelegationService caseDelegationService,
            IDocumentDelegationService documentDelegationService, ICachedNotificationsStorage cachedNotificationsStorage)
            : base(repository, caseDelegationService, documentDelegationService, cachedNotificationsStorage)
        {
        }
    }

执行注册的完整代码

public abstract class BaseHandler<T>: IHandleMessages<T>
{
    ...
    protected BaseHandler(IRepository repository, ICaseDelegationService caseDelegationService,
        IDocumentDelegationService documentDelegationService, ICachedNotificationsStorage cachedNotificationsStorage)
    {
        this.repository = repository;
        this.caseDelegationService = caseDelegationService;
        this.documentDelegationService = documentDelegationService;
        this.cachedNotificationsStorage = cachedNotificationsStorage;
    }
...
}

RegisterESBMEssageHanlders():该方法注册了40多个消息处理程序

private void StartNserviceBus()
        {
            SetLoggingLibrary.Log4Net(() => log4net.Config.XmlConfigurator.Configure());

            Configure.Transactions.Enable();
            Configure.Serialization.Xml();

            var builder = Behaviors.IoCServiceBehavior.CreateContainerBuilder();
            builder.RegisterType<Repository>().As<IRepository>()
                   .WithResolvedParameter((c, p) => new Lazy<ISession>(() => SessionFactory.Get().OpenSession()));

            builder.RegisterType<CaseDelegationService>().As<ICaseDelegationService>();
            builder.RegisterType<DocumentDelegationService>().As<IDocumentDelegationService>();
            builder.RegisterType<ChargesServiceStub>().As<IProjectChargesService>();
            builder.RegisterType<CachedNotificationsStorage>().As<ICachedNotificationsStorage>();
            builder.RegisterType<MeowDataOutputServiceClient>().As<IMeowDataOutputService>().AsWcfProxy();

            RegisterESBMessageHandlers(builder);
            container = builder.Build();

            Configure.With(AllAssemblies.Matching("ExternalServices.Contracts.ESB.").And("ProjectService"))
                     .DefineEndpointName("project_inbound")
                     .AutofacBuilder(container)
                     .UseTransport<Msmq>()
                     .MsmqSubscriptionStorage()
                     .DisableTimeoutManager()
                     .PurgeOnStartup(false)
                     .UnicastBus()
                     .ImpersonateSender(false)
                     .LoadMessageHandlers()
                     .DisableTimeoutManager()
#if DEBUG
                     .Log4Net()
#endif
                     .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("ExternalServices.Contracts.ESB"))
                     .CreateBus()
                     .Start(
                         () => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install()
                );
        }

0 个答案:

没有答案