c# - How to pass parameters to an implementation of IEventProcessor -
i busy implementing eventprocessorhost
client azure eventbus client.
i have class implements ieventprocessor
follows:
public class myeventprocessor : ieventprocessor { stopwatch checkpointstopwatch; //todo: provider id parent class public async task closeasync(partitioncontext context, closereason reason) { debug.writeline("processor shutting down. partition '{0}', reason: '{1}'.", context.lease.partitionid, reason); if (reason == closereason.shutdown) { await context.checkpointasync(); } } public task openasync(partitioncontext context) { debug.writeline("simpleeventprocessor initialized. partition: '{0}', offset: '{1}'", context.lease.partitionid, context.lease.offset); eventhandler = new myeventhandler(); this.checkpointstopwatch = new stopwatch(); this.checkpointstopwatch.start(); return task.fromresult<object>(null); } async task ieventprocessor.processeventsasync(partitioncontext context, ienumerable<eventdata> messages) { foreach (eventdata eventdata in messages) { string data = encoding.utf8.getstring(eventdata.getbytes()); debug.writeline(data); } //call checkpoint every 5 minutes, worker can resume processing 5 minutes if restarts. if (this.checkpointstopwatch.elapsed > timespan.fromminutes(5)) { await context.checkpointasync(); this.checkpointstopwatch.restart(); } } }
i call follows:
eventprocessorhost _eventprocessorhost = new eventprocessorhost(eventprocessorhostname, endpointname, eventhubconsumergroup.defaultgroupname, connectionstring, storageconnectionstring, "messages-events"); await _eventprocessorhost.registereventprocessorasync<myeventprocessor>();
i need pass parameter instance of myeventprocessor
eventprocessorhost
creates. how go doing this?
you need use registereventprocessorfactoryasync pass in factory instance. factory class can pass in whatever parameters appropriate in factory method possibly passing them factory in first place, or having factory vary behavior. in code sketched out below can see 2 parameters being passed ieventprocessor. 1 of them factory's parameters , other counter of how many times factory has been called.
class azurestreamprocessor : ieventprocessor { .... } class azurestreamprocessorfactory : ieventprocessorfactory { public azurestreamprocessorfactory(string str) { this.randomstring = str; } private string randomstring; private int numcreated = 0; ieventprocessor ieventprocessorfactory.createeventprocessor(partitioncontext context) { return new azurestreamprocessor(context, randomstring, interlocked.increment(ref numcreated)); } } host.registereventprocessorfactoryasync(new azurestreamprocessorfactory("a parameter"), options);
Comments
Post a Comment