-1

Hi I can't fix the issue in OnLoginSuccess() pubSocket.SendMoreFrame("TopicA").SendFrame("Hello"); variable due to error the name '' does not exist in the current context.

I know that I need to change variable to public but when I add static public PublisherSocket pubSocket = new PublisherSocket(); at the beggining of the class the code doues not work properly. Also, the problem is that I need to set some options (.Options, .Bind) in Main().

using System;
using System.Threading;
using NetMQ;
using NetMQ.Sockets;

namespace Publisher
{
class Program
{

    static public void OnLoginSuccess()
    {
        pubSocket.SendMoreFrame("TopicA").SendFrame("Hello");
    }

    static void Main(string[] args)
    {


        using (var pubSocket = new PublisherSocket())
        {
            pubSocket.Options.SendHighWatermark = 1000;
            pubSocket.Bind("tcp://localhost:12345");             
        }

        OnLoginSuccess();
    }
}
}
sortedMAn
  • 3
  • 2
  • Your pubSocket is only declared in your Main(), not in OnLoginSuccess – Wheels73 May 22 '18 at 11:40
  • It sounds like you need to *declare* the variable as a static field, but you can still *initialize* it in `Main`. (Or potentially change your design so that you pass it to `OnLoginSuccess()` instead.) – Jon Skeet May 22 '18 at 11:41
  • The other issue would be that `pubSocket` would also be disposed by the time `onLoginSuccess` is called. – Evan Trimboli May 22 '18 at 11:42

1 Answers1

0

As discussed, either create a private static variable in the class

private static PublisherSocket  pubSocket;

or pass the socket reference into your OnLoginSuccess method.

 static public void OnLoginSuccess(PublisherSocket socket)
 {
       socket.SendMoreFrame("TopicA").SendFrame("Hello");
 }

As Evan pointed out, your ref would have been disposed of due to the using notation. Remove this and dispose once you've finished with it.

 var pubSocket = new PublisherSocket()
 {
       pubSocket.Options.SendHighWatermark = 1000;
       pubSocket.Bind("tcp://localhost:12345");
 };

 OnLoginSuccess(pubSocket);
 pubSocket.Dispose();

You might want to put your call in a try catch finally and put the dispose in the finally to ensure it is cleaned up correctly.

Hope that helps.

Wheels73
  • 2,850
  • 1
  • 11
  • 20