Hi,
I writing windows form application using Neuron API. I have already setup Topic, Publisher and Subscriber correctly in the "Neuron Esb Explorer" and tested it through test client and it works.
On the form's button click event i am connecting to Publisher, Subscriber and sending xml message (<message>Hello world</message>) through publisher to subcriber, I have already wired up an event handler "MyOnReceive" to subscriber.OnReceive event.
So following is the code i have done, here the button1_Click() works fine, but the MyOnReceive() event handler never triggers.
private void button1_Click(object sender, EventArgs e)
{
using (Publisher publisher = new Publisher("PublishParam"))
{
publisher.Connect();
// Create an instance of a subscriber.
using (Subscriber subscriber = new Subscriber("PublishParam"))
{
subscriber.OnReceive += MyOnReceive;
subscriber.Connect();
// Send an object.
publisher.SendXml("PublishParamTopic", "<message>Hello world</message>");
}
}
}
private/public void MyOnReceive(object sender, MessageEventArgs e)
{
ESBMessage message = e.Message;
string objString = message.GetBody<string>();
Console.WriteLine(objString.ToString());
}
The strange thing is when i move the above piece of code to console appliation () it works. see the follwoing example. Any idea whats going wrong here?
public class Program
{
static void Main(string[] args)
{
using (Publisher publisher = new Publisher("PublishParam"))
{
publisher.Connect();
// Create an instance of a subscriber.
using (Subscriber subscriber = new Subscriber("PublishParam"))
{
subscriber.OnReceive += MyOnReceive;
subscriber.Connect();
// Send an object.
publisher.SendXml("PublishParamTopic", "<message>Hello world</message>");
Console.ReadLine();
}
}
}
private static void MyOnReceive(object sender, MessageEventArgs e)
{
ESBMessage message = e.Message;
string objString = message.GetBody<string>();
Console.WriteLine(objString.ToString());
}
}
Any idea whats going wrong here, I would really appriciate if some one can advise me
Tags:
notice what you are doing though....for the console app, you have a console.readline() which basically prevents the Subscriber object from going out of scope...hence the event fires. For the windows app though...you have no stop point...and hence the subscriber goes out of scope and is disposed....hence the event never fires.
What you should do is just create the subscriber as a module level variable so it never goes out of scope.
Kind regards,
Marty Wasznicky
General Manager, Product Field Operations
Neuron ESB
Neudesic, L.L.C.
Neuron ESB Product Support Forums and Communities
© 2024 Created by Neuron Admin. Powered by