【问题标题】:How to pass parameters to an implementation of IEventProcessor如何将参数传递给 IEventProcessor 的实现
【发布时间】:2015-10-27 15:18:47
【问题描述】:

我正忙于为 azure EventBus 客户端实现 EventProcessorHost 客户端。

我有一个实现IEventProcessor 的类,如下所示:

 public class MyEventProcessor : IEventProcessor
    {
        Stopwatch checkpointStopWatch;            

        //TODO: get provider id from 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, 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();
            }
        }
    }

然后我这样称呼它:

 EventProcessorHost _eventProcessorHost = new EventProcessorHost(eventProcessorHostName, EndpointName, EventHubConsumerGroup.DefaultGroupName, ConnectionString, storageConnectionString, "messages-events");
 await _eventProcessorHost.RegisterEventProcessorAsync<MyEventProcessor>();

我需要将一个参数传递给EventProcessorHost 创建的MyEventProcessor 实例。我该怎么做呢?

【问题讨论】:

    标签: c# azure azure-eventhub event-processor-host


    【解决方案1】:

    您只需要使用RegisterEventProcessorFactoryAsync 来传递factory instance。该工厂类可以传入工厂方法中合适的任何参数,可能首先将它们传递给工厂,或者让工厂改变行为。在下面草绘的代码中,您可以看到两个参数被传递到IEventProcessor。其中一个来自工厂的参数,另一个是工厂被调用次数的计数器。

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

    【讨论】:

    • 这是要走的路!
    【解决方案2】:

    可以尝试使用如下参数对 MyEventProcessor 类进行构造函数依赖注入。

         public class MyEventProcessor : IEventProcessor
        {
            Stopwatch checkpointStopWatch;            
    
            //TODO: get provider id from parent class     
        IParameters _parameter;
        public MyEventProcessor (IParameters param)
        {
          this._parameter  = param;
         }
    
            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();
                }
            }.....
    

    使用 _parameter 检索您需要的内容。

    以下是您如何为您的 IParameters

    注册依赖项

    这里我使用 Ninject 依赖解析器。

    //Bind the class that implements IParameter.
     var parameters = new Parameter();
    paramters.Property = "my data"
    
     kernel.Bind<IParameters>().ToConstant(parameters);
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-09
      • 2011-10-07
      • 2015-01-09
      • 2015-07-25
      • 2013-11-07
      • 2018-09-20
      • 2020-10-12
      • 2021-12-20
      相关资源
      最近更新 更多