1

For example, we have some action and method, on which the event is being subscribed:

    // some response property
    static _response {get; set;}

    // client sends the post request
    [HttpPost]
    public async Task<ActionResult> SomeAction(Model someModel)
    {
        if (someModel != null)
        {
            // here we subscribing on event
            SomeEventHandler.Subscribe(MethodBeingFired)
            // here we must wait for firing the event and check if event has been fired
            if (eventBeenFired) return Json(new { response = _response })
            // or if some wait time's out
             return Json(new { error = "Time for executing your request is out" });
          }
    }

    public void MethodBeingFired(SomeEventResult result)
    {
       // here we have to notify the action in a current thread that event has been fired
       eventBeenFired = true;
      _response = result.Response;
    }

I guess that the code scheme above is pretty clear and understandable. My action should wait some time for capturing the event and return the response or error.

Lab Lab
  • 781
  • 10
  • 34
  • Possible answer here? https://stackoverflow.com/questions/9112305/how-to-asynchronously-wait-for-x-seconds-and-execute-something-then – Matt Aug 23 '19 at 11:23
  • @Matt, yeap, but I'm not sure if this is the best practice – Lab Lab Aug 23 '19 at 11:24
  • If you need to have custom timeout, I think you need to use some kind of a timer either way. – Matt Aug 23 '19 at 11:25
  • @LabLab The design you are proposing is bad for web based asp.net mvc. Read up on SignalR and see how it fits into your design – Nkosi Aug 23 '19 at 11:27
  • @Nkosi, you are right, but it's tightly tied on HTTP requests rather than sockets – Lab Lab Aug 23 '19 at 11:41
  • You'll need to create your own class to waits until the message is received or cancel if its timeout. Events like this aren't very good for web development unless you use websocket that allows the server to contact the client. – the_lotus Aug 23 '19 at 15:49

1 Answers1

0

If I understood correctly, I believe you should delegates & event handling

Moreover, you can;

  • use queues as mentioned here
  • use a message-queueing software like RabbitMQ and you may set your controllers to act like both a publisher and a consumer.