简体   繁体   English

尽管InvocationList包含预订方法C#,但EventHandler并未被调用

[英]EventHandler not being called despite InvocationList containing the subscribing method C#

I am developing a MEF application. 我正在开发MEF应用程序。 I am using a plugin as a publisher and another as a subscriber. 我使用一个插件作为发布者,另一个作为订阅者。 For the current issue I guarantee that both plugin instances are active. 对于当前问题,我保证两个插件实例都处于活动状态。 On the subscriber I subscribe to the event and on the publisher I iterate over the invocation list and call the BeginInvoke to raise the event asynchronously as so: 在订阅者上,我订阅了事件,在发布者上,我遍历了调用列表,并调用BeginInvoke异步引发事件,如下所示:

Publisher: 发布者:

public class BackchannelEventArgs : EventArgs {
    public string Intensity { get; }
    public BackchannelEventArgs(string intensity) {
        this.Intensity = intensity;
    }
}

public class Publisher {
    public event EventHandler<BackchannelEventArgs> BackchannelEvent = delegate { };

    private void BackchannelEventAux(string bcintensity) {
        Plugin.LogDebug("BACKCHANNEL EVENT, sending to " + BackchannelEvent.GetInvocationList().Length + " subscribers: " + bcintensity);

        var args = new BackchannelEventArgs(bcintensity);
        foreach (EventHandler<BackchannelEventArgs> receiver in BackchannelEvent.GetInvocationList()) {
            receiver.BeginInvoke(this, args, null, null);
        }
    }
}

Subscriber (relevant snippet, the Init is being called by a pluginsManager in which I can see the logs): 订阅服务器(相关代码段,Init正在由pluginsManager调用,我可以在其中查看日志):

class Subscriber {

    public void Init(){
        LogInfo("Before subscribing");
        publisher.BackchannelEvent += HandleBackchannelEvent;
        LogInfo("After subscribing");
    }

    private void HandleBackchannelEvent(object sender, BackchannelEventArgs e) {
        LogDebug("Handle Backchannel!");
    }
}

Now, the Log you see on the event handler is not called at all. 现在,根本不会调用您在事件处理程序上看到的日志。 I have 4 other events that follow the same structure and somewhat this event in particular is not being called (I can see the logs on the other events). 我还有其他4个遵循相同结构的事件,并且在某些情况下并未特别调用此事件(我可以查看其他事件的日志)。 The other plugins follow the exact same structure. 其他插件遵循完全相同的结构。

Already tried: 已经尝试过:

  • Call synchronously BackchannelEvent(this, args) but the results are the same; 同步调用BackchannelEvent(this, args)但结果相同;
  • Subscribe this same event on the other plugins as well but the issue remains on this single event (and not on the others who follow the same structure). 也可以在其他插件上订阅此同一事件,但问题仍在此单个事件上(而不是在遵循相同结构的其他事件上)。

I hope you can give me some help on this. 希望您能在这方面给我一些帮助。

Thank you 谢谢

Edit: The shown code is a snippet. 编辑:显示的代码段。 The Init method is being called by the pluginsManager. 插件管理器正在调用Init方法。 I have put a log before the subscribing call and I can confirm that I am indeed subscribing. 我已经在订阅电话之前放置了一个日志,并且可以确认我确实在订阅。

Edit2: The number of elements in the InvocationList is in fact 2 (the empty delegate and the subscriber) so it checks out. Edit2:InvocationList中的元素数实际上为2(空的委托人和订户),因此它签出。

Okay. 好的。 I don't know why but I figured out the solution so that other ones who stumble with the issue can find a solution. 我不知道为什么,但是我想出了解决方案,以便其他迷恋这个问题的人可以找到解决方案。 It was related with a extension I created for the Random class (which wasn't throwing a exception although... so it might be a bug on C#, I can't really explain). 它与我为Random类创建的扩展有关(尽管它不会引发异常,但是...这可能是C#上的错误,我无法真正解释)。 The Random extension is provided by an external NuGet package I created. 随机扩展由我创建的外部NuGet包提供。

Version A (without using the Random Extension): 版本A(不使用随机扩展名):

Body of the EventHandler: EventHandler的主体:

LogDebug("Inside Handler");
double intensityValue2 = GetRandomNumber(Settings.MinimumIntensity, Settings.MaximumIntensity);
double frequency2 = GetRandomNumber(Settings.MinimumFrequency, Settings.MaximumFrequency);
int repetitions2 = GetRandomInt(Settings.MinimuMRepetitions, Settings.MaximumRepetitions);

Version B (using Random extension): 版本B(使用随机扩展名):

Body of EventHandler: EventHandler的主体:

LogDebug("Inside Handler");
double intensityValue2 = random.GetRandomNumber(Settings.MinimumIntensity, Settings.MaximumIntensity);
double frequency2 = random.GetRandomNumber(Settings.MinimumFrequency, Settings.MaximumFrequency);
int repetitions2 = random.GetRandomNumber(Settings.MinimuMRepetitions, Settings.MaximumRepetitions);

Version A is the one that it is working. 版本A是它正在运行的版本。 The Logs are guaranteed to appear. 保证日志出现。 I don't know why it isn't letting me use extensions but it is solved for now. 我不知道为什么它不能让我使用扩展程序,但现在已经解决了。 It would make sense if the Random extension threw an exception but it is not the case... 如果Random扩展抛出异常,那是有道理的,但事实并非如此……

If any other person stumbles upon the issue I hope this helps you figure out the issue faster than me. 如果有其他人偶然发现了这个问题,我希望这可以帮助您比我更快地找到问题。

Thank you 谢谢

Edit: typo 编辑:错别字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM