JeffreySu/WeiXinMPSDK · error · InvalidOperationException

调用方法 . 时发生错误

Error message

调用方法 {typeName}.{methodName} 时发生错误

What it means

InvokeStaticMethod wraps the whole reflection invocation in a try/catch and rethrows any exception as InvalidOperationException("调用方法 Type.Method 时发生错误", innerException). The real failure — thrown by the target method, a TargetInvocationException, or an argument binding problem — is in InnerException.

Solutions

  1. Inspect the InnerException of this InvalidOperationException for the root cause
  2. Verify the parameters array matches the target method's signature exactly (count, types, order)
  3. Ensure Senparc.Weixin config (appSecret, token, etc.) is registered before invoking API methods
  4. Check logs/HTTP result of the underlying WeChat call (e.g. errcode returned by the API)

Example fix

// before
catch (InvalidOperationException ex) { log(ex.Message); }
// after
catch (InvalidOperationException ex)
{
    log(ex.Message);
    if (ex.InnerException != null) log("Root cause: " + ex.InnerException); // inspect inner for real error
}
Defensive patterns

Strategy: try-catch

Validate before calling

var m = Type.GetType(typeName)?.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
if (m != null && m.GetParameters().Length != parameters.Length) throw new ArgumentException("Parameter count does not match target method");

Try / catch

try { var result = InvokeWeixinApiHelper.InvokeFullMethod(methodPath, args); }
catch (InvalidOperationException ex) { log.Error(ex, "Invoke failed for {0}", methodPath); if (ex.InnerException != null) HandleRootCause(ex.InnerException); }

Prevention

When it happens

Trigger: The invoked static method itself throws (network errors, missing config like appSecret, invalid parameters), or arguments do not match the method signature so MethodInfo.Invoke throws TargetInvocationException/ArgumentException.

Common situations: WeChat API calls failing due to missing/invalid app credentials, passing wrong parameter count or types to the reflected method, timeouts or access-token errors inside the target API method.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/1b20569ec4993348. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.MCP.Server/InvokeWeixinApiHelper.cs:98

                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    type = assembly.GetType(typeName);
                    if (type != null) break;
                }
            }

            if (type == null)
                throw new InvalidOperationException($"无法找到类型: {typeName}");

            var method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
            if (method == null)
                throw new InvalidOperationException($"无法找到静态方法: {typeName}.{methodName}");

            return method.Invoke(null, parameters);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException($"调用方法 {typeName}.{methodName} 时发生错误", ex);
        }
    }

    /// <summary>
    /// 异步调用静态方法
    /// </summary>
    /// <param name="typeName">类型名称</param>
    /// <param name="methodName">方法名称</param>
    /// <param name="parameters">参数</param>
    /// <returns>方法执行结果</returns>
    public async Task<object> InvokeStaticMethodAsync(string typeName, string methodName, object[] parameters)
    {
        try
        {
            var type = Type.GetType(typeName);
            if (type == null)
            {
                // 尝试从当前加载的程序集中查找类型

View on GitHub (pinned to be573f6f94)