简体   繁体   中英

How do I subscribe to and raise an event within the same post method of my controller?

When an instance of my model MyPost is posted and it has a property with a certain value, eg Type = "Question" , I want to save the Question , subscribe to an event NoMoreAnswers and wait for a NoMoreAnswers event before returning the HTTP Response which will be a collection of 0 or more MyPost of Type = "Answer" . Each MyPost will have a unique Id.

When additional instance of MyPost come in with a Type of Answer they also need to be saved. Each Answer will also have its own unique id and a RefId pointing back to one of the Questions.

Finally when an instance of MyPost of Type = "NoMoreAnswers" is posted, also with its own unique id and with a RefId pointing back to one of the Questions, I want to raise the event NoMoreAnswers . Optionally, the MyPost instance with Type = "NoMoreAnswers" should also be saved. This raised event should cause the waiting Question to collect all the related Answers and return them as a collection of MyPosts . This will obviously mean that the eventargs will need to contain the RefId of the NoMoreAnswers post, and every waiting Question will have to examine the eventargs to see if the event is relevant to it, and if not go back to a waiting state.

Can someone help me get started? How does one define, subscribe to and raise events that are relevant in this situation? Is there a pattern that applies to this kind of problem that I should read up on?

If I right understand you, you need to use Session as a context for keeping and operating intermediate data between requests. See HttpContext Session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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