免费企业网站程序asp,福州营销网站建设模板,广安市网站建设,一级a做爰片免费网站天天看官方和老人言#xff0c;asp.net core中尽量用异步#xff0c;为什么呢#xff1f;接下来是个小demo#xff0c;看看同步异步的差别吧#xff0c;或许通过这个demo#xff0c;就明白官方和老人的良苦用心了。1、创建一个sql server的表CREATE TABLE [dbo].[Students]([St… 官方和老人言asp.net core中尽量用异步为什么呢接下来是个小demo看看同步异步的差别吧或许通过这个demo就明白官方和老人的良苦用心了。1、创建一个sql server的表CREATE TABLE [dbo].[Students]([StuNo] [varchar](50) NOT NULL,[Name] [varchar](50) NULL,[CardID] [varchar](18) NULL,[Sex] [varchar](4) NULL,[Birthday] [datetime] NULL,[ClassID] [int] NULL,CONSTRAINT [PK_dbo.Students] PRIMARY KEY CLUSTERED
([StuNo] ASC
)WITH (PAD_INDEX OFF, STATISTICS_NORECOMPUTE OFF, IGNORE_DUP_KEY OFF, ALLOW_ROW_LOCKS ON, ALLOW_PAGE_LOCKS ON) ON [PRIMARY]
) ON [PRIMARY]
GO
2、创建一个asp.net core api项目5.0的using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;namespace AsyncWebAPI.Controllers
{[ApiController][Route([controller])]public class StudentController : ControllerBase{private readonly ILoggerStudentController _logger;public StudentController(ILoggerStudentController logger){_logger logger;}[HttpDelete(/deleteall)]public bool DeleteAll(){_logger.LogInformation(删除全部);using var con new SqlConnection(server.;databaseTestManageDB;uidsa;pwdsa;);var sql delete from [dbo].[Students];var cmd new SqlCommand(sql, con);con.Open();var result cmd.ExecuteNonQuery();con.Close();return true;}[HttpPost(/addstudent)]public Student AddEntity([FromBody] Student student){_logger.LogInformation(同步添加);return SavaEntity(student);}Student SavaEntity(Student student){student.StuNo Guid.NewGuid().ToString();using var con new SqlConnection(server.;databaseTestManageDB;uidsa;pwdsa;);var sql INSERT INTO [dbo].[Students]([StuNo],[Name],[CardID],[Sex],[Birthday],[ClassID])VALUES(StuNo,Name,CardID,Sex,Birthday,ClassID);var cmd new SqlCommand(sql, con);cmd.Parameters.Add(new SqlParameter { ParameterName StuNo, Value student.StuNo, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName Name, Value student.Name, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName CardID, Value student.CardID, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName Sex, Value student.Sex, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName Birthday, Value student.Birthday, SqlDbType System.Data.SqlDbType.DateTime });cmd.Parameters.Add(new SqlParameter { ParameterName ClassID, Value student.ClassID, SqlDbType System.Data.SqlDbType.Int });con.Open();var result cmd.ExecuteNonQuery();con.Close();return student;}[HttpPost(/addstudentasync)]public async TaskStudent AddEntityAsync([FromBody] Student student){_logger.LogInformation(异步添加);return await SavaEntityAsync(student);}async TaskStudent SavaEntityAsync(Student student){student.StuNo Guid.NewGuid().ToString();using var con new SqlConnection(server.;databaseTestManageDB;uidsa;pwdsa;);var sql INSERT INTO [dbo].[Students]([StuNo],[Name],[CardID],[Sex],[Birthday],[ClassID])VALUES(StuNo,Name,CardID,Sex,Birthday,ClassID);var cmd new SqlCommand(sql, con);cmd.Parameters.Add(new SqlParameter { ParameterName StuNo, Value student.StuNo, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName Name, Value student.Name, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName CardID, Value student.CardID, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName Sex, Value student.Sex, SqlDbType System.Data.SqlDbType.VarChar });cmd.Parameters.Add(new SqlParameter { ParameterName Birthday, Value student.Birthday, SqlDbType System.Data.SqlDbType.DateTime });cmd.Parameters.Add(new SqlParameter { ParameterName ClassID, Value student.ClassID, SqlDbType System.Data.SqlDbType.Int });await con.OpenAsync();var result await cmd.ExecuteNonQueryAsync();await con.CloseAsync();return student;}}public class Student{public string StuNo { get; set; }public string Name { get; set; }public string CardID { get; set; }public string Sex { get; set; }public DateTime Birthday { get; set; }public int ClassID { get; set; }}
}3、创建一个控制台程序了是.net core 5.0的using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;namespace AsyncRquestClient
{class ProgramAsync{static int times 100;static async Task Main(string[] args){while (true){Console.WriteLine(输入循环次数);times int.Parse(Console.ReadLine());#region 同步Console.WriteLine(-----------------同步调同步API------------------);SyncCallSyncAPI();Console.ReadLine();Console.WriteLine(-----------------同步调异步API------------------);SyncCallAsyncAPI();Console.ReadLine();Console.WriteLine(-----------------TaskFactory同步调同步API------------------);TaskFactorySyncCallSyncAPI();Console.ReadLine();Console.WriteLine(-----------------TaskFactory同步调异步API------------------);TaskFactorySyncCallAsyncAPI();Console.ReadLine();#endregion#region 异步Console.WriteLine(-----------------异步调异步API------------------);await AsyncCallAsyncAPI();Console.ReadLine();Console.WriteLine(-----------------异步调同步API------------------);await AsyncCallSyncAPI();Console.ReadLine();Console.WriteLine(-----------------TaskFactory异步调异步API------------------);await TaskFactoryAsyncCallAsyncAPI();Console.ReadLine();Console.WriteLine(-----------------TaskFactory异步调同步API------------------);await TaskFactoryAsyncCallSyncAPI();Console.ReadLine();#endregion}}#region 异常/// summary/// 异步调异步API/// /summary/// returns/returnsasync static Task AsyncCallAsyncAPI(){var r DeleteAllAsync().Result;Console.WriteLine($异步调异步API开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudentasync);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var stu JsonConvert.DeserializeObjectStudent(content);}else{var content response.Content.ReadAsStringAsync().Result;Console.WriteLine(同步调同步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}}}/// summary/// TaskFactory异步调异步API/// /summary/// returns/returnsstatic async Task TaskFactoryAsyncCallAsyncAPI(){var r await DeleteAllAsync();Console.WriteLine($TaskFactory异步调异步开API始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){await Task.Factory.StartNew(async () {try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudentasync);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var stu JsonConvert.DeserializeObjectStudent(content);}else{var content await response.Content.ReadAsStringAsync();Console.WriteLine(异步调异步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}});}}/// summary/// 异步调同步API/// /summary/// returns/returnsasync static Task AsyncCallSyncAPI(){var r DeleteAllAsync().Result;Console.WriteLine($异步调同步API开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudent);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var stu JsonConvert.DeserializeObjectStudent(content);}else{var content response.Content.ReadAsStringAsync().Result;Console.WriteLine(同步调同步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}}}/// summary/// TaskFactory异步调同步API/// /summary/// returns/returnsstatic async Task TaskFactoryAsyncCallSyncAPI(){var r await DeleteAllAsync();Console.WriteLine($TaskFactory异步调同步API开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){await Task.Factory.StartNew(async () {try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudent);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var stu JsonConvert.DeserializeObjectStudent(content);}else{var content await response.Content.ReadAsStringAsync();Console.WriteLine(异步调异步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}});}}#endregion#region 同步/// summary/// 同步调同步API/// /summarystatic void SyncCallSyncAPI(){var r DeleteAllAsync().Result;Console.WriteLine($同步调同步开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudent);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response client.SendAsync(request).Result;if (response.IsSuccessStatusCode){var content response.Content.ReadAsStringAsync().Result;var stu JsonConvert.DeserializeObjectStudent(content);}else{var content response.Content.ReadAsStringAsync().Result;Console.WriteLine(同步调同步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}}}static void TaskFactorySyncCallSyncAPI(){var r DeleteAllAsync().Result;Console.WriteLine($TaskFactory异步调同步API开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){var result Task.Factory.StartNew(async () {try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudent);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var stu JsonConvert.DeserializeObjectStudent(content);}else{var content await response.Content.ReadAsStringAsync();Console.WriteLine(异步调异步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}}).Result;}}/// summary/// 同步调异步API/// /summarystatic void SyncCallAsyncAPI(){var r DeleteAllAsync().Result;Console.WriteLine($同步调异步开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudentasync);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response client.SendAsync(request).Result;if (response.IsSuccessStatusCode){var content response.Content.ReadAsStringAsync().Result;var stu JsonConvert.DeserializeObjectStudent(content);}else{var content response.Content.ReadAsStringAsync().Result;Console.WriteLine(同步调异步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}}}static void TaskFactorySyncCallAsyncAPI(){var r DeleteAllAsync().Result;Console.WriteLine($TaskFactory异步调同步API开始时间{DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.ffff)});for (int i 1; i times; i){var result Task.Factory.StartNew(async () {try{var student new Student { Name 张三 i, Birthday DateTime.Now, CardID C0000 i, ClassID 1, Sex 男 };using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Post, addstudentasync);request.Content new StringContent(JsonConvert.SerializeObject(student), Encoding.UTF8, application/json);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var stu JsonConvert.DeserializeObjectStudent(content);}else{var content await response.Content.ReadAsStringAsync();Console.WriteLine(异步调异步添加错误返回值 content);}}catch (Exception exc){Console.WriteLine(exc.Message);}}).Result;}}#endregionstatic async Taskbool DeleteAllAsync(){using var client new HttpClient();client.BaseAddress new Uri(https://localhost:5001);var request new HttpRequestMessage(HttpMethod.Delete, deleteall);var response await client.SendAsync(request);if (response.IsSuccessStatusCode){var content await response.Content.ReadAsStringAsync();var result JsonConvert.DeserializeObjectbool(content);return result;}else{var content await response.Content.ReadAsStringAsync();Console.WriteLine(删除错误返回值 content);return false;}}}public class Student{public string StuNo { get; set; }public string Name { get; set; }public string CardID { get; set; }public string Sex { get; set; }public DateTime Birthday { get; set; }public int ClassID { get; set; }}}4、在sql查询分析器中用这里的语句采集结果--检查记录数是否完整
select count(*) from students;
--查询时间隔
select datediff(
millisecond,
(select min(birthday) as mi from students),
(select max(birthday) as ma from students)
);
结果如下1000次请求同步调同步API同步调异步APITaskFactory同步调同步APITaskFactory同步调异步API异步调异步API异步调同步APITaskFactory异步调异步APITaskFactory异步调同步API1次毫秒31903574390374339331403563872次毫秒31943324386454337031534773563次毫秒33503343443397332331964174064次毫秒32073340360423320630834334035次毫秒3214334742649031673136430387平均32313385.6401427.63291.83141.6422.6387.8客户端调用异步优势明显在所有的调用中服务端的同步要优于异步。