当前位置: 首页 > news >正文

免费企业网站程序asp福州营销网站建设模板

免费企业网站程序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客户端调用异步优势明显在所有的调用中服务端的同步要优于异步。
http://www.zqtcl.cn/news/524938/

相关文章:

  • 做网站选哪个语言怎么登录百度app
  • 国发网站建设网站优化主要优化哪些地方
  • 快速微信网站开发定制网站建设费用预算
  • 网站制作叫什么知名网站建设制作
  • 网络营销网站建设公司h5应用
  • 网站开发合同要上印花税吗南江红鱼洞水库建设管理局网站
  • 疏通下水道网站怎么做wordpress 恢复初始化
  • 电脑商业网站怎的做软文推广渠道
  • 自己做网站需要买什么如何做微信商城网站
  • 有了网站开发app是不是更容易自建网站管理
  • 网站将要准备建设的内容有哪些做外贸有效的网站
  • 网站设计博客网站内容添加
  • 网站建站行业新闻微盟开店怎么收费
  • 网站的建设参考文献郑州网站建设中国建设建设银行
  • 重庆那些公司的网站是网易做的电信100m光纤做网站
  • 网站怎么设计产品营销策略包括哪些内容
  • 天元建设集团有限公司破产重组河源seo排名
  • 网站权重什么意思seo的搜索排名影响因素有
  • 建设报名系统是正规网站吗计算机培训班出来好找工作吗
  • 网站上的文章用秀米可以做吗宁波外客网络科技有限公司
  • 网站底部导航代码成品视频直播软件推荐哪个好一点ios
  • 上海电商网站开发公司垫江网站建设价格
  • 门户网站建设存在问题与不足商城网站开发项目文档
  • wordpress建站方便吗wordpress加入海报功能
  • 网站名称注册保护2018wordpress主题
  • 类似享设计的网站企业信息系统公示
  • 如何学习网站开发酒店网站源码
  • 怎么用nas做网站服务器WordPress云虚拟空间
  • 网站设计 ipad企业品牌推广宣传方案
  • 织梦网站怎么更换模板济南建设厅网站