OPC UA ServiceResultException(BadSecureChannelClosed) 从服务器更新端点

OPC UA ServiceResultException(BadSecureChannelClosed) updating endpoint from server

我的 C# 应用程序使用 OPC UA 核心堆栈作为客户端连接作为服务器的 PLC 运行。我没有使用 OPC UA SDK,因为应用程序是 WPF,SDK 看不懂。

我查阅了各种参考资料(包括 OPC UA SDK 和 Converter Systems LLC WPF 工具包)并拼凑了我自己的方法来配置 ITransportChannel 对象以传递给 Opc.UaSessionClient构造函数。

我面临的问题是,为了获得与服务器预期值相匹配的端点描述和配置,我使用了 ConfiguredEndpoint.UpdateFromServer,但这会抛出 ServiceResultException(BadSecureChannelClosed) DiscoveryClient 对象已关闭。

我没有看到 SDK 附带的 SimpleOpClient 项目报告了这个异常(这一点都不简单)。

知道这个函数有什么问题吗?

    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 结果?如果是这样,为什么?或者它是堆栈抛出的众多内部异常之一?

首先,您的代码运行良好。即使抛出该异常,它也会更新端点。每当库关闭套接字时,它都是 opc.ua.core 库的处理异常。如果您在 "Exception Settings" window.

中选择了 "All Common Language Runtime Exceptions not in this list",您会在调试器中看到它

其次,对于我们这些在 UA-.NET you will be happy to know that the Session.Create() constructor will update endpoints automatically, if the parameter is true. See this post for an example.

使用 OPC 基金会代码的人

此外,既然您找到了我原来的 WPF 工具包,我想邀请您使用我在 GitHub 上托管的新的和改进的 WPF and UAP Toolkit