How to maintain state or queue of requests in Web API

后端 未结 6 1994
清歌不尽
清歌不尽 2020-12-13 07:39

I have situation, where I have to receive requests in a Web API method, queue those request and then send the bulk to a database (Solr instance).

I am not really su

6条回答
  •  生来不讨喜
    2020-12-13 08:13

    you should try to implement NServiceBus have the ability to schedule messages and send messages in the future, from the service bus documentation you can scheduling capability you can schedule a task or an action/lambda, to be executed repeatedly in a given interval.

    that means that you can have a memeory cache and write the content of the array into your solr/lucene impl every 10 minutes for example, thats something as easier as:

    Schedule.Every(TimeSpan.FromMinutes(10)).Action(() => { < task to be executed > })
    

    In the case that you will need more flexibility for setting a schedueler you can integrate it quartz.net

    the case should be the following:

    • WCF as windows service and NServiceBus should share the same context or implement a cacheManager that can be shared between those two different parts of your system.
    • Every time you do a request you call your wcf pass the parameters and whitin your wcf you add an in memory row to the array (it could be a string array with the same json value you are writing to disk)
    • ServiceBus will handle to manage the queues for the operations into the array and avoid any collisions for the array operations for example:

      • add items to the array
      • empty the array
      • write to your database

提交回复
热议问题