0

我的 C# 应用程序使用 OPC UA 核心堆栈作为客户端来连接作为服务器运行的 PLC。我没有使用 OPC UA SDK,因为应用程序是 WPF 并且 SDK 难以理解。

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

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

我没有看到SimpleOpClientSDK 中包含的项目报告了这个异常(这绝不是简单的)。

任何想法这个功能可能有什么问题?

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

4

1 回答 1

2

首先,您的代码工作正常。即使它抛出异常,它也会更新端点。每当库关闭套接字时,它就是 opc.ua.core 库的已处理异常。如果您在“异常设置”窗口中选择了“不在此列表中的所有公共语言运行时异常”,您会在调试器中看到它。

其次,对于我们这些在UA-.NET上使用 OPC 基础代码的人来说,如果参数为 true,您会很高兴知道 Session.Create() 构造函数将自动更新端点。有关示例,请参见这篇文章。OPC UA:浏览服务器根节点的最少代码

此外,由于您找到了我最初的 WPF 工具包,我想邀请您使用我在 GitHub 上托管的新的和改进的WPF 和 UAP 工具包。

于 2016-06-30T15:42:40.347 回答