Skip to content

Commit

Permalink
v1.6.11 - 修复 AsyncLocal 可能上下文丢失的 bug;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 6, 2023
1 parent 92b75db commit c603d07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
15 changes: 15 additions & 0 deletions examples/net60_webapi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FreeSql;
using FreeSql.Cloud.Abstract;
using FreeSql.Internal;
using net60_webapi;
using Rougamo.Context;
Expand Down Expand Up @@ -46,9 +47,14 @@
await context.Response.WriteAsync("hello word");
});

AsyncLocalAccessor<int> access = new AsyncLocalAccessor<int>(() => 100);
await access.SetValue();
Console.WriteLine($"ValueAccessor before await FooAsync in Main: {access.Value}");

test();
app.Run();


void test()
{
var fsql = new FreeSqlCloud2();
Expand Down Expand Up @@ -224,4 +230,13 @@ void _OnExit()
}
}
}
}

public static class Extension
{
public static async Task SetValue(this AsyncLocalAccessor<int> face)
{
face.Value = 200;
await Task.Delay(100);
}
}
25 changes: 17 additions & 8 deletions src/FreeSql.Cloud/Abstract/FreeSqlCloudBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,40 @@ public class DBKeyString

public class AsyncLocalAccessor<T>
{
Func<T> _defaultValue;
public AsyncLocalAccessor(Func<T> defaultValue)
Func<T> _defaultValue;
public AsyncLocalAccessor(Func<T> defaultValue)
{
_defaultValue = defaultValue;
if (_asyncLocal.Value == null) _asyncLocal.Value = new ValueHolder { DefaultValue = _defaultValue };
}
public T Value
{
get
{
if (_asyncLocal.Value != null) return _asyncLocal.Value.Value;
if (_defaultValue != null) return _defaultValue();
if (_asyncLocal.Value != null) return _asyncLocal.Value.GetValue();
return default;
}
set
{
if (_asyncLocal.Value == null) _asyncLocal.Value = new ValueHolder();
_asyncLocal.Value.Value = value;
if (_asyncLocal.Value == null) _asyncLocal.Value = new ValueHolder { DefaultValue = _defaultValue };
_asyncLocal.Value.SetValue(value);
}

}

class ValueHolder
{
public T Value { get; set; }
}
T _rawValue;
bool _rawValueChanged = false;
public Func<T> DefaultValue { get; set; }

public T GetValue() => _rawValueChanged ? _rawValue : DefaultValue();
public void SetValue(T value)
{
_rawValueChanged = true;
_rawValue = value;
}
}
#if net40
ThreadLocal<ValueHolder> _asyncLocal = new ThreadLocal<ValueHolder>();
#else
Expand Down
7 changes: 6 additions & 1 deletion src/FreeSql.Cloud/FreeSql.Cloud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard20;net40</TargetFrameworks>
<Version>1.6.10</Version>
<Version>1.6.11</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+.</Description>
Expand All @@ -18,8 +18,13 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<PackageReadmeFile>readme.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="../../readme.md" Pack="true" PackagePath="\"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="FreeSql.DbContext" Version="3.2.666" />
<PackageReference Include="FreeScheduler" Version="1.1.0" />
Expand Down

0 comments on commit c603d07

Please sign in to comment.