Example #1
0
        public static void Register(string corpId, string corpSecret, string name = null)
        {
            //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
            RegisterFunc = () =>
            {
                using (FlushCache.CreateInstance())
                {
                    var bag = new AccessTokenBag()
                    {
                        Name              = name,
                        CorpId            = corpId,
                        CorpSecret        = corpSecret,
                        ExpireTime        = DateTime.MinValue,
                        AccessTokenResult = new AccessTokenResult()
                    };
                    Update(BuildingKey(corpId, corpSecret), bag);
                    return(bag);
                }
            };
            RegisterFunc();

            JsApiTicketContainer.Register(corpId, corpSecret);   //连带注册JsApiTicketContainer

            ProviderTokenContainer.Register(corpId, corpSecret); //连带注册ProviderTokenContainer
        }
        /// <summary>
        /// 注册每个corpId和corpSecret,在调用高级接口时可以仅使用AppKey(由 AccessTokenContainer.BuildingKey() 方法获得)
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="corpSecret"></param>
        /// <param name="name">标记AccessToken名称(如微信公众号名称),帮助管理员识别。当 name 不为 null 和 空值时,本次注册内容将会被记录到 Senparc.Weixin.Config.SenparcWeixinSetting.Items[name] 中,方便取用。</param>
        public static void Register(string corpId, string corpSecret, string name = null)
        {
            //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
            RegisterFunc = () =>
            {
                //using (FlushCache.CreateInstance())
                //{
                var bag = new AccessTokenBag()
                {
                    Name              = name,
                    CorpId            = corpId,
                    CorpSecret        = corpSecret,
                    ExpireTime        = DateTimeOffset.MinValue,
                    AccessTokenResult = new AccessTokenResult()
                };
                Update(BuildingKey(corpId, corpSecret), bag, null);
                return(bag);
                //}
            };
            RegisterFunc();

            if (!name.IsNullOrEmpty())
            {
                Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinCorpId     = corpId;
                Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinCorpSecret = corpSecret;
            }

            JsApiTicketContainer.Register(corpId, corpSecret);   //连带注册JsApiTicketContainer

            ProviderTokenContainer.Register(corpId, corpSecret); //连带注册ProviderTokenContainer
        }
        ///// <summary>
        ///// 检查是否已经注册
        ///// </summary>
        ///// <param name="corpId"></param>
        ///// <returns></returns>
        ///// 此接口无异步方法
        //public new static bool CheckRegistered(string corpId)
        //{
        //    return Cache.CheckExisted(corpId);
        //}

        #endregion


        #region 异步方法

        /// <summary>
        /// 【异步方法】注册每个corpId和corpSecret,在调用高级接口时可以仅使用AppKey(由 AccessTokenContainer.BuildingKey() 方法获得)
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="corpSecret"></param>
        /// <param name="name">标记AccessToken名称(如微信公众号名称),帮助管理员识别。当 name 不为 null 和 空值时,本次注册内容将会被记录到 Senparc.Weixin.Config.SenparcWeixinSetting.Items[name] 中,方便取用。</param>
        public static async Task RegisterAsync(string corpId, string corpSecret, string name = null)
        {
            //记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
            RegisterFunc = async() =>
            {
                //using (FlushCache.CreateInstance())
                //{
                var bag = new AccessTokenBag()
                {
                    Name              = name,
                    CorpId            = corpId,
                    CorpSecret        = corpSecret,
                    ExpireTime        = DateTimeOffset.MinValue,
                    AccessTokenResult = new AccessTokenResult()
                };
                await UpdateAsync(BuildingKey(corpId, corpSecret), bag, null);

                return(bag);
                //}
            };

            var registerTask = RegisterFunc();

            if (!name.IsNullOrEmpty())
            {
                Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinCorpId     = corpId;
                Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinCorpSecret = corpSecret;
            }

            var registerJsApiTask = JsApiTicketContainer.RegisterAsync(corpId, corpSecret);      //连带注册JsApiTicketContainer

            var registerProviderTask = ProviderTokenContainer.RegisterAsync(corpId, corpSecret); //连带注册ProviderTokenContainer

            Task.WaitAll(new[] { registerTask, registerJsApiTask, registerProviderTask });       //等待所有任务完成
        }