Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer #1722

Merged
merged 11 commits into from
Apr 29, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
<!--<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>-->

<Version>3.3.3</Version>
<Version>3.4.0</Version>
<!--<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>-->
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
Expand All @@ -18,8 +18,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
<PackageReference Include="Senparc.CO2NET" Version="0.6.1-preview1" />
<PackageReference Include="Senparc.NeuChar.App" Version="0.3.5-preview1" />
<PackageReference Include="Senparc.CO2NET" Version="0.7.0-preview3" />
<PackageReference Include="Senparc.NeuChar.App" Version="0.3.5-preview3" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="ZXing.Net" Version="0.16.4" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview1" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview3" />
<PackageReference Include="System.Diagnostics.FileVersionInfo" Version="4.3.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ https://github.com/JeffreySu/WeiXinMPSDK</Description>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.CO2NET.Cache.Memcached" Version="3.3.0-preview1" />
<PackageReference Include="Senparc.CO2NET.Cache.Memcached" Version="3.3.0-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>7.2.8.2-preview1</Version>
<Version>7.2.8-preview2</Version>
<AssemblyName>Senparc.Weixin.MP.MvcExtension</AssemblyName>
<RootNamespace>Senparc.Weixin.MP.MvcExtension</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -159,7 +159,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview1" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
using System.Threading.Tasks;
using Senparc.Weixin.MP.Test.CommonAPIs;
using System.Threading;
using System.Collections.Generic;
using Senparc.CO2NET.Extensions;
using Senparc.CO2NET.Cache;
using Senparc.CO2NET.Cache.Redis;
using Senparc.Weixin.MP.Containers;

