Skip to content

Commit

Permalink
MP v16.7.0 支持异步 Container 1、支持异步 Container;2、停止对 .NET 3.5 和 .NET 4.0 的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySu committed Apr 18, 2019
1 parent 0330087 commit 4566129
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
修改标识:Senparc - 20170522
修改描述:v16.6.2 修改 DateTime 为 DateTimeOffset
修改标识:Senparc - 20190418
修改描述:v17.0.0 支持异步 Container
----------------------------------------------------------------*/

using System;
Expand Down Expand Up @@ -165,10 +168,22 @@ public class AccessTokenContainer : BaseContainer<AccessTokenBag>
/// <param name="appId">微信公众号后台的【开发】>【基本配置】中的“AppID(应用ID)”</param>
/// <param name="appSecret">微信公众号后台的【开发】>【基本配置】中的“AppSecret(应用密钥)”</param>
/// <param name="name">标记AccessToken名称(如微信公众号名称),帮助管理员识别。当 name 不为 null 和 空值时,本次注册内容将会被记录到 Senparc.Weixin.Config.SenparcWeixinSetting.Items[name] 中,方便取用。</param>
[Obsolete("请使用 RegisterAsync() 方法")]
public static void Register(string appId, string appSecret, string name = null)
{
RegisterAsync(appId, appSecret, name).GetAwaiter();
}

/// <summary>
/// 【异步方法】注册应用凭证信息,此操作只是注册,不会马上获取Token,并将清空之前的Token
/// </summary>
/// <param name="appId">微信公众号后台的【开发】>【基本配置】中的“AppID(应用ID)”</param>
/// <param name="appSecret">微信公众号后台的【开发】>【基本配置】中的“AppSecret(应用密钥)”</param>
/// <param name="name">标记AccessToken名称(如微信公众号名称),帮助管理员识别。当 name 不为 null 和 空值时,本次注册内容将会被记录到 Senparc.Weixin.Config.SenparcWeixinSetting.Items[name] 中,方便取用。</param>
public static async Task RegisterAsync(string appId, string appSecret, string name = null)
{
//记录注册信息,RegisterFunc委托内的过程会在缓存丢失之后自动重试
RegisterFunc = () =>
RegisterFunc = async () =>
{
//using (FlushCache.CreateInstance())
//{
Expand All @@ -181,11 +196,12 @@ public static void Register(string appId, string appSecret, string name = null)
AccessTokenExpireTime = DateTimeOffset.MinValue,
AccessTokenResult = new AccessTokenResult()
};
Update(appId, bag, null);//第一次添加,此处已经立即更新
await UpdateAsync(appId, bag, null);//第一次添加,此处已经立即更新
return bag;
//}
};
RegisterFunc();

var taskRegister = RegisterFunc();

if (!name.IsNullOrEmpty())
{
Expand All @@ -199,6 +215,7 @@ public static void Register(string appId, string appSecret, string name = null)
//OAuthAccessTokenContainer进行自动注册
OAuthAccessTokenContainer.Register(appId, appSecret, name);

Task.WaitAll(new[] { taskRegister });
}

#region 同步方法
Expand Down Expand Up @@ -278,9 +295,9 @@ public static AccessTokenResult GetAccessTokenResult(string appId, bool getNewTo
/// <returns></returns>
public static async Task<string> TryGetAccessTokenAsync(string appId, string appSecret, bool getNewToken = false)
{
if (!CheckRegistered(appId) || getNewToken)
if (!await CheckRegisteredAsync(appId) || getNewToken)
{
Register(appId, appSecret);
await RegisterAsync(appId, appSecret);
}
return await GetAccessTokenAsync(appId, getNewToken);
}
Expand All @@ -305,14 +322,14 @@ public static async Task<string> GetAccessTokenAsync(string appId, bool getNewTo
/// <returns></returns>
public static async Task<IAccessTokenResult> GetAccessTokenResultAsync(string appId, bool getNewToken = false)
{
if (!CheckRegistered(appId))
if (! await CheckRegisteredAsync(appId))
{
throw new UnRegisterAppIdException(appId, string.Format("此appId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!", appId));
}

var accessTokenBag = TryGetItem(appId);

using (Cache.BeginCacheLock(LockResourceName, appId))//同步锁
using (await Cache.BeginCacheLockAsync(LockResourceName, appId))//同步锁
{
if (getNewToken || accessTokenBag.AccessTokenExpireTime <= SystemTime.Now)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
修改标识:Senparc - 20181226
修改描述:v16.6.2 修改 DateTime 为 DateTimeOffset
修改标识:Senparc - 20190418
修改描述:v17.0.0 支持异步 Container
----------------------------------------------------------------*/

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net35;net40;net45;netstandard2.0</TargetFrameworks>
<Version>16.6.15</Version>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>16.7.0-preview1</Version>
<AssemblyName>Senparc.Weixin.MP</AssemblyName>
<RootNamespace>Senparc.Weixin.MP</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -460,6 +460,9 @@
v16.6.13 添加 SendMenu 相关接口,并打通消息回复响应
v16.6.14 支持最新版本 Senparc.Weixin
v16.6.15 Card_BaseInfoBase 添加 get_custom_code_mode 属性
v16.7.0
1、支持异步 Container
2、停止对 .NET 3.5 和 .NET 4.0 的支持
</PackageReleaseNotes>
<RepositoryUrl>https://github.com/JeffreySu/WeiXinMPSDK</RepositoryUrl>
</PropertyGroup>
Expand Down

0 comments on commit 4566129

Please sign in to comment.