Это (для меня) довольно странная проблема, потому что она уже работала отлично, но после некоторых несвязанных изменений полностью исчезла.
У меня есть Repository
, который импортирует в свой конструктор список IExtensions
через интеграцию Autofacs MEF. Одно из этих расширений содержит обратную ссылку на Repository
как на Lazy(Of IRepository)
(ленивый из-за циклической ссылки, которая может возникнуть).
Но как только я пытаюсь использовать репозиторий, Autofac выдает ComponentNotRegisteredException
с сообщением. Запрошенная служба «ContractName = Assembly.IRepository ()» не зарегистрирована.
Это, однако, не совсем правильно, потому что, когда я прерываю работу сразу после сборки контейнера и исследую список служб, он там - Exported () и с правильным ContractName.
Буду признателен за любую помощь в этом ...
Майкл
[Edit] Вот урезанная версия кода:
Репозиторий
Public Class DocumentRepository Implements IDocumentRepository Private _extensions As IEnumerable(Of IRepositoryExtension) Public Sub New(ByVal extensions As IEnumerable(Of IRepositoryExtension)) _extensions = extensions End Sub Public Sub AddDocument(ByVal document As Contracts.IDocument) Implements Contracts.IDocumentRepository.AddDocument For Each extension In _extensions extension.OnAdded(document.Id) Next End Sub End Class
Плагин
<Export(GetType(IRepositoryExtension))> <PartCreationPolicy(ComponentModel.Composition.CreationPolicy.Shared)> Public Class PdfGenerator Implements IRepositoryExtension Private _repositoryFactory As Lazy(Of IDocumentRepository) Public Sub New(ByVal repositoryFactory As Lazy(Of IDocumentRepository)) _repositoryFactory = repositoryFactory End Sub Public Sub CreatePdf(ByVal id As Guid) Implements Contracts.IRepositoryExtension.OnAdded Dim document = _repositoryFactory.Value.GetDocumentById(id) End Sub End Class
Бутстраппер
Public Class EditorApplication Inherits System.Web.HttpApplication Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim builder As New ContainerBuilder() Dim catalog1 As New TypeCatalog(GetType(DataRepositoryScheme)) Dim catalog2 As New DirectoryCatalog(HttpContext.Current.Server.MapPath("/Plugins")) builder.RegisterComposablePartCatalog(New AggregateCatalog(catalog1, catalog2)) builder.RegisterType(Of DocumentRepository).As(Of IDocumentRepository).SingleInstance().Exported(Function(x) x.As(Of IDocumentRepository)()) AutofacServiceHostFactory.Container = builder.Build() End Sub End Class