我的C#应用程序使用OPC UA Core堆栈作为客户端,与作为服务器运行的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
后果如果是,为什么?还是堆栈抛出的众多内部异常之一?