Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

How to build a rest component with very long process

// One way is to build 3 types of classes
// 1. controller which sends to the 
// 2. A class that implements a sender and receiver of messages (messageTransfer)
// 3. A worker that recieves from messageTranfer on a loop (best to use with the infinite loop a cancellation token)
// Response could be in some DB where service will get from it

//Class one:
namespace Example.Controllers
{
    [Route("/")]
    [ApiController]
    public class ExampleController : ControllerBase
    {
        readonly IRequestSender _requestSender;

        public PurgeController(IRequestSender requestSender)
        {
            _requestSender = requestSender;
        }

        [HttpPost("example")]
        public async Task<IActionResult> Example()
        {
        	//get or create a decription DTO
        	_requestSender.Send(descDTO);
        }
      }
 }
 
 namespace Example.Impl
{
    public class MessageTransfer : IRequestProvider, IRequestSender
    {
        private readonly IStopTokenProvider _stopTokenProvider;
        //Some dataModel (queue, dict)
        
        public MessageTransfer(IStopTokenProvider stopTokenProvider)
        {
            _stopTokenProvider = stopTokenProvider;
            Task.Run(() => MessageTransferRunnerAsync());
        }
        
        private async Task MessageTransferRunnerAsync()
        {
        	//loop for keeping DataModel alice
        }
	}
    
    public bool Send(DescDTO descDTO)
        {
            if(!_testsDict.TryAdd(descDTO.Type, 1)) //check if you are not handling same handle
            	return false;
            _queue.Enqueue(descDTO);
            return true;
        }

        public DescDTO GetNextAsync(CancellationToken cancellationToken)
        {
            _queue.TryDequeue(descDTO);
        }
 }
 
 namespace Example.Impl
{
    public class Service
    {
    	readonly IRequestProvider _requestProvider;

        public Service(IRequestProvider requestProvider)
        {
            _requestProvider = requestProvider;
            Task.Run(() => RunnerAsync());
        }
        
        private async Task RunnerAsync()
        {
        	//loop and every a few seconds: _requestProvider.TryDequeue()
        }
    }
}
 
 
Comment

PREVIOUS NEXT
Code Example
Csharp :: if session is not active then redirect to login page mvc.net 
Csharp :: how to make diagonal movement not double the speed of the player in unity 
Csharp :: iserviceprovider vs iservicecollection 
Csharp :: hide component in component menu 
Csharp :: csharp-for-loop 
Csharp :: lock a cache in asp.net 
Csharp :: nuget package TSETMC guide 
Csharp :: blazor conditional reenreing 
Csharp :: Bedingungen in C# – if, else und else if 
Csharp :: C# multiple button click event to textbox 
Csharp :: get one parameter from list in an new list c# 
Csharp :: how to change the color of a single line of code in c# 
Csharp :: HOW TO CALL AN EXTENSION METHOD FOR VIEW C# 
Csharp :: unity editorwindowtitle obsolete 
Csharp :: C# Convert range 
Csharp :: c# datafield change cell background color 
Csharp :: unity ar scale 
Csharp :: how to detach the camera from the player after death unity 
Csharp :: how to get scene color to work with urp unity 
Csharp :: C# assign integer 
Csharp :: c# propertyinfo indexof 
Csharp :: C# Move Camera Over Terrain Using Touch Input In Unity 3D 
Csharp :: jittery movement unity 
Csharp :: xunit setup throw exception 
Csharp :: unity get object position on screen 
Csharp :: c# read file while writing 
Csharp :: show a message box in c# 
Csharp :: c# read single key 
Csharp :: Console.WriteLine($"Hello {Ana.ToUpper($)}!"); 
Csharp :: GetNetworkTime 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =