1

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

我有两个项目

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

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

我从 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 类,它具有我的接口与我的具体类的映射。

Registery.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);
            };
        }
    }
}

同样,我有我的接口并尊重创建类,该类定义了我的方法BeginBackup(CancellationToken token, DateTime.Now());

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

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

4

1 回答 1

0

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

   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>();
       });
   }
于 2019-03-18T16:43:07.787 回答