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

.NET8中使用Lextm.SharpSnmpLib出现SNMP请求超时异常

.NET 8下SharpSnmpLib SNMPv3请求超时问题(原.NET Framework 4.6.2正常)

在.NET Framework 4.6.2环境中使用Lextm.SharpSnmpLib发起SNMPv3请求完全正常,但升级到.NET 8后,无论设置多长超时时间,请求回复阶段都会触发超时异常。已确认Telnet、Ping目标服务器正常,也尝试了隔离单请求、分析异常,问题仍存在。

相关代码

using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;
using Lextm.SharpSnmpLib;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace SNMP
{
  public class Class1
  {
    public static string GetSNMP(string username, string authPassword, string privacyPassword, string ipAddress, string snmpId)
    {
      var auth = new SHA1AuthenticationProvider(new OctetString(authPassword));
      var priv = new DESPrivacyProvider(new OctetString(privacyPassword), auth);

      Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetNextRequestPdu);

      ReportMessage report = discovery.GetResponse(60000, new IPEndPoint(IPAddress.Parse(ipAddress), 161));

      GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(username), new List<Variable> {
        new Variable(new ObjectIdentifier(snmpId))
      }, priv, Messenger.MaxMessageSize, report);

      ISnmpMessage reply = request.GetResponse(60000, new IPEndPoint(IPAddress.Parse(ipAddress), 161));

      if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError   
      {
        throw ErrorException.Create("error in response", IPAddress.Parse(ipAddress), reply);
      }
      var resultVariable = reply.Pdu().Variables[0];
      return resultVariable.Data.ToString();
    }
  }
}

解决方案

  • 升级SharpSnmpLib版本
    旧版本的SharpSnmpLib可能未适配.NET 8的网络栈变化,建议升级到官方最新稳定版(v12.x及以上),该版本已明确支持.NET 6+及.NET 8环境。

  • 显式指定本地UDP端点
    .NET 8对UDP套接字的默认绑定逻辑做了调整,未指定本地端点可能导致无法接收响应。修改GetResponse调用,添加本地端点参数:

    var localEndPoint = new IPEndPoint(IPAddress.Any, 0);
    ReportMessage report = discovery.GetResponse(60000, new IPEndPoint(IPAddress.Parse(ipAddress), 161), localEndPoint);
    ISnmpMessage reply = request.GetResponse(60000, new IPEndPoint(IPAddress.Parse(ipAddress), 161), localEndPoint);
    
  • 适配加密算法的.NET 8兼容性
    .NET 8中SHA1、DES等旧算法的默认实现受限,可尝试两种方案:

    1. 更换为现代加密算法(优先推荐,若设备支持):
      var auth = new SHA256AuthenticationProvider(new OctetString(authPassword));
      var priv = new AesPrivacyProvider(new OctetString(privacyPassword), auth, 128); // 根据设备支持选择128/192/256位密钥
      
    2. 启用旧算法支持(若设备只能用SHA1/DES):
      在项目文件(.csproj)中添加配置:
      <PropertyGroup>
        <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
      </PropertyGroup>
      
      同时在代码初始化时添加:
      AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true);
      AppContext.SetSwitch("Switch.System.Security.Cryptography.DontUpgradetoSha2", true);
      
  • 启用调试日志排查数据包交互
    注册SharpSnmpLib的控制台日志,查看请求发送、响应接收的详细过程:

    // 在调用GetSNMP方法前添加
    Messenger.RegisterLogger(new ConsoleLogger());
    

    通过日志确认是否有数据包发送成功、响应是否到达本地,以及是否在解密/解析阶段出现异常。

  • 检查网络权限与防火墙设置
    确认.NET 8应用程序拥有网络访问权限,同时检查操作系统防火墙是否允许应用接收UDP响应(需允许出站UDP请求及入站UDP响应数据包)。

内容的提问来源于stack exchange,提问作者salman dukanwala

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 08:14:53