前言
在使用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模式,均會拿到不同的上下文