/// <summary> /// 开始 SDK 初始化参数流程(.NET Core) /// </summary> /// <param name="env">IHostingEnvironment,控制台程序可以输入null,</param> /// <param name="senparcSetting"></param> /// <returns></returns> public static RegisterService Start(IHostingEnvironment env, OFoodSetting senparcSetting) { //提供网站根目录 if (env != null && env.ContentRootPath != null) { OFoodConfig.RootDictionaryPath = env.ContentRootPath; } else { OFoodConfig.RootDictionaryPath = AppDomain.CurrentDomain.BaseDirectory; } var register = new RegisterService(senparcSetting); //如果不注册此线程,则AccessToken、JsTicket等都无法使用SDK自动储存和管理。 register.RegisterThreads();//默认把线程注册好 return(register); }
/// <summary> /// 开始 Senparc.Weixin SDK 初始化参数流程 /// </summary> /// <param name="registerService"></param> /// <param name="senparcWeixinSetting">微信全局设置参数,必填</param> /// <param name="senparcSetting">用于提供 SenparcSetting.Cache_Redis_Configuration 和 Cache_Memcached_Configuration 两个参数,如果不使用这两种分布式缓存可传入null</param> /// <returns></returns> public static IRegisterService UseSenparcWeixin(this IRegisterService registerService, WeixinSetting senparcWeixinSetting, OFoodSetting senparcSetting = null) { senparcWeixinSetting = senparcWeixinSetting ?? new WeixinSetting(); senparcSetting = senparcSetting ?? new OFoodSetting(); //Senparc.Weixin SDK 配置 WxConfig.WeixinSetting = senparcWeixinSetting; /* 扩展缓存注册开始 */ var cacheTypes = "";//所有注册的扩展缓存 // 微信的 本地 缓存 var cache = LocalContainerCacheStrategy.Instance;//只要引用就可以被激活 cacheTypes += typeof(LocalContainerCacheStrategy); var dt1 = SystemTime.Now; //官方扩展缓存注册 //var officialTypes = new List<Type>() { typeof(LocalContainerCacheStrategy) };//官方提供的扩展缓存策略 //自动注册 Redis 和 Memcached //Redis var redisConfiguration = senparcSetting.Cache_Redis_Configuration; if (!string.IsNullOrEmpty(redisConfiguration) && /*缓存配置默认值,不启用*/ redisConfiguration != "Redis配置" && redisConfiguration != "#{Cache_Redis_Configuration}#") { try { var redisInstance = ReflectionHelper.GetStaticMember("Senparc.Weixin.Cache.Redis", "Senparc.Weixin.Cache.Redis", "RedisContainerCacheStrategy", "Instance"); if (redisInstance != null) { //officialTypes.Add(redisInstance.GetType()); cacheTypes += "\r\n" + redisInstance.GetType(); } } catch (Exception ex) { WeixinTrace.WeixinExceptionLog(new Exceptions.WeixinException(ex.Message, ex)); } } //Memcached var memcachedConfiguration = senparcSetting.Cache_Memcached_Configuration; if (!string.IsNullOrEmpty(memcachedConfiguration) && /*缓存配置默认值,不启用*/ memcachedConfiguration != "Memcached配置" && memcachedConfiguration != "#{Cache_Memcached_Configuration}#") { try { var memcachedInstance = ReflectionHelper.GetStaticMember("Senparc.Weixin.Cache.Memcached", "Senparc.Weixin.Cache.Memcached", "MemcachedContainerCacheStrategy", "Instance"); if (memcachedInstance != null) { //officialTypes.Add(memcachedInstance.GetType()); cacheTypes += "\r\n" + memcachedInstance.GetType(); } } catch (Exception ex) { WeixinTrace.WeixinExceptionLog(new Exceptions.WeixinException(ex.Message, ex)); } } var dt2 = SystemTime.Now; var exCacheLog = "微信扩展缓存注册总用时:{0}ms\r\n扩展缓存:{1}".FormatWith((dt2 - dt1).TotalMilliseconds, cacheTypes); WeixinTrace.SendCustomLog("微信扩展缓存注册完成", exCacheLog); /* 扩展缓存注册结束 */ //ApiBind 自动扫描 Register.RegisterApiBind(false); return(registerService); }
/// <summary> /// 开始 SDK 初始化参数流程(.NET Core) /// </summary> /// <param name="env">IHostingEnvironment,控制台程序可以输入null,</param> /// <param name="senparcSetting"></param> /// <returns></returns> public static RegisterService Start(OFoodSetting senparcSetting) { return(Start(null, senparcSetting)); }
//private RegisterService() : this(null) { } private RegisterService(OFoodSetting senparcSetting) { OFoodConfig.Setting = senparcSetting ?? new OFoodSetting(); }