{"record":{"id":"afdb29db9edf9a82","repo":"JeffreySu/WeiXinMPSDK","slug":"onexecutingasync","errorCode":null,"errorMessage":"请使用异步方法 OnExecutingAsync()","messagePattern":"请使用异步方法 OnExecutingAsync\\(\\)","errorType":"exception","errorClass":"MessageHandlerException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.MP/Senparc.Weixin.MP/MessageHandlers/MessageHandler.cs","lineNumber":358,"sourceCode":"        //        return null;\n        //    }\n\n        //    var responseMessage = RequestMessage.CreateResponseMessage<TR>();\n        //    responseMessage.Content = content;\n        //    return responseMessage;\n        //}\n\n        #endregion\n\n        #region 消息处理\n\n        /// <summary>\n        /// OnExecuting\n        /// </summary>\n        [Obsolete(\"请使用异步方法 OnExecutingAsync()\", true)]\n        public override void OnExecuting()\n        {\n            throw new MessageHandlerException(\"请使用异步方法 OnExecutingAsync()\");\n        }\n\n        [Obsolete(\"请使用异步方法 OnExecutedAsync()\", true)]\n        public override void OnExecuted()\n        {\n            throw new MessageHandlerException(\"请使用异步方法 OnExecutedAsync()\");\n        }\n\n        #endregion }\n    }\n}\n","sourceCodeStart":340,"sourceCodeEnd":370,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.MP/Senparc.Weixin.MP/MessageHandlers/MessageHandler.cs#L340-L370","documentation":"In the MP MessageHandler, the synchronous OnExecuting override is marked [Obsolete(..., true)] and unconditionally throws MessageHandlerException. The library migrated its message-processing pipeline to async (OnExecutingAsync), so calling the sync hook is a compile-time-warning but run-time hard error: the sync path is no longer supported.","triggerScenarios":"Calling messageHandler.OnExecuting() directly, overriding it in a subclass and relying on the base synchronous Execute() pipeline, or using legacy sync extension/pipeline code (e.g. old Weixin MpMessageHandler helper overloads) that invokes the sync hook.","commonSituations":"Migrating code written against older Senparc.Weixin versions (pre-async pipeline) to a newer version where the sync OnExecuting/OnExecuted were made throw-on-use; following outdated blog samples that call sync message handler methods inside ASP.NET (non-Core) controllers.","solutions":["Replace all synchronous handler invocation with the async pipeline: call MessageHandler.ExecuteAsync() (or the async request-message extension), which uses OnExecutingAsync/OnExecutedAsync.","Move your pre-processing logic from an OnExecuting override into OnExecutingAsync override in your custom handler.","Update old sync extension methods / framework wrappers (e.g. MessageHandler<T>.Execute sync overloads) to their Async equivalents.","Treat compiler CS0619 (obsolete-with-error) on OnExecuting as the signal: fix call sites at compile time rather than at runtime."],"exampleFix":"// before\nmessageHandler.OnExecuting();\nmessageHandler.Execute();\n\n// after\nawait messageHandler.ExecuteAsync(cancellationToken); // pipeline calls OnExecutingAsync internally\n\n// custom handler\npublic override async Task OnExecutingAsync(CancellationToken ct)\n{\n    // pre-processing here\n}","handlingStrategy":"type-guard","validationCode":"// compile-time: treat Obsolete-with-error as a build failure\n#pragma warning disable CS0619 // deliberately detect sync usage\n// do NOT reference messageHandler.OnExecuting();\n#pragma warning restore CS0619","typeGuard":"bool UsesAsyncPipeline(MessageHandler<TRequest, TResponse> h) =>\n    h.GetType().GetMethod(nameof(MessageHandler<TRequest, TResponse>.OnExecutingAsync)) != null;","tryCatchPattern":"try { await messageHandler.ExecuteAsync(ct); }\ncatch (MessageHandlerException ex) when (ex.Message.Contains(\"OnExecutingAsync\"))\n{\n    logger.LogError(ex, \"Sync OnExecuting invoked; migrate to ExecuteAsync\");\n    throw;\n}","preventionTips":["Always invoke handlers through ExecuteAsync, never the sync Execute path.","Enable 'warnings as errors' so [Obsolete(true)] misuse fails the build.","Put pre-processing in OnExecutingAsync overrides only.","Update old samples/wrappers when upgrading Senparc.Weixin major versions."],"tags":["wechat","messagehandler","async","obsolete-api","deprecated"],"backgroundTag":"deprecated-api-usage","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}