You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Opc.Ua.Client 1.1.0连接OPC UA服务器报错求助

Opc.Ua.Client 1.1.0 连接与浏览问题修复方案

问题概述

使用Opc.Ua.Client 1.1.0版本连接OPC UA服务器时,遇到三个核心错误:

  • Session.Create无5参数重载
  • Session.Browse无2参数重载
  • ResponseHeader不包含Results定义

以下是针对1.1.0版本特性的代码修改方案:

关键修改点说明

1. 修复Session.Create方法重载错误

1.1.0版本中,Session.Create要求传入ApplicationConfiguration而非直接的EndpointConfiguration,且需将EndpointDescription转换为ConfiguredEndpoint。需先构建基础的客户端应用配置,再调用正确的重载。

2. 修复Session.Browse方法及响应处理错误

1.1.0版本中,Session.Browse的正确重载是直接接收BrowseRequest并返回BrowseResponse,之前的错误调用导致返回ResponseHeader,因此无法访问Results属性。

修改后的完整代码

using Microsoft.AspNetCore.Mvc;
using Opc.Ua.Client;
using Opc.Ua;

namespace InternshipProject.Controllers
{
    public class OPCConnectionController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult ConnectToOpc(string opcConnectionString)
        {
            try
            {
                var endpointDescription = CoreClientUtils.SelectEndpoint(opcConnectionString, useSecurity: false);
                // 构建1.1.0版本要求的应用配置
                var appConfig = new ApplicationConfiguration
                {
                    ApplicationName = "OPC UA Client",
                    ApplicationType = ApplicationType.Client,
                    SecurityConfiguration = new SecurityConfiguration { AutoAcceptUntrustedCertificates = true },
                    TransportConfigurations = new TransportConfigurationCollection(),
                    TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
                    ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
                };
                appConfig.Validate(ApplicationType.Client).Wait();

                // 将端点描述转换为配置端点
                var configuredEndpoint = new ConfiguredEndpoint(null, endpointDescription, EndpointConfiguration.Create());

                // 使用1.1.0版本支持的Session.Create重载
                using (var session = Session.Create(
                    configuration: appConfig,
                    endpoint: configuredEndpoint,
                    updateBeforeConnect: false,
                    checkDomain: false,
                    sessionName: "OPC Session"))
                {
                    session.ReturnDiagnostics = DiagnosticsMasks.All;
                    // 1.1.0版本中Open方法无需传入ApplicationType
                    session.Open();

                    var channels = BrowseChannels(session);

                    ViewBag.Channels = channels;
                    ViewBag.StatusMessage = "Connected to OPC server.";
                    return View("Index");
                }
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = $"Failed to connect to OPC server: {ex.Message}";
                return View("Index");
            }
        }

        private List<string> BrowseChannels(Session session)
        {
            var channels = new List<string>();

            var browseRequest = new BrowseRequest
            {
                NodesToBrowse = new BrowseDescriptionCollection
                {
                    new BrowseDescription
                    {
                        NodeId = Objects.RootFolder,
                        BrowseDirection = BrowseDirection.Forward,
                        ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
                        IncludeSubtypes = true,
                        NodeClassMask = (uint)NodeClass.Object,
                        ResultMask = (uint)BrowseResultMask.All
                    }
                },
                RequestedMaxReferencesPerNode = 0
            };

            // 调用1.1.0版本支持的Browse重载,直接获取BrowseResponse
            var browseResponse = session.Browse(browseRequest);

            foreach (var result in browseResponse.Results)
            {
                if (result.References != null)
                {
                    foreach (var referenceDescription in result.References)
                    {
                        channels.Add(referenceDescription.DisplayName.Text);
                    }
                }
            }

            return channels;
        }
    }
}

1.1.0版本特定变更说明

  • 会话创建:必须基于ApplicationConfiguration构建,不再支持直接传入EndpointConfiguration
  • Session.Open方法:移除了接收ApplicationType的重载,调用时无需传入该参数
  • Browse方法:仅保留接收BrowseRequest的重载,返回BrowseResponse对象,可直接访问Results属性

内容的提问来源于stack exchange,提问作者Edward Cullen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 18:01:17