Subject: Connection fails to receive XML from Worker Thread
We have an application which attempts to initialize a Matrix XMPP listener in a worker thread. When we do this, the client's OnSendXml event fires, but other events do not (in particular, OnReceiveXML and OnLogin).
Initializing the client outside of the worker thread functions as expected, but won't work in production given our overall architecture (we don't know the Password and Username until we are already in the worker thread).
For diagnostic purposes, I've created a minimal application that demonstrates the issue.
From a standard Windows Form Application template (with the MatriX v2.1.0.2 Nuget package installed) , this code works (breakpoints in OnReceiveXml, OnSendXml, and OnLogin are all hit):
But this code fails (breakpoints in OnReceiveXml and OnLogin are never hit):
What would cause the xmppClient to fail in this scenario? What can we do to work around this issue?
Initializing the client outside of the worker thread functions as expected, but won't work in production given our overall architecture (we don't know the Password and Username until we are already in the worker thread).
For diagnostic purposes, I've created a minimal application that demonstrates the issue.
From a standard Windows Form Application template (with the MatriX v2.1.0.2 Nuget package installed) , this code works (breakpoints in OnReceiveXml, OnSendXml, and OnLogin are all hit):
- public partial class Form1 : Form {
- public Form1() {
- InitializeComponent();
- }
- private void OnReceiveXml(object sender, Matrix.TextEventArgs e) {
- Console.WriteLine("ReceiveXml");
- }
- private void OnSendXml(object sender, Matrix.TextEventArgs e) {
- Console.WriteLine("SendXml");
- }
- private void OnLogin(object sender, Matrix.EventArgs e) {
- Console.WriteLine("Login");
- }
- private void button1_Click(object sender, System.EventArgs e) {
- Matrix.License.LicenseManager.SetLicense(System.Configuration.ConfigurationManager.AppSettings["MatrixLicenseKey"].ToString());
- xmppClient.Transport = Matrix.Net.Transport.Bosh;
- xmppClient.PreferredSsoSaslMechanism = Matrix.Xmpp.Sasl.SaslMechanism.Plain;
- xmppClient.AutoPresence = true;
- xmppClient.Password = "xxxxx";
- xmppClient.Username = "xxxxx";
- xmppClient.Port = 7443;
- xmppClient.XmppDomain = originalUri.Host;
- xmppClient.Open();
- }
- }
But this code fails (breakpoints in OnReceiveXml and OnLogin are never hit):
- public partial class Form1 : Form {
- public Form1() {
- InitializeComponent();
- }
- private void OnReceiveXml(object sender, Matrix.TextEventArgs e) {
- Console.WriteLine("ReceiveXml");
- }
- private void OnSendXml(object sender, Matrix.TextEventArgs e) {
- Console.WriteLine("SendXml");
- }
- private void OnLogin(object sender, Matrix.EventArgs e) {
- Console.WriteLine("Login");
- }
- private void button1_Click(object sender, System.EventArgs e) {
- System.Threading.ThreadPool.QueueUserWorkItem((o) => {
- Matrix.License.LicenseManager.SetLicense(System.Configuration.ConfigurationManager.AppSettings["MatrixLicenseKey"].ToString());
- xmppClient.Transport = Matrix.Net.Transport.Bosh;
- xmppClient.PreferredSsoSaslMechanism = Matrix.Xmpp.Sasl.SaslMechanism.Plain;
- xmppClient.AutoPresence = true;
- xmppClient.Password = "xxxxx";
- xmppClient.Username = "xxxxx";
- xmppClient.Port = 7443;
- xmppClient.XmppDomain = originalUri.Host;
- xmppClient.Open();
- }, null);
- }
- }
What would cause the xmppClient to fail in this scenario? What can we do to work around this issue?
This post was edited on 2017-06-26, 23:05 by Alex.