Refactor platform detection for unsupported features.

This commit is contained in:
Christian
2020-11-21 15:43:32 +01:00
parent 01bb09ff4b
commit 7738c5faa1
3 changed files with 18 additions and 13 deletions
@@ -141,7 +141,7 @@ namespace MQTTnet.Implementations
if (options.TlsOptions.Certificates.Count > 1) if (options.TlsOptions.Certificates.Count > 1)
{ {
throw new NotSupportedException("Only one client certificate is supported for UWP."); throw new NotSupportedException("Only one client certificate is supported when using 'uap10.0'.");
} }
return new Certificate(options.TlsOptions.Certificates.First().AsBuffer()); return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
@@ -48,7 +48,7 @@ namespace MQTTnet.Implementations
if (options.TlsEndpointOptions.IsEnabled) if (options.TlsEndpointOptions.IsEnabled)
{ {
throw new NotSupportedException("TLS servers are not supported for UWP apps."); throw new NotSupportedException("TLS servers are not supported when using 'uap10.0'.");
} }
} }
@@ -156,10 +156,20 @@ namespace MQTTnet.Implementations
} }
var certificateValidationHandler = _options.TlsOptions?.CertificateValidationHandler; var certificateValidationHandler = _options.TlsOptions?.CertificateValidationHandler;
#if NETSTANDARD2_1
if (certificateValidationHandler != null) if (certificateValidationHandler != null)
{ {
clientWebSocket.Options.RemoteCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) => #if NETSTANDARD1_3
throw new NotSupportedException("Remote certificate validation callback is not supported when using 'netstandard1.3'.");
#elif NETSTANDARD2_0
throw new NotSupportedException("Remote certificate validation callback is not supported when using 'netstandard2.0'.");
#elif WINDOWS_UWP
throw new NotSupportedException("Remote certificate validation callback is not supported when using 'uap10.0'.");
#elif NET452
throw new NotSupportedException("Remote certificate validation callback is not supported when using 'net452'.");
#elif NET461
throw new NotSupportedException("Remote certificate validation callback is not supported when using 'net461'.");
#else
clientWebSocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{ {
// TODO: Find a way to add client options to same callback. Problem is that they have a different type. // TODO: Find a way to add client options to same callback. Problem is that they have a different type.
var context = new MqttClientCertificateValidationCallbackContext var context = new MqttClientCertificateValidationCallbackContext
@@ -171,14 +181,9 @@ namespace MQTTnet.Implementations
}; };
return certificateValidationHandler(context); return certificateValidationHandler(context);
}); };
}
#else
if (certificateValidationHandler != null)
{
throw new NotSupportedException("The remote certificate validation callback for Web Sockets is only supported for netstandard 2.1+");
}
#endif #endif
}
} }
void Cleanup() void Cleanup()
@@ -207,9 +212,9 @@ namespace MQTTnet.Implementations
} }
#if WINDOWS_UWP #if WINDOWS_UWP
throw new NotSupportedException("Proxies are not supported in UWP."); throw new NotSupportedException("Proxies are not supported when using 'uap10.0'.");
#elif NETSTANDARD1_3 #elif NETSTANDARD1_3
throw new NotSupportedException("Proxies are not supported in netstandard 1.3."); throw new NotSupportedException("Proxies are not supported when using 'netstandard 1.3'.");
#else #else
var proxyUri = new Uri(_options.ProxyOptions.Address); var proxyUri = new Uri(_options.ProxyOptions.Address);