From 07ea27d9bac4c868fb7e3caf0e57f104ec0e3811 Mon Sep 17 00:00:00 2001 From: JeffreySu Date: Thu, 14 Dec 2023 12:52:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Console=20Sample=20=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E5=88=B0=20.NET=208.0=EF=BC=8C=E4=BD=BF=E7=94=A8=E9=A1=B6?= =?UTF-8?q?=E7=BA=A7=E8=AF=AD=E5=8F=A5=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....Net8SampleForConsole.CommonService.csproj | 20 +- .../Program.cs | 341 +----------------- .../Senparc.Weixin.MP.Sample.Consoles.csproj | 20 +- 3 files changed, 27 insertions(+), 354 deletions(-) diff --git a/Samples/All/Senparc.Weixin.Sample.CommonService/Senparc.Weixin.Net8SampleForConsole.CommonService.csproj b/Samples/All/Senparc.Weixin.Sample.CommonService/Senparc.Weixin.Net8SampleForConsole.CommonService.csproj index 475a3745ea..ab77c96711 100644 --- a/Samples/All/Senparc.Weixin.Sample.CommonService/Senparc.Weixin.Net8SampleForConsole.CommonService.csproj +++ b/Samples/All/Senparc.Weixin.Sample.CommonService/Senparc.Weixin.Net8SampleForConsole.CommonService.csproj @@ -21,7 +21,7 @@ - + @@ -30,15 +30,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Program.cs b/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Program.cs index abec667359..fe0b443955 100644 --- a/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Program.cs +++ b/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Program.cs @@ -33,13 +33,14 @@ and limitations under the License. ----------------------------------------------------------------*/ using System; -using Microsoft.AspNetCore.Builder.Internal; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Senparc.CO2NET; +using Senparc.CO2NET.Extensions; +using Senparc.Weixin; using Senparc.Weixin.Entities; +using Senparc.Weixin.MP; using Senparc.Weixin.RegisterServices; -using Senparc.Weixin.Sample.CommonService.CustomMessageHandler; var builder = new ConfigurationBuilder(); @@ -73,10 +74,8 @@ and limitations under the License. //手动获取配置信息可使用以下方法 //var senparcWeixinSetting = app.Services.GetService>()!.Value; -app.UseSenparcWeixin(); - //启用微信配置(必须) -var registerService = app.UseSenparcWeixin(app.Environment, +var registerService = app.UseSenparcWeixin( null /* 不为 null 则覆盖 appsettings 中的 SenpacSetting 配置*/, null /* 不为 null 则覆盖 appsettings 中的 SenpacWeixinSetting 配置*/, register => { /* CO2NET 全局配置 */ }, @@ -86,336 +85,10 @@ and limitations under the License. register.RegisterMpAccount(weixinSetting, "【盛派网络小助手】公众号"); }); -#region 使用 MessageHadler 中间件,用于取代创建独立的 Controller - -//MessageHandler 中间件介绍:https://www.cnblogs.com/szw/p/Wechat-MessageHandler-Middleware.html -//使用公众号的 MessageHandler 中间件(不再需要创建 Controller) -app.UseMessageHandlerForMp("/WeixinAsync", CustomMessageHandler.GenerateMessageHandler, options => -{ - //获取默认微信配置 - var weixinSetting = Senparc.Weixin.Config.SenparcWeixinSetting; - - //[必须] 设置微信配置 - options.AccountSettingFunc = context => weixinSetting; - - //[可选] 设置最大文本长度回复限制(超长后会调用客服接口分批次回复) - options.TextResponseLimitOptions = new TextResponseLimitOptions(2048, weixinSetting.WeixinAppId); -}); - #endregion -#endregion - - - -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Senparc.CO2NET; -using Senparc.CO2NET.Cache; -using Senparc.CO2NET.Cache.Memcached; -using Senparc.CO2NET.RegisterServices; -using Senparc.Weixin.Entities; -using Senparc.Weixin.RegisterServices; -using Senparc.Weixin.WxOpen; -using Senparc.Weixin.Work; -using Senparc.Weixin.TenPay; -using Senparc.Weixin.Open; -using System; -using Senparc.CO2NET.Utilities; -using System.IO; -using Senparc.Weixin.Open.ComponentAPIs; -using Senparc.CO2NET.Extensions; - -namespace Senparc.Weixin.MP.Sample.Consoles -{ - class Program - { - static void Main(string[] args) - { - var dt1 = SystemTime.Now; - - var configBuilder = new ConfigurationBuilder(); - configBuilder.AddJsonFile("appsettings.json", false, false); - Console.WriteLine("完成 appsettings.json 添加"); - - var config = configBuilder.Build(); - Console.WriteLine("完成 ServiceCollection 和 ConfigurationBuilder 初始化"); - - //更多绑定操作参见:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2 - var senparcSetting = new SenparcSetting(); - var senparcWeixinSetting = new SenparcWeixinSetting(); - - config.GetSection("SenparcSetting").Bind(senparcSetting); - config.GetSection("SenparcWeixinSetting").Bind(senparcWeixinSetting); - - var services = new ServiceCollection(); - services.AddMemoryCache();//使用本地缓存必须添加 - - /* - * CO2NET 是从 Senparc.Weixin 分离的底层公共基础模块,经过了长达 6 年的迭代优化,稳定可靠。 - * 关于 CO2NET 在所有项目中的通用设置可参考 CO2NET 的 Sample: - * https://github.com/Senparc/Senparc.CO2NET/blob/master/Sample/Senparc.CO2NET.Sample.netcore/Startup.cs - */ - - services.AddSenparcGlobalServices(config);//Senparc.CO2NET 全局注册 - Console.WriteLine("完成 AddSenparcGlobalServices 注册"); - - //输出 JSON 校验 - //var jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }; - //Console.WriteLine($"SenparcSetting:{senparcSetting.ToJson(true, jsonSerializerSettings)}"); - //Console.WriteLine($"SenparcWeixinSetting:{senparcWeixinSetting.ToJson(true,jsonSerializerSettings)}"); - - // 启动 CO2NET 全局注册,必须! - IRegisterService register = RegisterService.Start(senparcSetting) - //关于 UseSenparcGlobal() 的更多用法见 CO2NET Demo:https://github.com/Senparc/Senparc.CO2NET/blob/master/Sample/Senparc.CO2NET.Sample.netcore/Startup.cs - .UseSenparcGlobal(); - - Console.WriteLine("完成 RegisterService.Start().UseSenparcGlobal() 启动设置"); - Console.WriteLine($"设定程序目录为:{Senparc.CO2NET.Config.RootDirectoryPath}"); - - #region CO2NET 全局配置 - - #region 全局缓存配置(按需) - - //当同一个分布式缓存同时服务于多个网站(应用程序池)时,可以使用命名空间将其隔离(非必须) - register.ChangeDefaultCacheNamespace("DefaultCO2NETCache"); - Console.WriteLine($"默认缓存命名空间替换为:{CO2NET.Config.DefaultCacheNamespace}"); - - - #region 配置和使用 Redis -- DPBMARK Redis - - //配置全局使用Redis缓存(按需,独立) - var redisConfigurationStr = senparcSetting.Cache_Redis_Configuration; - var useRedis = !string.IsNullOrEmpty(redisConfigurationStr) && redisConfigurationStr != "#{Cache_Redis_Configuration}#"/*默认值,不启用*/; - if (useRedis)//这里为了方便不同环境的开发者进行配置,做成了判断的方式,实际开发环境一般是确定的,这里的if条件可以忽略 - { - /* 说明: - * 1、Redis 的连接字符串信息会从 Config.SenparcSetting.Cache_Redis_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略 - /* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Redis 链接信息(仅修改配置,不立即启用) - */ - Senparc.CO2NET.Cache.Redis.Register.SetConfigurationOption(redisConfigurationStr); - Console.WriteLine("完成 Redis 设置"); - - - //以下会立即将全局缓存设置为 Redis - Senparc.CO2NET.Cache.Redis.Register.UseKeyValueRedisNow();//键值对缓存策略(推荐) - Console.WriteLine("启用 Redis UseKeyValue 策略"); - - //Senparc.CO2NET.Cache.Redis.Register.UseHashRedisNow();//HashSet储存格式的缓存策略 - - //也可以通过以下方式自定义当前需要启用的缓存策略 - //CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);//键值对 - //CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisHashSetObjectCacheStrategy.Instance);//HashSet - } - //如果这里不进行Redis缓存启用,则目前还是默认使用内存缓存 - - #endregion // DPBMARK_END - - #region 配置和使用 Memcached -- DPBMARK Memcached - - //配置Memcached缓存(按需,独立) - var memcachedConfigurationStr = senparcSetting.Cache_Memcached_Configuration; - var useMemcached = !string.IsNullOrEmpty(memcachedConfigurationStr) && memcachedConfigurationStr != "#{Cache_Memcached_Configuration}#"; - - if (useMemcached) //这里为了方便不同环境的开发者进行配置,做成了判断的方式,实际开发环境一般是确定的,这里的if条件可以忽略 - { - /* 说明: - * 1、Memcached 的连接字符串信息会从 Config.SenparcSetting.Cache_Memcached_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略 - /* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Memcached 链接信息(仅修改配置,不立即启用) - */ - Senparc.CO2NET.Cache.Memcached.Register.SetConfigurationOption(memcachedConfigurationStr); - Console.WriteLine("完成 Memcached 设置"); - - //以下会立即将全局缓存设置为 Memcached - Senparc.CO2NET.Cache.Memcached.Register.UseMemcachedNow(); - Console.WriteLine("启用 Memcached UseKeyValue 策略"); - - - //也可以通过以下方式自定义当前需要启用的缓存策略 - CacheStrategyFactory.RegisterObjectCacheStrategy(() => MemcachedObjectCacheStrategy.Instance); - Console.WriteLine("立即启用 Memcached 策略"); - } - - #endregion // DPBMARK_END - - #endregion - - #region 注册日志(按需,建议) - - register.RegisterTraceLog(ConfigTraceLog);//配置TraceLog - - #endregion - - #endregion - - #region 微信相关配置 - - - /* 微信配置开始 - * - * 建议按照以下顺序进行注册,尤其须将缓存放在第一位! - */ - - //注册开始 - - //开始注册微信信息,必须! - register.UseSenparcWeixin(senparcWeixinSetting, senparcSetting) - //注意:上一行没有 ; 下面可接着写 .RegisterXX() - - #region 注册公众号或小程序(按需) - - //注册公众号(可注册多个) -- DPBMARK MP - .RegisterMpAccount(senparcWeixinSetting, "【盛派网络小助手】公众号")// DPBMARK_END - - - //注册多个公众号或小程序(可注册多个) -- DPBMARK MiniProgram - .RegisterWxOpenAccount(senparcWeixinSetting, "【盛派网络小助手】小程序")// DPBMARK_END - - //除此以外,仍然可以在程序任意地方注册公众号或小程序: - //AccessTokenContainer.Register(appId, appSecret, name);//命名空间:Senparc.Weixin.MP.Containers - #endregion - - #region 注册企业号(按需) -- DPBMARK Work - - //注册企业微信(可注册多个) - .RegisterWorkAccount(senparcWeixinSetting, "【盛派网络】企业微信") - - //除此以外,仍然可以在程序任意地方注册企业微信: - //AccessTokenContainer.Register(corpId, corpSecret, name);//命名空间:Senparc.Weixin.Work.Containers - #endregion // DPBMARK_END - - #region 注册微信支付(按需) -- DPBMARK TenPay - - //注册旧微信支付版本(V2)(可注册多个) - .RegisterTenpayOld(senparcWeixinSetting, "【盛派网络小助手】公众号")//这里的 name 和第一个 RegisterMpAccount() 中的一致,会被记录到同一个 SenparcWeixinSettingItem 对象中 - - //注册最新微信支付版本(V3)(可注册多个) - .RegisterTenpayV3(senparcWeixinSetting, "【盛派网络小助手】公众号")//记录到同一个 SenparcWeixinSettingItem 对象中 - - #endregion // DPBMARK_END - - #region 注册微信第三方平台(按需) -- DPBMARK Open - - //注册第三方平台(可注册多个) - .RegisterOpenComponent(senparcWeixinSetting, - //getComponentVerifyTicketFunc - async componentAppId => - { - var dir = Path.Combine(ServerUtility.ContentRootMapPath("~/App_Data/OpenTicket")); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - - var file = Path.Combine(dir, string.Format("{0}.txt", componentAppId)); - using (var fs = new FileStream(file, FileMode.Open)) - { - using (var sr = new StreamReader(fs)) - { - var ticket = await sr.ReadToEndAsync(); - return ticket; - } - } - }, - - //getAuthorizerRefreshTokenFunc - async (componentAppId, auhtorizerId) => - { - var dir = Path.Combine(ServerUtility.ContentRootMapPath("~/App_Data/AuthorizerInfo/" + componentAppId)); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - - var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); - if (!File.Exists(file)) - { - return null; - } - - using (Stream fs = new FileStream(file, FileMode.Open)) - { - var binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - var result = (RefreshAuthorizerTokenResult)binFormat.Deserialize(fs); - return result.authorizer_refresh_token; - } - }, - - //authorizerTokenRefreshedFunc - (componentAppId, auhtorizerId, refreshResult) => - { - var dir = Path.Combine(ServerUtility.ContentRootMapPath("~/App_Data/AuthorizerInfo/" + componentAppId)); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - - var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); - using (Stream fs = new FileStream(file, FileMode.Create)) - { - //这里存了整个对象,实际上只存RefreshToken也可以,有了RefreshToken就能刷新到最新的AccessToken - var binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - binFormat.Serialize(fs, refreshResult); - fs.Flush(); - } - }, "【盛派网络】开放平台") - - //除此以外,仍然可以在程序任意地方注册开放平台: - //ComponentContainer.Register();//命名空间:Senparc.Weixin.Open.Containers - #endregion // DPBMARK_END - - ; - - /* 微信配置结束 */ - - #endregion - - - Console.WriteLine("Hello Senparc.Weixin!"); - Console.WriteLine($"Total initialization time: {(SystemTime.Now - dt1).TotalMilliseconds}ms"); - - Console.WriteLine($"当前缓存策略: {CacheStrategyFactory.GetObjectCacheStrategyInstance()}"); - - var jsonSetting = new Newtonsoft.Json.JsonSerializerSettings() - { - NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, - ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore - }; - Console.WriteLine($"SenparcWeixinSetting: {Senparc.Weixin.Config.SenparcWeixinSetting.ToJson(true, jsonSetting)}"); - - Console.ReadLine(); - } - - - /// - /// 配置微信跟踪日志 - /// - static void ConfigTraceLog() - { - //这里设为Debug状态时,/App_Data/WeixinTraceLog/目录下会生成日志文件记录所有的API请求日志,正式发布版本建议关闭 - - //如果全局的IsDebug(Senparc.CO2NET.Config.IsDebug)为false,此处可以单独设置true,否则自动为true - CO2NET.Trace.SenparcTrace.SendCustomLog("系统日志", "系统启动");//只在Senparc.Weixin.Config.IsDebug = true的情况下生效 - - //全局自定义日志记录回调 - CO2NET.Trace.SenparcTrace.OnLogFunc = () => - { - //加入每次触发Log后需要执行的代码 - }; - - - //当发生基于WeixinException的异常时触发 - WeixinTrace.OnWeixinExceptionFunc = ex => - { - //加入每次触发WeixinExceptionLog后需要执行的代码 +//可以开始使用 - //发送模板消息给管理员 -- DPBMARK Redis - var eventService = new Senparc.Weixin.Sample.CommonService.EventService(); - eventService.ConfigOnWeixinExceptionFunc(ex); // DPBMARK_END - }; +var weixinSetting = Senparc.Weixin.Config.SenparcWeixinSetting; - Console.WriteLine("完成日志设置,已经记录 1 条系统启动日志"); - } - } -} +Console.WriteLine(weixinSetting.Items["【盛派网络小助手】公众号"].ToJson(true)); \ No newline at end of file diff --git a/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Senparc.Weixin.MP.Sample.Consoles.csproj b/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Senparc.Weixin.MP.Sample.Consoles.csproj index 09678b9921..be413c017c 100644 --- a/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Senparc.Weixin.MP.Sample.Consoles.csproj +++ b/Samples/All/console/Senparc.Weixin.MP.Sample.Consoles/Senparc.Weixin.MP.Sample.Consoles.csproj @@ -8,16 +8,16 @@ - - - - - - - - - - + + + + + + + + + + From 77c503e901505736cb293ab20764118c316d64f5 Mon Sep 17 00:00:00 2001 From: Jeffrey Su Date: Thu, 14 Dec 2023 12:53:41 +0800 Subject: [PATCH 2/2] Update azure-pipelines.yml --- azure-pipelines.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 3ed46718e6..52db0365ce 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -18,9 +18,9 @@ steps: - task: NuGetToolInstaller@1 - task: UseDotNet@2 - displayName: 'Use .NET SDK 6' + displayName: 'Use .NET SDK 8' inputs: - version: '6.0.x' + version: '8.0.x' packageType: 'sdk' # - task: NuGetCommand@2