3

I'm using Autofac and have a QuizManager which has an event. The QuizManager is responsible for creating and handling different quizes. The lifetime of this class is transient, so it will have several instances.

public class QuizManager : IQuizManager
{
    // Other methods...

    public event Action<string> OnQuizStarted;
}

Every time one instance of this class is resolved I want another class to subscribe to it's event. This is because I have "chained" logic that should occur every time a quiz is started. In particular it should send an event to a SignalR hub. I do not want this logic in my QuizManager. For other reasons I cannot register QuizManager as a singleton. QuizManager is injected in constructor at several other classes.

Below is a class that subscribes to the event in QuizManager.

public class QuizHubInvoker
{
    public QuizHubInvoker(IQuizManager quizManager)
    {
        quizManager.OnQuizStarted += OnQuizStarted;
    }

    private void OnQuizStarted(string quizToken)
    {
        // Do logic here every time event is invoked.
        // It actually invokes a method in a SignalR Hub.
    }
}

Is there a way I can subscribe to an event of a class, which has a transient lifetime?

smoksnes
  • 10,509
  • 4
  • 49
  • 74

0 Answers0