JeffreySu/WeiXinMPSDK · error · ArgumentException
BotID 不能为空。
Error message
BotID 不能为空。
What it means
SmartRobotWebSocketClient.ConnectAndSubscribeAsync validates botId with string.IsNullOrWhiteSpace and throws ArgumentException("BotID 不能为空。") when it is null, empty, or whitespace-only. The botId is required to establish the SmartRobot WebSocket subscription, and it is also stored on the client instance for reconnects.
Solutions
- Supply a valid non-empty botId from your SmartRobot configuration.
- Validate the botId at application startup before attempting connection.
- If botId is fetched dynamically, handle the empty result before connecting.
Example fix
// before
await client.ConnectAndSubscribeAsync(config.BotId, config.Secret); // BotId == null
// after
if (string.IsNullOrWhiteSpace(config.BotId))
throw new InvalidOperationException("配置缺少 BotID");
await client.ConnectAndSubscribeAsync(config.BotId, config.Secret); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(botId)) throw new InvalidOperationException("SmartRobot BotID 未配置"); Try / catch
try { await client.ConnectAndSubscribeAsync(botId, secret, ct); } catch (ArgumentException ex) when (ex.ParamName == nameof(botId)) { logger.LogError(ex, "BotID 为空,无法订阅"); } Prevention
- Validate botId (and secret) right after loading configuration at startup.
- Avoid whitespace-only placeholder values in config files.
- Wrap connection setup in a factory method that enforces non-empty identifiers.
When it happens
Trigger: Calling ConnectAndSubscribeAsync(botId, secret) with botId null, "", or whitespace (e.g. " "), typically after ThrowIfDisposed has passed.
Common situations: Bot ID read from configuration that was never filled in, placeholder values left in appsettings, or botId coming from an upstream lookup that returned empty on first run.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Secret 不能为空。
- 方法路径不能为空
- 商品券图片仅支持 JPG、JPEG、BMP 或 PNG。
- ArgumentOutOfRangeException (length must be >= 0)
- 请求未提供 sig 时必须提供收银台 API 调用密钥。
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/16ab019927c6789b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/SmartRobot/SmartRobotWebSocketClient.cs:71
/// <param name="endpoint">WebSocket 服务地址,默认使用企业微信官方地址。</param>
public SmartRobotWebSocketClient(string endpoint = DefaultEndpoint)
{
_endpoint = new Uri(endpoint);
_socket = CreateSocket();
}
/// <summary>
/// 异步连接智能机器人 WebSocket 并订阅事件。
/// </summary>
/// <param name="botId">智能机器人 ID。</param>
/// <param name="secret">智能机器人 Secret。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>表示异步操作的任务。</returns>
public async Task ConnectAndSubscribeAsync(string botId, string secret,
CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
_botId = string.IsNullOrWhiteSpace(botId) ? throw new ArgumentException("BotID 不能为空。", nameof(botId)) : botId;
_secret = string.IsNullOrWhiteSpace(secret) ? throw new ArgumentException("Secret 不能为空。", nameof(secret)) : secret;
if (_socket.State != WebSocketState.None)
{
_socket.Dispose();
_socket = CreateSocket();
}
await _socket.ConnectAsync(_endpoint, cancellationToken).ConfigureAwait(false);
await SendCommandAsync("aibot_subscribe", CreateRequestId(), new
{
bot_id = _botId,
secret = _secret
}, cancellationToken).ConfigureAwait(false);
}
/// <summary>持续接收消息,直到取消、服务端关闭或网络断开。</summary>
/// <param name="cancellationToken">取消令牌。</param>View on GitHub (pinned to be573f6f94)