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

You may only call Receive-methods when constructing the actor and inside Become() #3163

Closed
wbradney opened this issue Oct 18, 2017 · 6 comments

Comments

@wbradney
Copy link
Contributor

wbradney commented Oct 18, 2017

I have an actor (pub sub subscriber) that does something like this:

public class MyActor : ReceiveActor
    {
        private ILoggingAdapter Log { get; } = Context.GetLogger();
        private readonly IActorRef _mediator;

        public MyActor()
        {
            _mediator = DistributedPubSub.Get(Context.System).Mediator;

            _mediator.Tell(new Subscribe("events", Self));

            Become(Subscribing);
        }

        public void Subscribing(object message)
        {
            Receive<SubscribeAck>(ack =>
            {
                Log.Info($"{Self} subscribed to 'events'");
            });
        }
}

This throws an InvalidOperationException at the Receive method. I'm a bit confused about how Become() is supposed to work in light of this exception. Of course, this works:

        public void Subscribing(object message)
        {
            switch (message)
            {
                case SubscribeAck ack:
                    Log.Info($"{Self} subscribed to 'events'");
                    break;
            };
        }

But does this mean Receive is useless inside ReceiveActors that change state?

@Danthar
Copy link
Member

Danthar commented Oct 19, 2017

You are using it wrong.

It should be
public void Subscribing() {}

The fact that the exception is triggered at the Receive method is because the handlers are resolved upon receiving a message. So its only then that your "bug" comes to light.

Either way. Ditch the object message parameter.

@wbradney
Copy link
Contributor Author

Ah, thanks - I missed the redundant parameter.

@robvangeloven
Copy link

Sorry to open something so long dead, but the official documentation (https://getakka.net/articles/actors/receive-actor-api.html) still uses the "object message" as parameter in the examples

@seungyongshim
Copy link

@Aaronontheweb
Copy link
Member

@robvangeloven @seungyongshim sorry about that - I'll submit a PR for it

@Aaronontheweb
Copy link
Member

Should be resolved here: #4958

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

5 participants