自定义事件未在EventHandler .NET CORE中引发

时间:2018-09-14 06:45:37

标签: .net-core eventhandler

使用.net核心,我正在实现类库以将大量电子邮件推送到电子邮件服务器中。电子邮件服务器将消耗我的电子邮件列表并在完成后提供反馈。这将需要20-30秒。当我从电子邮件服务器获得反馈时,需要开火的方法。

我已经引用this article来实现事件处理程序。但是当我调试它时

EventHandler OnFeedbackReceived

参数为空。请参见下图 enter image description here

这是类库代码。

using System;
using System.Text;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Collections.Generic;


namespace OSH_EmailServerLibrary
{
    public static class EmailMQServerLibrary
    {        
         public static event EventHandler<EmailFeedbackEventArgs> OnFeedbackReceived;

        public static void PushToMQ(List<EmailMessage> _emailList)
        {           
        
        //
        // Long RabbitMQ msg push code here
        //             
        
         EmailMessageFeedback feedback = new EmailMessageFeedback { Description = "Completed", SuccessCount = 10, FailedCount = 0 };
         SendFeedback(feedback);
         Console.ReadLine();
        }
       
        private static EmailMessageFeedback SendFeedback(EmailMessageFeedback feedback)
        {
            if (OnFeedbackReceived != null)
            {
                OnFeedbackReceived(feedback, new 
                EmailFeedbackEventArgs(feedback));
            }
            return feedback;
        }
    }

    public class EmailFeedbackEventArgs : EventArgs
    {
        public EmailFeedbackEventArgs(EmailMessageFeedback _feedback)
        {
            feedback = _feedback;
        }
        public EmailMessageFeedback feedback { get; set; }
    }
}

这就是我在控制台应用程序中使用它的方式

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using OSH_EmailServerLibrary;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace EmailServerSenderSampleConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            List<EmailMessage> _emailList = _emails.GetAllEmailToSend();
            
            EmailMQServerLibrary.PushToMQ(_emailList);
            EmailMQServerLibrary.OnFeedbackReceived += EmailMQServerLibrary_OnFeedbackReceived;

        } 

        private static void EmailMQServerLibrary_OnFeedbackReceived(object sender, EmailFeedbackEventArgs e)
        {
          
        }    
    }
}

1 个答案:

答案 0 :(得分:0)

最后解决了我的问题。问题不在我的类库中。问题是我在控制台应用程序中的PushToMQ()之后确实订阅了事件,这是错误的。.非常感谢 @Hans Passant 我出来。

这是我的正确答案。我认为这会帮助像我这样的人。

类库>>

using System;
using System.Text;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Collections.Generic;


namespace OSH_EmailServerLibrary
{
    public static class EmailMQServerLibrary
    {        
         public static event EventHandler<EmailFeedbackEventArgs> OnFeedbackReceived;

        public static void PushToMQ(List<EmailMessage> _emailList)
        {           
        
        //
        // Long RabbitMQ msg push code here
        //             
        
         EmailMessageFeedback feedback = new EmailMessageFeedback { Description = "Completed", SuccessCount = 10, FailedCount = 0 };
         SendFeedback(feedback);
         Console.ReadLine();
        }
       
        private static EmailMessageFeedback SendFeedback(EmailMessageFeedback feedback)
        {
            if (OnFeedbackReceived != null)
            {
                OnFeedbackReceived(feedback, new 
                EmailFeedbackEventArgs(feedback));
            }
            return feedback;
        }
    }

    public class EmailFeedbackEventArgs : EventArgs
    {
        public EmailFeedbackEventArgs(EmailMessageFeedback _feedback)
        {
            feedback = _feedback;
        }
        public EmailMessageFeedback feedback { get; set; }
    }
}

控制台应用程序>>

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using OSH_EmailServerLibrary;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace EmailServerSenderSampleConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            List<EmailMessage> _emailList = _emails.GetAllEmailToSend();
            
             EmailMQServerLibrary.OnFeedbackReceived +=
             EmailMQServerLibrary_OnFeedbackReceived; //-- worked
             EmailMQServerLibrary.PushToMQ(_emailList);
             //EmailMQServerLibrary.OnFeedbackReceived +=
             EmailMQServerLibrary_OnFeedbackReceived; -- Not working like this

        } 

        private static void EmailMQServerLibrary_OnFeedbackReceived(object sender, EmailFeedbackEventArgs e)
        {
          
        }    
    }
}