代码之家  ›  专栏  ›  技术社区  ›  Evil Dog Pie

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

  •  0
  • Evil Dog Pie  · 技术社区  · 10 年前

    我的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 后果如果是,为什么?还是堆栈抛出的众多内部异常之一?

    1 回复  |  直到 10 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

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

    其次,对于我们这些使用OPC基础代码的人来说 UA-.NET 你会很高兴知道这次会议。如果参数为true,Create()构造函数将自动更新端点。请参阅本文中的示例。 OPC UA : minimal code that browses the root node of a server

    另外,既然你找到了我原来的WPF工具包,我想邀请你参加我的新改进版 WPF and UAP Toolkit 托管在GitHub上。