Search code examples
c#azuremessagenservicebus

Nservice message field issue


I have implemented Voice call in my code using .net with NServiceBus version 7. Below is the code snippet to send voice call:

public Task Handle(AddServiceAuto message, IMessageHandlerContext context)
{
    try
    {
        string VoiceCallCode = null;
        Guid userID = User.userID;
        VoiceCallCode = GetVoiceCallCode(userID);
       if (VoiceCallCode != null)
        {
            publishAddVoiceCallEvent(context, user.caseID, userID.Mobile, 
                userID.Voicecall, VoiceMessageText, VoiceCallCode);
        }
    }
}

private void publishAddVoiceCallEvent(IMessageHandlerContext context, 
    Guid caseID, string mobile, bool voicecall, 
    string voiceMessageText, string voiceCallCode)
{
    AddVoiceCallEvent addVoiceCallEvent = new AddVoiceCallEvent()
    {
        CaseID = caseID,
        Mobile = mobile,
        Voicecall = voicecall,
        VoiceMessageText = voiceMessageText,
        VoiceCallCode = voiceCallCode
    };

    context.Publish(addVoiceCallEvent).ConfigureAwait(false);
}

public Task Handle(AddVoiceCallEvent message, IMessageHandlerContext context)
{
    try
    {
        Logger.InfoFormat("message.CaseID: {0}", message.CaseID);

        Logger.InfoFormat("message.Voicecall= {0}", message.Voicecall);
        Logger.InfoFormat("message.Mobile {0}", message.Mobile);
        Logger.InfoFormat("message.VoiceCallCode {0}", message.VoiceCallCode);
        // The user should satisfy below conditions in order to receive a voice call. 

        if ((message.Voicecall) && !string.IsNullOrEmpty(message.Mobile) && 
            !string.IsNullOrEmpty(message.VoiceMessageText) && 
            !string.IsNullOrEmpty(message.VoiceCallCode))
        {
            Voicecall(message.Mobile, message.Voicecall, 
                message.VoiceMessageText, message.VoiceCallCode);
        }
        else
        {
            Logger.Error("Mobile Value is Empty (OR) Voicecall is False (OR) 
                + VoiceMessageText is Empty (OR) VoiceCallCode is Empty");
        }
    }
}

If condition satisfied it will send voice call, else it will print log.

Problem: The Voice call is random i.e. sometimes user is receiving voice call and sometimes not(even though with same settings i.e mobile, VoiceCallCode values stored properly in DB and Voicecall is also true) and the Strange part is, though the values are stored correctly DB, when we look into the logs that we are printing, it shows the value of Mobile, VoiceCallCode is null and Voicecall is false. Again after 5 mins I tried, it worked.

One more thing is, when voice call is not working.

Logger.InfoFormat("message.CaseID: {0}", message.CaseID); // CaseID printed

For Below, data is not printing even though data is there in available in DB (i.e. printing as null)

Logger.InfoFormat("message.Voicecall= {0}", message.Voicecall);
Logger.InfoFormat("message.Mobile {0}", message.Mobile);
Logger.InfoFormat("message.VoiceCallCode {0}", message.VoiceCallCode);

Strange is that, for CaseID it printed while for others it is not printing.

Why this is happening? Can someone please help on this?


Solution

  • The code you've shared doesn't seem to be a running code (try w/o catch) therefore it would be hard to pinpoint what contributes to the issue. But the random behaviour could be attributed to improper use of async APIs. The handler methods should return a Task or use async/await. So are operations invoked on IMessageHandlerContext.

    For example, publishAddVoiceCallEvent should be returning a Task and not void. The code inside it (context.Publish(addVoiceCallEvent).ConfigureAwait(false);) should be either return context.Publish(addVoiceCallEvent); or await context.Publish(addVoiceCallEvent).ConfigureAwait(false);.

    NServiceBus comes with a Rozlyn analyzer to help with these issues.