0

I am using azure event hub and I am trying to work out how pass in dependencies into the EventProcessor class used to process events off the event hub in my worker role. This class inherits the .net interface IEventProcessor.

My event processor class is shown below. I am struggling using structure map to inject the OrchestrationService and its dependancies in through constructor injection.

Any suggestions will be gratefully accepted :-)

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();
        }
    }

}

}

macca18
  • 197
  • 1
  • 1
  • 14

1 Answers1

0

Perhaps you need to implement IEventProcessorFactory:

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

...and pass an instance of that into EventProcessorHost.RegisterEventProcessorFactoryAsync():

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

That way your factory can handle the StructureMap magic and inject OrchestrationService as needed.

JoshL
  • 1,706
  • 1
  • 12
  • 13