0

Controller code:

[HttpPost]
[Route("mood")]
public async Task<IActionResult> ProcessMoodProfile([FromBody] MoodTelemetryDTO moodProfile)
{
    string userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                 
    var result = await _moodDataService.ProcessMoodTelemetryData(userId, moodProfile).ConfigureAwait(false);
    return Ok(HttpStatusCode.Created);
}

Data service code:

public async Task<bool> ProcessMoodTelemetryData(string userId, MoodTelemetryDTO moodTelemetry)
{
    try
    {
        var moodProfile = mapper.Map<MoodTelemetryProfile>(moodTelemetry);
        moodProfile.CreatedAt = moodTelemetry.CreatedDate.Date;
        moodProfile.UserId = new Guid(userId);
        return await _sendTelemetryData.SendMessageAsync(TelemetryType.MoodTelemetry, userId, JsonConvert.SerializeObject(moodProfile), new System.Threading.CancellationToken()).ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Unable to process telemetry data. MethodName {methodname}", nameof(ProcessMoodTelemetryData));
        return false;
    }
}

EventHubTelemetrySinkService

public async Task<bool> SendMessageAsync(TelemetryType telemetryType, string partitionKey, string messagejson, CancellationToken cancellationToken)
{
    var status = false;
    _client = _sink[telemetryType];
    if (_client == null)
    {
        _logger.LogError("User telemetry data processes successfully but no sink found for the telemetry type {telemetryType}", telemetryType);
    }

    using (_logger.BeginScope("Send Message"))
    {
        _logger.LogInformation($"Message to be send {messagejson}");
        _logger.LogInformation($"Type of Telemetry {telemetryType}");

        var data = new Microsoft.Azure.EventHubs.EventData(Encoding.UTF8.GetBytes(messagejson));

        try
        {
            await SendMessageInternalAsync(data, partitionKey, cancellationToken).ConfigureAwait(false);
            status = true;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error Sending Message. Method {methodName}", nameof(SendMessageAsync));
        }
        data.Dispose();
    }
    return status;
}

private async Task SendMessageInternalAsync(Microsoft.Azure.EventHubs.EventData messgae, string partitionKey, CancellationToken cancellationToken)
{
    var success = false;
    var attempt = 0;
    using (_logger.BeginScope("Send Message Internal"))
    {
        do
        {
            try
            {
                await _client.SendAsync(messgae, partitionKey).ConfigureAwait(false);
                var response = _client.SendAsync(messgae, partitionKey).ConfigureAwait(false);
                success = true;
            }
            catch (Exception ex)
            {
                if (attempt == _messageRetryCount || cancellationToken.IsCancellationRequested)
                {
                    _logger.LogError(ex, "Internal message retry exceeded or cancellation invoked. method {methodname}", nameof(SendMessageInternalAsync));
                }
            }
        } while (!success && ++attempt <= _messageRetryCount && !cancellationToken.IsCancellationRequested);
    }
}

This code is working earlier, but from few days it stopped working. sendasync function receive all the values and no exception is there, still values are not stored in database.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Shri
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jun 04 '22 at 08:21
  • Hi @Shri, did you set breakpoint on your api to debug your code and check if the api return any error? – Rena Jun 06 '22 at 05:53
  • Hi @Rena , yes, i debug entire code, but don't get any exception – Shri Jun 06 '22 at 06:47
  • I see several places where you're using `.ConfigureAwait(false)`. Why are you doing that? – mason Jun 06 '22 at 17:47
  • Working code that hasn't changed, typically doesn't suddenly just stop working. If you connect to some external resources, maybe something has changed there? Maybe something in the configuration of your backend changed. Impossible to say. But if you changed *nothing*, behaviour won't change either. – derpirscher Jun 06 '22 at 17:48
  • 1
    And why are you calling `_client.SendAsync()` twice, but only awaiting it once? That seems rather strange – derpirscher Jun 06 '22 at 17:53
  • @derpirscher var response = _client.SendAsync(messgae, partitionKey).ConfigureAwait(false); Used above code , just to check response from method – Shri Jun 24 '22 at 09:50

0 Answers0