尝试获取类型IBackupTableStorageService,键“”StructureMap库的实例时发生激活错误

问题描述 投票:1回答:1

我正面临着映射问题。我的解决方案如下。

我有两个项目

1. Azure Function (v1) project with Framework:  .Net Framework 4.7.2
2. Class Library               with Framework:  .Net Framework 4.7.2

在第一个项目中,我有一个azure函数,它使用Queue触发并调用我的函数BeginBackup(CancellationToken token,DateTime.Now()),它存在于我的concreate类中。

我从Azure函数调用我的函数如下。

DependencyContext.Instance.Locator.GetInstance<IBackupTableStorageService>().BeginBackup(cancellationToken, SystemTime.UtcNow());

我的DependencyContext.cs类如下。

using ClassLibrary1.BackupTableStorage;
using CommonServiceLocator;
using StructureMap;
using System;
using System.Collections.Generic;

namespace AzureFunctionsProject.Functions
{
   public interface IDependencyContext
   {
        IServiceLocator Locator { get; }
   }

   public class DependencyContext : IDependencyContext
   {
       private static volatile IDependencyContext _instance;
       private static readonly object SyncRoot = new Object();

       public static IDependencyContext Instance
       {
           get
           {
               if (_instance == null)
               {
                   lock (SyncRoot)
                   {
                       if (_instance == null)
                       {
                           _instance = new DependencyContext();
                       }
                   }
               }

               return _instance;
            }
           set => _instance = value;
       }

       public IServiceLocator Locator { get; private set; }

       public DependencyContext()
       {
           Build();
       }

       private void Build()
       {
           var registry = new Registry();//Registry is the class belongs to StructureMap.
           var container = new Container();

           var serviceLocatorProvider = new StructureMapServiceLocator(container);
           ServiceLocator.SetLocatorProvider(() => serviceLocatorProvider);
           Locator = serviceLocatorProvider;

           // register the container itself
           registry.For<IServiceLocator>().Use(ServiceLocator.Current);

           // Apply the registry to the container
           container.Configure(x =>
           {
               x.AddRegistry<Domain.Registry>();
               x.AddRegistry(registry);
           });
       }
   }

   public class StructureMapServiceLocator : ServiceLocatorImplBase
   {
       private IContainer Container { get; set; }

       public StructureMapServiceLocator(IContainer container)
       {
           Container = container;
       }

       protected override object DoGetInstance(Type serviceType, string key)
       {
           if (string.IsNullOrEmpty(key))
           {
               return Container.GetInstance(serviceType);
           }

           return Container.GetInstance(serviceType, key);
       }

       protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
       {
           foreach (object obj in Container.GetAllInstances(serviceType))
           {
               yield return obj;
           }
       }
    }
}

我有另一个Registery类,我已经创建了我的接口与我的具体类的映射。

Registry.cs文件如下。

namespace ClassLibrary1.Domain
{
    public class Registry: StructureMap.Registry
    {
        public Registry()
        {

            // The Localization Service needs to be passed a function to record exceptions
            For<ILocalizationService>().Use<LocalizationService>()
            .Ctor<LocalizationService.LogException>().Is(context => CreateExceptionLogger(context));

            For<ICloudStorageWrapper>().Use<CloudStorageWrapper>();
            For<IBackupTableStorageService>().Use<BackupTableStorageService>();
        }

        private LocalizationService.LogException CreateExceptionLogger(IContext context)
        {
            return (ex, c, m) =>
            {
                var logger = context.GetInstance<ILogicalOperationsLogger>();
            logger.ErrorException(m, ex);
            };
        }
    }
}

同样明智的我有我的接口并尊重具体类,它具有我的方法Begin Backup(CancellationToken token,DateTime.Now())的定义;

我在DependencyContext.cs类中的DoGetInstance方法中遇到异常。

我是IoC和依赖注入的新手,请让我知道我做错了什么,如果有什么需要让问题更清楚请告诉我。

inversion-of-control structuremap common-service-locator
1个回答
0
投票

尝试将IServiceLocator的注册移动到configure操作中。您还可以删除您创建的Registry对象:

   private void Build()
   {
       var container = new Container();

       var serviceLocatorProvider = new StructureMapServiceLocator(container);
       ServiceLocator.SetLocatorProvider(() => serviceLocatorProvider);
       Locator = serviceLocatorProvider;

       // Apply the registry to the container
       container.Configure(x =>
       {
           // register the container itself
           x.For<IServiceLocator>().Use(serviceLocatorProvider);
           x.AddRegistry<Domain.Registry>();
       });
   }
© www.soinside.com 2019 - 2024. All rights reserved.