Senparc.Weixin.MP SDK 微信公众平台开发教程(二十三):在 .NE
概述在 《Senparc.Weixin.MP SDK?微信公众平台开发教程(六):了解MessageHandler》 中我们已经了解了 MessageHandler 的运行原理和使用方法,从我设计了这种处理方式到现在已经 6 年多的时间,这是一种非常稳定而且(在如此复杂环境下)相对易于维护的的设计。 现在 .NET Core 已经进入了一个新的阶段,随着 .NET Core 3.0 的发布,我决在兼容原有方式不改变的情况下,让 MessageHandler 可以支持 .NET Core(或者说 .NET Standard 2.0+)的中间件,为开发者提供另外一种使用 SDK 的方式。 新的设计将达到如下目标或新特性:
准备开发
开发步骤第一步:创建一个 .NET Core 3.0 Web 项目(空):原始的?startup.cs?如下: 1 1 namespace WechatMessageSample 2 2 { 3 3 public class Startup 4 4 { 5 5 // This method gets called by the runtime. Use this method to add services to the container. 6 6 For more information on how to configure your application,visit https://go.microsoft.com/fwlink/?LinkID=398940 7 7 void ConfigureServices(IServiceCollection services) 8 8 { 9 9 } 10 10 11 11 This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 12 12 Configure(IApplicationBuilder app,IWebHostEnvironment env) 13 1314 14 if (env.IsDevelopment()) 15 15 { 16 16 app.UseDeveloperExceptionPage(); 17 17 } 18 18 19 19 app.UseRouting(); 20 20 21 21 app.UseEndpoints(endpoints => 22 2223 23 endpoints.MapGet("/",async context => 24 24 { 25 25 await context.Response.WriteAsync(Hello World!"); 26 26 }); 27 27 }); 28 2829 29 } 30 30 }View Code ? 第二步:使用 Nuget 添加 Senparc.Weixin SDK:上图标注出来的 3 个包分别是:公众号、小程序和企业微信,本实例主要演示公众号,其他两个平台使用方法是一致的,可以举一反三,不再赘述。 ? 第三步:创建自定义消息处理器(MessageHandler):1 /// <summary> 2 /// 自定义公众号消息处理 3 </summary> 4 class CustomMpMessageHandler : Senparc.Weixin.MP.MessageHandlers.MessageHandler<DefaultMpMessageContext> 5 { 6 public CustomMpMessageHandler(Stream inputStream,PostModel postModel,1)">int maxRecordCount = 0,1)">bool onlyAllowEcryptMessage = false,DeveloperInfo developerInfo = null) 7 : base(inputStream,postModel,maxRecordCount,onlyAllowEcryptMessage,developerInfo) 8 { 9 } 11 override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage) 12 13 throw new NotImplementedException(); 14 15 } 一般情况下,此文件是独立的 .cs 文件,当前实例为了方便展示,一起写在了?startup.cs?文件中。 以上是一个默认的空的 MessageHandler,还不能正常运行,我们为 DefaultResponseMessage 添加默认的返回消息: 1 2 3 var responseMessage = base.CreateResponseMessage<ResponseMessageNews>(); 4 responseMessage.Articles.Add( Article() 6 Title = 欢迎使用 Senparc.Weixin SDK, 7 Description = 这是一条默认消息 8 PicUrl = https://sdk.weixin.senparc.com/images/v2/logo.png 9 Url = https://weixin.senparc.com" 10 }); return responseMessage; 12 } 也可以再创建一系列响应规则,例如处理文本信息,并返回一条文本: override async Task<IResponseMessageBase> OnTextRequestAsync(RequestMessageText requestMessage) 2 base.CreateResponseMessage<ResponseMessageText>4 responseMessage.Content = $您发送了文字:{requestMessage.Content}; 5 6 } 以此类推,可以为每一种类型的消息创建处理过程。 为了方便接下去的中间件对接,再创建一个初始化当前自定义 MessageHandler 示例的委托: static Func<Stream,PostModel,1)">int,CustomMpMessageHandler> GenerateMessageHandler = (stream,maxRecordCount) 2 => new CustomMpMessageHandler(stream,1)">false); 上述 CustomMpMessageHandler 构造函数中的最后一个 bool 参数(onlyAllowEcryptMessage),提供了一种加强的安全策略,可以指定是否只允许处理加密消息,开启之后,试图使用明文进行消息推送(嗅探)的请求将被拒绝(前提是对方已经拿到了正确的 Token),并终止后续处理流程,确保程序安全。 ? 第四步:修改 startup.cs 进行全局注册修改 ConfigureServices() 方法:public Startup(IConfiguration configuration) 3 Configuration = configuration; } 5 6 public IConfiguration Configuration { get; } 7 10 services.AddMemoryCache();使用本地缓存必须添加(按需) 11 12 services.AddSenparcWeixinServices(Configuration);Senparc.Weixin 注册 13 } 修改 Configure() 方法参数:2 IOptions<SenparcSetting> senparcSetting,IOptions<SenparcWeixinSetting> senparcWeixinSetting)
Configure() 方法内追加:1 app.UseSenparcGlobal(env,senparcSetting.Value,globalRegister => { }) 2 .UseSenparcWeixin(senparcWeixinSetting.Value,weixinRegister => 3 { 4 weixinRegister.RegisterMpAccount(senparcWeixinSetting.Value,【盛派网络小助手】公众号5 }); 至此注册工作已经全部完成! 指定使用 MessageHandler 中间件,只需一行:app.UseMessageHandlerForMp(/WeixinAsync |