namespace Senparc.Weixin.MP.Test.AdvancedAPIs
{
Expand All @@ -17,13 +22,14 @@ public void TestMethod1()
Console.WriteLine("1. Start");

bool finished = false;
Thread thread = new Thread(()=> {
Thread thread = new Thread(() =>
{
Task.Factory.StartNew(async () =>
{
var d2 = SystemTime.Now;
var tagJsonResult = await Senparc.Weixin.MP.AdvancedAPIs.UserTagApi.GetAsync(base._appId);
Console.WriteLine("3. tagJsonResult 1:" + string.Join(",", tagJsonResult.tags.Select(z => z.name)));
Console.WriteLine("4. 用时:" + (SystemTime.Now-d2).TotalMilliseconds+" ms");
Console.WriteLine("4. 用时:" + (SystemTime.Now - d2).TotalMilliseconds + " ms");

return tagJsonResult;
}).ContinueWith(async task =>
Expand All @@ -45,7 +51,81 @@ public void TestMethod1()
Thread.Sleep(5);
}

Console.WriteLine("6. End:"+(SystemTime.Now -d1).TotalMilliseconds+" ms");
Console.WriteLine("6. End:" + (SystemTime.Now - d1).TotalMilliseconds + " ms");

}


/// <summary>
/// 并发测试
/// </summary>
[TestMethod]
public void ConcurrentTesting()
{
var threadCount = 100;//同时运行线程数
var finishedCount = 0;
var threads = new List<Thread>();

var apiTotalTime = 0D;//接口总耗时

//指定缓存
Senparc.Weixin.Cache.Redis.Register.RegisterDomainCache();//注册领域缓存
CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);

var cacheStrategy = CacheStrategyFactory.GetObjectCacheStrategyInstance();
Console.WriteLine($"== 开始,缓存:{cacheStrategy.GetType()} ==");

//先获取一次AccessToken(避免并发过程中获取) ——无效
var accessToken = AccessTokenContainer.GetAccessTokenResult(base._appId).access_token;

object objLock = new object();

for (int i = 0; i < threadCount; i++)
{
var index = i;
var thread = new Thread(async () =>
{
try
{
var dt0 = SystemTime.Now;
var threadName = Thread.CurrentThread.Name;
Console.WriteLine($"{SystemTime.Now.ToString("HH:mm:ss.ffffff")}\t[{threadName}] START");
var result = await MP.AdvancedAPIs.QrCodeApi.CreateAsync(base._appId, 300, 100000 * 10 + index, QrCode_ActionName.QR_SCENE);
var apiRunTime = (SystemTime.Now - dt0).TotalMilliseconds;
Console.WriteLine($"{SystemTime.Now.ToString("HH:mm:ss.ffffff")}\t[{threadName}] RESULT - 耗时 {apiRunTime:###,###}ms - {result.url}");
apiTotalTime += apiRunTime;
}
catch (Exception ex)
{
Console.WriteLine($"{SystemTime.Now.ToString("HH:mm:ss.ffffff")}\t{ex.ToString()}");
}
finally
{
lock (objLock)
{
finishedCount++;
}
}
});
thread.Name = "T" + index.ToString("00");

threads.Add(thread);
}

var dt1 = SystemTime.Now;
foreach (var thread in threads)
{
thread.Start();//尽量在相近的时间开始
//Thread.Sleep(500);//TODO:如果Sleep,则可以快速完成(没有阻塞)
}

while (finishedCount < threadCount)
{
//等待线程结束(为测试避免线程锁死,这里不使用信号灯)
}

var costTime = (SystemTime.Now - dt1).TotalMilliseconds;
Console.WriteLine($"== 运行结束,总耗时 {costTime:###,###}ms,平均 {apiTotalTime / threadCount:###,###}ms ==");

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ and limitations under the License.

修改标识:Senparc - 20180917
修改描述:BaseContainer.GetFirstOrDefaultAppId() 方法添加 PlatformType 属性


修改标识:Senparc - 20190429
修改描述:v16.7.1 重构异步 ApiHandlerWapper
----------------------------------------------------------------*/

using System;
Expand Down Expand Up @@ -219,11 +221,11 @@ public static T TryCommonApi<T>(Func<string, T> fun, string accessTokenOrAppId =
/// <returns></returns>
public static async Task<T> TryCommonApiAsync<T>(Func<string, Task<T>> fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxJsonResult
{
Func<string> accessTokenContainer_GetFirstOrDefaultAppIdFunc =
() => AccessTokenContainer.GetFirstOrDefaultAppId(PlatformType.MP);
Func<Task<string>> accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc =
async () => await AccessTokenContainer.GetFirstOrDefaultAppIdAsync(PlatformType.MP);

Func<string, bool> accessTokenContainer_CheckRegisteredFunc =
appId => AccessTokenContainer.CheckRegistered(appId);
Func<string, Task<bool>> accessTokenContainer_CheckRegisteredAsyncFunc =
async appId => await AccessTokenContainer.CheckRegisteredAsync(appId);

Func<string, bool, Task<IAccessTokenResult>> accessTokenContainer_GetAccessTokenResultAsyncFunc =
(appId, getNewToken) => AccessTokenContainer.GetAccessTokenResultAsync(appId, getNewToken);
Expand All @@ -233,8 +235,8 @@ public static async Task<T> TryCommonApiAsync<T>(Func<string, Task<T>> fun, stri
var result = ApiHandlerWapperBase.
TryCommonApiBaseAsync(
PlatformType.MP,
accessTokenContainer_GetFirstOrDefaultAppIdFunc,
accessTokenContainer_CheckRegisteredFunc,
accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc,
accessTokenContainer_CheckRegisteredAsyncFunc,
accessTokenContainer_GetAccessTokenResultAsyncFunc,
invalidCredentialValue,
fun, accessTokenOrAppId, retryIfFaild);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>16.7.0.2-preview1</Version>
<Version>16.7.1-preview2</Version>
<AssemblyName>Senparc.Weixin.MP</AssemblyName>
<RootNamespace>Senparc.Weixin.MP</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -547,8 +547,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.CO2NET.APM" Version="0.3.0-preview1" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview1" />
<PackageReference Include="Senparc.CO2NET.APM" Version="0.3.0-preview3" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>4.5.0.2-preview1</Version>
<Version>4.5.0-preview2</Version>
<AssemblyName>Senparc.Weixin.Open</AssemblyName>
<RootNamespace>Senparc.Weixin.Open</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -210,7 +210,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview1" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ and limitations under the License.
修改描述:添加TryCommonApi()方法

修改标识:Senparc - 20170102
修改描述:v14.3.116 TryCommonApi抛出ErrorJsonResultException、WeixinException异常时加入了accessTokenOrAppId参数
修改描述:MP v14.3.116 TryCommonApi抛出ErrorJsonResultException、WeixinException异常时加入了accessTokenOrAppId参数

修改标识:Senparc - 20170123
修改描述:v14.3.121 TryCommonApiAsync方法返回代码改为return await result;避免死锁。
修改描述:MP v14.3.121 TryCommonApiAsync方法返回代码改为return await result;避免死锁。

修改标识:Senparc - 20180917
修改描述:BaseContainer.GetFirstOrDefaultAppId() 方法添加 PlatformType 属性

修改标识:Senparc - 20190429
修改描述:v3.5.1 重构异步 ApiHandlerWapper

----------------------------------------------------------------*/

using System;
Expand Down Expand Up @@ -124,16 +127,16 @@ public static T TryCommonApi<T>(Func<string, T> fun, string accessTokenOrAppKey,
/// <returns></returns>
public static async Task<T> TryCommonApiAsync<T>(Func<string, Task<T>> fun, string accessTokenOrAppKey, bool retryIfFaild = true) where T : WorkJsonResult
{
Func<string> accessTokenContainer_GetFirstOrDefaultAppIdFunc =
() => AccessTokenContainer.GetFirstOrDefaultAppId(PlatformType.Work);
Func<Task<string>> accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc =
async () => await AccessTokenContainer.GetFirstOrDefaultAppIdAsync(PlatformType.Work);

Func<string, bool> accessTokenContainer_CheckRegisteredFunc =
appKey =>
Func<string, Task<bool>> accessTokenContainer_CheckRegisteredAsyncFunc =
async appKey =>
{
/*
* 对于企业微信来说,AppId = key = CorpId+'@'+CorpSecret
*/
return AccessTokenContainer.CheckRegistered(appKey);
return await AccessTokenContainer.CheckRegisteredAsync(appKey);
};

Func<string, bool, Task<IAccessTokenResult>> accessTokenContainer_GetAccessTokenResultAsyncFunc =
Expand All @@ -150,8 +153,8 @@ public static async Task<T> TryCommonApiAsync<T>(Func<string, Task<T>> fun, stri
var result = ApiHandlerWapperBase.
TryCommonApiBaseAsync(
PlatformType.Work,
accessTokenContainer_GetFirstOrDefaultAppIdFunc,
accessTokenContainer_CheckRegisteredFunc,
accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc,
accessTokenContainer_CheckRegisteredAsyncFunc,
accessTokenContainer_GetAccessTokenResultAsyncFunc,
invalidCredentialValue,
fun, accessTokenOrAppKey, retryIfFaild);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<FrameworkPathOverride Condition="'$(TargetFramework)' == 'net35'">$(Net35FrameworkPathOverride)</FrameworkPathOverride>
<Version>3.5.0.2-preview1</Version>
<Version>3.5.0-preview2</Version>
<AssemblyName>Senparc.Weixin.Work</AssemblyName>
<RootNamespace>Senparc.Weixin.Work</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -224,7 +224,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview1" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>3.4.0.2-preview1</Version>
<Version>3.4.0-preview2</Version>
<AssemblyName>Senparc.Weixin.WxOpen</AssemblyName>
<RootNamespace>Senparc.Weixin.WxOpen</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -185,7 +185,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview1" />
<PackageReference Include="Senparc.NeuChar" Version="0.6.5-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ and limitations under the License.

修改标识:Senparc - 20181027
修改描述:v6.1.10 改进 TryCommonApiBase 方法

修改标识:Senparc - 20190429
修改描述:v6.4.0 重构异步 ApiHandlerWapper

----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -168,8 +172,8 @@ public static T TryCommonApiBase<T>(
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="platformType">平台类型,PlatformType枚举</param>
/// <param name="accessTokenContainer_GetFirstOrDefaultAppIdFunc">AccessTokenContainer中的GetFirstOrDefaultAppId()方法</param>
/// <param name="accessTokenContainer_CheckRegisteredFunc">AccessTokenContainer中的bool CheckRegistered(appId,getNew)方法</param>
/// <param name="accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc">AccessTokenContainer中的GetFirstOrDefaultAppId()方法</param>
/// <param name="accessTokenContainer_CheckRegisteredAsyncFunc">AccessTokenContainer中的bool CheckRegistered(appId,getNew)方法</param>
/// <param name="accessTokenContainer_GetAccessTokenResultAsyncFunc">AccessTokenContainer中的AccessTokenResult GetAccessTokenResultAsync(appId)方法(异步方法)</param>
/// <param name="invalidCredentialValue">"ReturnCode.获取access_token时AppSecret错误或者access_token无效"枚举的值</param>
/// <param name="fun"></param>
Expand All @@ -178,8 +182,8 @@ public static T TryCommonApiBase<T>(
/// <returns></returns>
public static async Task<T> TryCommonApiBaseAsync<T>(
PlatformType platformType,
Func<string> accessTokenContainer_GetFirstOrDefaultAppIdFunc,
Func<string, bool> accessTokenContainer_CheckRegisteredFunc,
Func<Task<string>> accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc,
Func<string, Task<bool>> accessTokenContainer_CheckRegisteredAsyncFunc,
Func<string, bool, Task<IAccessTokenResult>> accessTokenContainer_GetAccessTokenResultAsyncFunc,
int invalidCredentialValue,
Func<string, Task<T>> fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : BaseJsonResult
Expand All @@ -194,7 +198,7 @@ public static async Task<T> TryCommonApiBaseAsync<T>(

if (accessTokenOrAppId == null)
{
appId = accessTokenContainer_GetFirstOrDefaultAppIdFunc();// AccessTokenContainer.GetFirstOrDefaultAppId();
appId = await accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc();// AccessTokenContainer.GetFirstOrDefaultAppId();
if (appId == null)
{
throw new UnRegisterAppIdException(null,
Expand All @@ -204,7 +208,7 @@ public static async Task<T> TryCommonApiBaseAsync<T>(
else if (ApiUtility.IsAppId(accessTokenOrAppId, platformType))
{
//if (!AccessTokenContainer.CheckRegistered(accessTokenOrAppId))
if (!accessTokenContainer_CheckRegisteredFunc(accessTokenOrAppId))
if (!await accessTokenContainer_CheckRegisteredAsyncFunc(accessTokenOrAppId))
{
throw new UnRegisterAppIdException(accessTokenOrAppId,
string.Format("此appId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!",
Expand Down Expand Up @@ -243,8 +247,8 @@ public static async Task<T> TryCommonApiBaseAsync<T>(
accessToken = accessTokenResult.access_token;

result = await TryCommonApiBaseAsync(platformType,
accessTokenContainer_GetFirstOrDefaultAppIdFunc,
accessTokenContainer_CheckRegisteredFunc,
accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc,
accessTokenContainer_CheckRegisteredAsyncFunc,
accessTokenContainer_GetAccessTokenResultAsyncFunc,
invalidCredentialValue,
fun, appId, false);
Expand Down
Loading