【SqlSugarCore】SqlSugarScope的异步上下文问题

前言

在使用SqlSugarCore时,偶尔会出现报错:

English Message : Connection open error . A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 远程主机强迫关闭了一个现有的连接。)

去SqlSugarCore官网寻找答案,看到了这个文章,随即有了下面的测试

https://www.donet5.com/Ask/9/13824

测试

环境

Core3.1的控制台程序

引用了SqlSugarCore-5.0.3.4类库

 1 using ConTestSqlSugar.Models;
 2 using SqlSugar;
 3 using System;
 4 using System.Threading;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConTestSqlSugar
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             _ = TestAsync();
14 
15             Console.ReadKey();
16         }
17 
18         static async Task TestAsync()
19         {
20             SqlSugarScope sqlSugarScope = new SqlSugarScope(new ConnectionConfig()
21             {
22                 DbType = DbType.SqlServer,
23                 ConnectionString = "server=.;database=TestDB;uid=sa;pwd=123456;",
24                 IsAutoCloseConnection = true,
25                 InitKeyType = InitKeyType.Attribute
26             });
27 
28             var data = sqlSugarScope.Queryable<UserInfo>().ToList();
29 
30             Console.WriteLine("   run-1--->:" + sqlSugarScope.ContextID);
31 
32             await Task.Run(() =>
33             {
34                 Console.WriteLine(" await-2--->:" + sqlSugarScope.ContextID);
35             });
36 
37             await Task.Run(() =>
38             {
39                 Console.WriteLine(" await-5--->:" + sqlSugarScope.ContextID);
40             });
41 
42             {
43                 Thread thread = new Thread(() =>
44                 {
45                     Console.WriteLine("thread-3--->:" + sqlSugarScope.ContextID);
46                 });
47                 thread.IsBackground = true;
48                 thread.Start();
49             }
50 
51             await Task.Run(() =>
52             {
53                 Console.WriteLine(" await-6--->:" + sqlSugarScope.ContextID);
54             });
55 
56             {
57                 Thread thread = new Thread(() =>
58                 {
59                     Console.WriteLine("thread-8--->:" + sqlSugarScope.ContextID);
60                 });
61                 thread.IsBackground = true;
62                 thread.Start();
63             }
64 
65             await Task.Run(() =>
66             {
67                 Console.WriteLine(" await-7--->:" + sqlSugarScope.ContextID);
68             });
69 
70             Console.WriteLine("   run-4--->:" + sqlSugarScope.ContextID);
71 
72             {
73                 Thread thread = new Thread(() =>
74                 {
75                     Console.WriteLine("thread-9--->:" + sqlSugarScope.ContextID);
76                 });
77                 thread.IsBackground = true;
78                 thread.Start();
79             }
80         }
81     }
82 }

Debug模式

     run-1--->:6f7930b6-5a51-445c-b470-722bcebfb1f1
  await-2--->:33038044-0d1e-433e-b078-707eae5f37bf
  await-5--->:e755c4be-5e63-47a3-a21f-4ce9c51e47f2
  await-6--->:33038044-0d1e-433e-b078-707eae5f37bf
thread-3--->:482f0e69-4ffa-4ec6-8a04-333b2d146edd
  await-7--->:e755c4be-5e63-47a3-a21f-4ce9c51e47f2
thread-8--->:661c6ecd-394b-4e45-b121-1f94bcc4ec0c
     run-4--->:6f7930b6-5a51-445c-b470-722bcebfb1f1
thread-9--->:7d84a287-8988-4ba1-b97c-e64ea4e26605

Release模式

     run-1--->:d06d78fd-3737-4a4d-916d-8cc237a3656b
  await-2--->:dd1f3f92-90b4-4199-9d94-5af90943149c
  await-5--->:dd1f3f92-90b4-4199-9d94-5af90943149c
  await-6--->:dd1f3f92-90b4-4199-9d94-5af90943149c
thread-3--->:82f1da24-691b-4ae8-90d4-a09053be93f8
  await-7--->:dd1f3f92-90b4-4199-9d94-5af90943149c
thread-8--->:f5b03194-4355-4581-806a-a56c4a8dc14b
     run-4--->:d06d78fd-3737-4a4d-916d-8cc237a3656b
thread-9--->:67820468-ba1a-4eb8-a06d-6c65853e7304

总结

备注:仅是对上面测试的一个小总结,并不是对我一开始的问题的处理方案

1、run

同步运行,拿到的始终是同一个上下文

2、await

(1)Debug模式下,会有多个上下文,但是会拿到重复的上下文

(2)Release模式下,拿到的都是同一个上下文

3、Thread

无论是Debug模式或Release模式,均会拿到不同的上下文

原文地址:https://www.cnblogs.com/masonblog/p/15079951.html