Mqtt Connection Error in .Net Client

I’m connecting via wss/8084 from my Angular 14 web client, however I’m no longer able to connect via the .Net client. I keep getting WebSocket Connection Exceptions in C#.

 Exception thrown: 'MQTTnet.Adapter.MqttConnectingFailedException' in MQTTnet.dll

I’m checking the .Net samples here: MQTT-Client-Examples/Program.cs at master · emqx/MQTT-Client-Examples · GitHub

 public async void StartPublisher()
	{
		var mqttFactory = new MqttFactory();
		var tlsOptions = new MqttClientTlsOptions
		{
			UseTls = MqttUseTls,
			IgnoreCertificateChainErrors = true,
			IgnoreCertificateRevocationErrors = true,
			AllowUntrustedCertificates = true
		};
		var managedClientOptions = WsMqttClientOptions();          
		this.managedMqttClientPublisher = mqttFactory.CreateManagedMqttClient();
			   
	await this.managedMqttClientPublisher.StartAsync(managedClientOptions);
				   
	Publish(defaultMessage, topicPubToHar);    // *** PUBLISH ***
}

public ManagedMqttClientOptions WsMqttClientOptions()
      {
        // secure wss url = "a37d0ae6.ala.us-east-1.emqxsl.com:8084/mqtt"
        var url = $"{mqttBrokerIpAddress}:{mqttPort}/mqtt";
        var caFile = "broker.emqx.io-ca.crt";

        // certFilePath = "C:\\bob\\dev\\mqttNotifications\\mqttNotificationsService\\bin\\x64\\Debug\\broker.emqx.io-ca.crt";
        string path = Path.GetDirectoryName(
                 Assembly.GetAssembly(typeof(MqttService)).CodeBase);
        var certFilePath = Path.Combine(path, caFile).Remove(0, 6); 
        var clientOptionsBldr = new MqttClientOptionsBuilder()
              .WithWebSocketServer(url)
              .WithCredentials(mqttClientUser, mqttClientPswd)
              .WithClientId(clientId)
              .WithCleanSession()
              .WithCredentials(mqttClientUser, mqttClientPswd)
              .WithTls(
                  new MqttClientOptionsBuilderTlsParameters()
                  {
                      UseTls = true,
                      SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
                      Certificates = new List<X509Certificate>()
                                  {
                                      // Download from https://www.emqx.com/en/mqtt/public-mqtt5-broker
                                      X509Certificate.CreateFromCertFile(certFilePath)
                                  }
                  }
                  );
        ManagedMqttClientOptions managedClientOptions =
                new ManagedMqttClientOptionsBuilder()
                              .WithClientOptions(clientOptionsBldr)
                              .Build();
        return managedClientOptions;
    }

 // PUBLISH MESSAGE
public async void Publish(string message, string topic)
{
	message += " " + DateTime.Now.ToString();
	var payload = Encoding.UTF8.GetBytes(message);
	var send = new MqttApplicationMessageBuilder()
		.WithTopic(topic)
		.WithPayload(payload)
		.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
		.WithRetainFlag(false)
		.Build();
	if (this.managedMqttClientPublisher != null)
	{
		await this.managedMqttClientPublisher.EnqueueAsync(send);
	}
}

Hello,
It appear to be working now. I can now see these published messages printing in your dashboard client tool. Gonna try now to subscribe with the TLS options.

QUESTION: Do you know if this code supports a .pfx certificate ? Or do I need to convert it to .crt format ?