【发布时间】: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