0

I have a C# application that connects to a NATS service to send and accept NATS messages. It is working well for some time. However I need to build in some logic that if the NATS service is stopped that I take action in letting the user know that we can't send or receive messages. I looked at examples on the internet and applied it accordingly but when testing I see no response as if the events are not triggered. When I stop the NATS service I am expecting the event to fire and a message to be displayed. Can anyone show me what I am missing?

private void OpenNatsConnections()
    {
        LocalLog("Opening connection to Nats");

        // Setup connection options      
        Options Opts = ConnectionFactory.GetDefaultOptions();
        Opts.Url = appSettings.GetNatsUrl();
        Opts.User = appSettings.NATSUsername;
        Opts.Password = appSettings.NATSPassword;

        Opts.AsyncErrorEventHandler += (sender, args) =>
        {
            LocalLog("AsyncErrorEventHandler");
        };

        Opts.ClosedEventHandler += (sender, args) =>
        {
            LocalLog("ClosedEventHandler");
        };

        Opts.DisconnectedEventHandler += (sender, args) =>
        {
            LocalLog("DisconnectedEventHandler");
        };

        // Open Nats connection, assign event handeler and subscribe to listening channel
        try
        {
            NATSConnection = new ConnectionFactory().CreateConnection(Opts);

            AsyncNATSMessageHandler = (sender, args) =>
            {
                string MessageData = Encoding.ASCII.GetString(args.Message.Data);
                DetermineMessageType(MessageData, args.Message.Reply);
            };                     

            SubscribeToNATS();
            LocalLog("Connected to Nats");
        }
        catch (Exception e)
        {
            LocalLog("Critical!! Connection to NATS failed! Error Message - " + e.Message);
        }
    }
LudwigW
  • 15
  • 3

1 Answers1

0

I have managed to resolve the issue. Quite simple rather. I downloaded the latest version of NATS Client and the above code worked and the relevant code is fired when triggered.

LudwigW
  • 15
  • 3