Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception : Collection has changed while changing it #1

Closed
m4kz opened this issue Oct 26, 2016 · 4 comments
Closed

Exception : Collection has changed while changing it #1

m4kz opened this issue Oct 26, 2016 · 4 comments

Comments

@m4kz
Copy link

m4kz commented Oct 26, 2016

Hi,

Thanks for your work, but there is an issue in EventBus, where you use a for-each to alter a collection.
I have fixed it by using a normal for instead:

    `public void Publish<TEventBase>(TEventBase eventItem) where TEventBase : EventBase
    {
        if (eventItem == null)
            throw new ArgumentNullException("eventItem");

        List<ISubscription> allSubscriptions = new List<ISubscription>();
        lock (SubscriptionsLock)
        {
            if (_subscriptions.ContainsKey(typeof(TEventBase)))
                allSubscriptions = _subscriptions[typeof(TEventBase)];
        }

        for (int listIndex = allSubscriptions.Count - 1; listIndex >= 0; listIndex--)
        {
            try
            {
                allSubscriptions[listIndex].Publish(eventItem);
            }
            catch (Exception exception)
            {
            }
        }
    }`

Regards :)

@m4kz m4kz changed the title Collection has changed while changing it exception Exception : Collection has changed while changing it Oct 26, 2016
@mxjones
Copy link
Owner

mxjones commented Oct 27, 2016

Hi,

Thanks for raising this - I haven't seen this issue come up before, do you have a reliable way/example code to reproduce this so I could see it in action?

Thanks for posting your fix, I'll look at getting that put in & a new Nuget package published!

Cheers

@m4kz
Copy link
Author

m4kz commented Nov 1, 2016

I have wrapped Eventbus in my own class so I don't have to propagate RedBus code in mine.
I think the problem might have been triggered by the lock around.
As a separated question, Whats the license of Redbus?

public void Publish<TEvent, TView>(TEvent myEvent) where TView : IMyView where TEvent : IMyEvent<TView> { lock (eventRegistrationLock) { PayloadEvent<TEvent> payLoadEvent = new PayloadEvent<TEvent>(myEvent); eventBus.Publish(payLoadEvent); } }

@mxjones
Copy link
Owner

mxjones commented Nov 2, 2016

Hi,

Ah ok - I am currently doing the same as you except I am not using a lock, if I get some time I will try and reproduce this issue this week.

Hadn't thought of License - MIT / Feel free to use it however and wherever you want, I will add a license to the repo later on.

Cheers

@mxjones
Copy link
Owner

mxjones commented Oct 24, 2017

Apologies for taking so long on this one - just pushed the fix to master now. New NuGet package is available aswell (1.0.4) - Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants