apevolo-api
apevolo-api copied to clipboard
nuget引用的库可以全部升级下
\trunk\ApeVolo.Api\ApeVolo.Api.csproj \trunk\ApeVolo.Repository\ApeVolo.Repository.csproj \trunk\ApeVolo.Common\ApeVolo.Common.csproj \trunk\ApeVolo.Entity\ApeVolo.Entity.csproj
全部升级以后,========== 全部重新生成: 成功 8 个,失败 0 个,跳过 0 个 ==========
不过没有测试
/// <summary>
/// Creates a salt
/// </summary>
/// <param name="size">A salt size</param>
/// <returns>A salt</returns>
public static string CreateSalt(int size)
{
#pragma warning disable CS0618
var provider = new RNGCryptoServiceProvider();
#pragma warning restore CS0618
byte[] data = new byte[size];
provider.GetBytes(data);
return Convert.ToBase64String(data);
}
warning SYSLIB0023: “RNGCryptoServiceProvider”已过时:“RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead.”
/// <summary>
/// Creates a salt
/// </summary>
/// <param name="size">A salt size</param>
/// <returns>A salt</returns>
public static string CreateSalt(int size)
{
var random = RandomNumberGenerator.Create();
byte[] bytes = new byte[size];
random.GetNonZeroBytes(bytes);
//return BitConverter.ToString(bytes);
return Convert.ToBase64String(bytes);
}
await Task.Run(async () =>
{
if (parameterInfos.Length == 0)
{
consumerExecutorDescriptor.MethodInfo.Invoke(obj, null);
}
else
{
object[] parameters = { res };
consumerExecutorDescriptor.MethodInfo.Invoke(obj, parameters);
}
});
warning CS1998: 此异步方法缺少 "await" 运算符,将以同步方式运行。请考虑使用 "await" 运算符等待非阻止的 API 调用,或者使用 "await Task.Run(...)" 在后台线程上执行占用大量 CPU 的工作。
是不是需要去掉async ?
await Task.Run(() =>
{
if (parameterInfos.Length == 0)
{
consumerExecutorDescriptor.MethodInfo.Invoke(obj, null);
}
else
{
object[] parameters = { res };
consumerExecutorDescriptor.MethodInfo.Invoke(obj, parameters);
}
});
/// <summary>
/// 计算SHA1摘要
/// </summary>
/// <param name="str">字符串</param>
/// <param name="encoding">编码</param>
/// <returns></returns>
public static byte[] ToSha1Bytes(this string str, Encoding encoding)
{
#pragma warning disable CS0618
SHA1 sha1 = new SHA1CryptoServiceProvider();
#pragma warning restore CS0618
byte[] inputBytes = encoding.GetBytes(str);
byte[] outputBytes = sha1.ComputeHash(inputBytes);
return outputBytes;
}
“SHA1CryptoServiceProvider”已过时:“Derived cryptographic types are obsolete. Use the Create method on the base type instead.”
修改为:
/// <summary>
/// 计算SHA1摘要
/// </summary>
/// <param name="str">字符串</param>
/// <param name="encoding">编码</param>
/// <returns>返回SHA1摘要</returns>
public static byte[] SHA1(this string str, Encoding encoding)
{
using var sha1 = SHA1.Create();
byte[] inputBytes = encoding.GetBytes(str);
return sha1.ComputeHash(inputBytes);
}
await Task.Run(async () => { if (parameterInfos.Length == 0) { consumerExecutorDescriptor.MethodInfo.Invoke(obj, null); } else { object[] parameters = { res }; consumerExecutorDescriptor.MethodInfo.Invoke(obj, parameters); } });
删除async
tasks.Add(Task.Run(async () =>
{
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var publish = $"queue:{consumerExecutorDescriptor.Attribute.Name}";
var provider = scope.ServiceProvider;
var obj = ActivatorUtilities.GetServiceOrCreateInstance(provider,
consumerExecutorDescriptor.ImplTypeInfo);
ParameterInfo[] parameterInfos = consumerExecutorDescriptor.MethodInfo.GetParameters();
//redis对象
var redis = scope.ServiceProvider.GetService<IRedisCacheService>();
//从zset添加到队列(锁)
tasks.Add(Task.Run(async () =>
{
while (true)
{
var keyInfo = "lockZSetTibos"; //锁名称
var token = Guid.NewGuid().ToString("N"); //锁持有者
var coon = await redis.GetDatabase().LockTakeAsync(keyInfo, token,
TimeSpan.FromSeconds(5));
if (coon)
{
try
{
var dt = DateTime.Now;
var arry = await redis.SortedSetRangeByScoreAsync(
consumerExecutorDescriptor.Attribute.Name, null, dt);
if (arry != null && arry.Length > 0)
{
foreach (var item in arry)
{
await redis.ListLeftPushAsync(publish, item);
}
//移除zset数据
await redis.SortedSetRemoveRangeByScoreAsync(
consumerExecutorDescriptor.Attribute.Name, null, dt);
}
else
{
//线程挂起1s
await Task.Delay(1000);
}
}
catch (System.Exception ex)
{
Console.WriteLine($"执行延迟队列报错:{ex.Message}");
}
finally
{
//释放锁
redis.GetDatabase().LockRelease(keyInfo, token);
}
}
}
}));
//消费队列
tasks.Add(Task.Run(async () =>
{
while (true)
{
try
{
if (options.ShowLog)
{
Console.WriteLine($"执行方法:{obj},key:{publish},执行时间{DateTime.Now}");
}
var count = await redis.ListLengthAsync(publish);
if (count > 0)
{
//从MQ里获取一条消息
var res = await redis.ListRightPopAsync(publish);
if (string.IsNullOrEmpty(res)) continue;
//堵塞
await Task.Delay(options.IntervalTime);
try
{
await Task.Run(() =>
{
if (parameterInfos.Length == 0)
{
consumerExecutorDescriptor.MethodInfo.Invoke(obj, null);
}
else
{
object[] parameters = { res };
consumerExecutorDescriptor.MethodInfo.Invoke(obj, parameters);
}
});
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
//线程挂起1s
await Task.Delay(options.SuspendTime);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}));
}
}));
删除async,使用简单的using语句
tasks.Add(Task.Run(() =>
{
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var publish = $"queue:{consumerExecutorDescriptor.Attribute.Name}";
var provider = scope.ServiceProvider;
var obj = ActivatorUtilities.GetServiceOrCreateInstance(provider,
consumerExecutorDescriptor.ImplTypeInfo);
ParameterInfo[] parameterInfos = consumerExecutorDescriptor.MethodInfo.GetParameters();
//redis对象
var redis = scope.ServiceProvider.GetService<IRedisCacheService>();
//从zset添加到队列(锁)
tasks.Add(Task.Run(async () =>
{
while (true)
{
var keyInfo = "lockZSetTibos"; //锁名称
var token = Guid.NewGuid().ToString("N"); //锁持有者
var coon = await redis.GetDatabase().LockTakeAsync(keyInfo, token,
TimeSpan.FromSeconds(5));
if (coon)
{
try
{
var dt = DateTime.Now;
var arry = await redis.SortedSetRangeByScoreAsync(
consumerExecutorDescriptor.Attribute.Name, null, dt);
if (arry != null && arry.Length > 0)
{
foreach (var item in arry)
{
await redis.ListLeftPushAsync(publish, item);
}
//移除zset数据
await redis.SortedSetRemoveRangeByScoreAsync(
consumerExecutorDescriptor.Attribute.Name, null, dt);
}
else
{
//线程挂起1s
await Task.Delay(1000);
}
}
catch (System.Exception ex)
{
Console.WriteLine($"执行延迟队列报错:{ex.Message}");
}
finally
{
//释放锁
redis.GetDatabase().LockRelease(keyInfo, token);
}
}
}
}));
//消费队列
tasks.Add(Task.Run(async () =>
{
while (true)
{
try
{
if (options.ShowLog)
{
Console.WriteLine($"执行方法:{obj},key:{publish},执行时间{DateTime.Now}");
}
var count = await redis.ListLengthAsync(publish);
if (count > 0)
{
//从MQ里获取一条消息
var res = await redis.ListRightPopAsync(publish);
if (string.IsNullOrEmpty(res)) continue;
//堵塞
await Task.Delay(options.IntervalTime);
try
{
await Task.Run(() =>
{
if (parameterInfos.Length == 0)
{
consumerExecutorDescriptor.MethodInfo.Invoke(obj, null);
}
else
{
object[] parameters = { res };
consumerExecutorDescriptor.MethodInfo.Invoke(obj, parameters);
}
});
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
//线程挂起1s
await Task.Delay(options.SuspendTime);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}));
}));
nuget引用以升级最新