【问题标题】:Azure - Structuremap. How to pass a dependency to a class that implements IEventProcessor(Event Hub)Azure - 结构图。如何将依赖项传递给实现 IEventProcessor(Event Hub) 的类
【发布时间】:2016-07-09 18:10:18
【问题描述】:

我正在使用 azure 事件中心,我正在尝试弄清楚如何将依赖项传递到 EventProcessor 类中,该类用于以我的工作角色处理事件中心外的事件。该类继承了 .net 接口 IEventProcessor。

我的事件处理器类如下所示。我正在努力使用结构映射通过构造函数注入来注入 OrchestrationService 及其依赖项。

我们将不胜感激地接受任何建议:-)

public class EventHubEventProcessor : IEventProcessor
{
    private readonly IOrchestrationService _orchestrationService;
    private readonly IEventReceiver _eventReceiver;
    IDictionary<string, int> map;
    PartitionContext partitionContext;
    Stopwatch checkpointStopWatch;

    public EventHubEventProcessor(IOrchestrationService orchestrationService)
    {
        _orchestrationService = orchestrationService;
    }


    public Task OpenAsync(PartitionContext context)
    {
        Console.WriteLine(string.Format("SimpleEventProcessor initialize.  Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset));
        this.partitionContext = context;
        this.checkpointStopWatch = new Stopwatch();
        this.checkpointStopWatch.Start();
        return Task.FromResult<object>(null);
    }


    public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> events)
    {
        try
        {
            foreach (EventData eventData in events)
            {
                _orchestrationService.Orchestrate(eventData);
                Console.WriteLine("Processed Event " + eventData.PartitionKey);
            }

            //Call checkpoint every 5 minutes, so that worker can resume processing from the 5 minutes back if it restarts.
            if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5))
            {
                await context.CheckpointAsync();
                this.checkpointStopWatch.Restart();
            }
        }
        catch (Exception exp)
        {
            Console.WriteLine("Error in processing: " + exp.Message);
        }
    }

    public async Task CloseAsync(PartitionContext context, CloseReason reason)
    {
        Console.WriteLine(string.Format("Processor Shuting Down.  Partition '{0}', Reason: '{1}'.", this.partitionContext.Lease.PartitionId, reason.ToString()));
        if (reason == CloseReason.Shutdown)
        {
            await context.CheckpointAsync();
        }
    }

}

}

【问题讨论】:

    标签: c# azure dependency-injection inversion-of-control structuremap


    【解决方案1】:

    也许你需要实现 IEventProcessorFactory:

    https://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.ieventprocessorfactory.aspx

    ...并将其实例传递给 EventProcessorHost.RegisterEventProcessorFactoryAsync():

    https://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.eventprocessorhost.registereventprocessorfactoryasync.aspx

    这样您的工厂就可以处理 StructureMap 魔法并根据需要注入 OrchestrationService。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2021-01-02
      • 2019-07-26
      • 1970-01-01
      • 2016-10-08
      相关资源
      最近更新 更多