我的 C# 应用程序使用 OPC UA 核心堆栈作为客户端来连接作为服务器运行的 PLC。我没有使用 OPC UA SDK,因为应用程序是 WPF 并且 SDK 难以理解。
我查阅了各种参考资料(包括 OPC UA SDK 和Converter Systems LLC WPF 工具包),并拼凑了我自己的方法来配置ITransportChannel
要传递给Opc.UaSessionClient
构造函数的对象。
我面临的问题是,为了获得与服务器预期值匹配的端点描述和配置,我使用了ConfiguredEndpoint.UpdateFromServer
,但是ServiceResultException(BadSecureChannelClosed)
当它的DiscoveryClient
对象关闭时会抛出一个。
我没有看到SimpleOpClient
SDK 中包含的项目报告了这个异常(这绝不是简单的)。
任何想法这个功能可能有什么问题?
private static ITransportChannel CreateTransportChannel(
ApplicationConfiguration appConfig,
String discoveryUrl)
{
// parse the selected URL.
Uri uri = new Uri(discoveryUrl);
EndpointDescription endpointDescription = new EndpointDescription
{
EndpointUrl = uri.ToString(),
SecurityMode = MessageSecurityMode.None,
SecurityPolicyUri = "http://opcfoundation.org/UA/SecurityPolicy#None"
};
// Configure the endpoint.
ServiceMessageContext messageContext = appConfig.CreateMessageContext();
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(appConfig);
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
// The server may require that the endpoint configuration be adjusted
// to match its own settings.
if (endpoint.UpdateBeforeConnect)
{
// Update endpoint description using the discovery endpoint.
// EXCEPTION thrown during this call.
endpoint.UpdateFromServer(BindingFactory.Create(appConfig, messageContext));
endpointDescription = endpoint.Description;
endpointConfiguration = endpoint.Configuration;
}
// Sanity check for the presence of required security certificates.
X509Certificate2 clientCertificate = null;
if (endpointDescription.SecurityPolicyUri != SecurityPolicies.None)
{
if (appConfig.SecurityConfiguration.ApplicationCertificate == null)
{
Utils.Trace("ApplicationCertificate missing from Configuration.");
throw ServiceResultException.Create(StatusCodes.BadConfigurationError,
"ApplicationCertificate must be specified.");
}
clientCertificate = appConfig.SecurityConfiguration.ApplicationCertificate.Find(true);
if (clientCertificate == null)
{
Utils.Trace("ApplicationCertificate file could not be found.");
throw ServiceResultException.Create(StatusCodes.BadConfigurationError,
"ApplicationCertificate cannot be found.");
}
}
// Create a transport channel.
return SessionChannel.Create(
appConfig,
endpointDescription,
endpointConfiguration,
clientCertificate,
messageContext);
}
是服务器返回BadSecureChannelClosed
结果吗?如果是这样,为什么?或者它是堆栈抛出的众多内部异常之一?