Great Thanks Luke
Apologies don't have access to upload source, due to company policy.
//Provided sample below.
//Note :Below application is deployed in IIS WEB API
//===========================================================
//Below method is used to push message in queue.
//No issue found in this method
[HttpPost]
public async Task<IActionResult> Any2PDF([FromBody] Any2Pdf request)
{
Log log = new Log();
try
{
string inputJson = JsonConvert.SerializeObject(request);
await _busControl.SendAsync(_queue.Value.Any2PDFQueue, request);
return Ok(new ResponseModel() { IsSuccess = true, Message = string.Empty });
}
catch (Exception ex)
{
log.apiLog("OwnController/Any2PDF " + ex.Message + ex.StackTrace, logPath);
return Ok(new ResponseModel() { IsSuccess = false, Message = ex.ToString() });
}
}
public async Task SendAsync<T>(string queue, T message)
{
await Task.Run(() =>
{
_channel.QueueDeclare(queue, true, false, false);
var properties = _channel.CreateBasicProperties();
properties.Persistent = false; var output = JsonConvert.SerializeObject(message);
_channel.BasicPublish(string.Empty, queue, null,
Encoding.UTF8.GetBytes(output));
});
}
//========================================================================
//Below method is API GET Method :
//Background service will hit this method continously, every 30 seconds
//if data exists in queue it will return.
//Findings: If no data found in queue for long time , Pipelining of requests forbidden exception throws
[HttpGet]
public async Task<IActionResult> Any2PDF()
{
Log log = new Log();
try
{
var result = await _busControl.ReceiveAny2PDF(_queue.Value.Any2PDFQueue);
return Ok(result);
}
catch (Exception ex)
{
log.apiLog("Consumer/Any2PDF " + ex.Message + ex.StackTrace, logPath);
return Ok(new ResponseModel() { IsSuccess = false, Message = ex.ToString() });
}
}
public async Task<Any2Pdf> ReceiveAny2PDF(string queue)
{
try
{
_channel.QueueDeclare(queue, true, false, false); //some cases Pipelining of requests forbidden
//var consumer = new AsyncEventingBasicConsumer(_channel);
//
var count = _channel.MessageCount(queue);
if (count > 0)
{
var queueresult = _channel.BasicGet(queue, true);
var jsonSpecified = Encoding.UTF8.GetString(queueresult.Body.Span);
var result = JsonConvert.DeserializeObject<Any2Pdf>(jsonSpecified);
return result;
}
}
catch (Exception ex)
{
throw ex;
}
return new Any2Pdf();
}
//=======================================================
Regards,
Vinoth E.