Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by Builder.services. AddMemory Cache
在ASP.NET Core 中实现幂等API和WinForms客户端防重提交实践
大家好,欢迎关注dotnet研习社。今天,我想和大家聊聊在 ASP.NET Core 中如何实现幂等 API,这是我们在实际项目开发中非常重要、但又常常被忽略的一个话题。 幂等性(Idempotency)指的是:一次请求与多次请求对资源产生的影响是相同的。换句话说,无论客户端发送一次请求还是多次相同的请求,服务端的状态都不会发生改变。 在 HTTP 协议中,像 GET、HEAD、PUT 和 DELETE 方法天生是幂等的,而 POST 方法默认并不具备幂等性。但在实际业务中,我们常常需要让某些 POST 接口具备幂等性,特别是在支付、订单、注册等业务场景中,防止重复提交带来的数据污染。 防止重复支付 避免资源重复创建(如重复下单) 增强系统稳定性,降低误操作风险 提升用户体验 尤其是在网络不稳定或者用户误操作导致重复提交时,一个幂等的接口能很好地兜底,避免我们后台系统的混乱。 我通常采用「幂等键」+「请求缓存」的方式来实现。这种做法清晰、通用、且易于维护。 一、定义幂等键(Idempotency Key) 我们可以约定客户端在请求头中携带一个唯一的幂等键,比如:...
.NET 缓存:内存缓存 IMemoryCache、分布式缓存 IDistributedCache(Redis)
.NET缓存里分了几类,主要学习内存缓存、分布式缓存 1、Program注入缓存 builder.Services.AddMemoryCache(); 2、相关方法及参数 Get、TryGetValue、GetOrCreate、GetOrCreateAsync、Set、Remove,关键参数是过期时间,GetOrCreate、GetOrCreateAsync是通过委托类型的参数设置的,Set方法可以通过参数直接设置,或者使用MemoryCacheEntryOptions,类型有三种: (1)AbsoluteExpiration 绝对过期,到期删除 (2)AbsoluteExpirationRelativeToNow 相对当前时间过期,到期删除 (3)SlidingExpiration 滑动过期,时效内访问再延长,未访问到期删除 [Route("api/[controller]/[action]")] [ApiController] public class MemoryCacheController : ControllerBase { private readonly...
.Net依赖注入神器Scrutor(下) - 董瑞鹏 - 博客园
前言 上一篇文章我们讲到了Scrutor第一个核心功能Scanning,本文讲解的是Scrutor第二个核心的功能Decoration 装饰器模式在依赖注入中的使用。 装饰器模式允许您向现有服务类中添加新功能,而无需改变其结构 Install-Package Scrutor 本文的完整源代码在文末 Decoration 依赖注入代理模式 首先首先一个 获取 User 的服务 定义 User 类 public class User { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string?
如何让你的.NET WebAPI程序支持HTTP3?
下面我将总结构建Http3的经验,以Token Gateway的项目为例,请注意使用Http3之前你需要知道它的限制, Windows Windows 11 版本 22000 或更高版本/Windows Server 2022。 TLS 1.3 或更高版本的连接。 Linux 已安装 libmsquic 包。 首先我们需要拉取我们的代码 git clone https://gitee.com/hejiale010426/Gateway.git cd Gateway 然后我们打开Program.cs #region FreeSql类型转换 Utils.TypeHandlers.TryAdd(typeof(Dictionary<string, string>), new StringJsonHandler<Dictionary<string, string>>()); Utils.TypeHandlers.TryAdd(typeof(RouteMatchEntity), new StringJsonHandler<RouteMatchEntity>());...
如何让你的.NET WebAPI程序支持HTTP3?
下面我将总结构建Http3的经验,以Token Gateway的项目为例,请注意使用Http3之前你需要知道它的限制, Windows 11 版本 22000 或更高版本/Windows Server 2022。 TLS 1.3 或更高版本的连接。 已安装 libmsquic 包。 实现讲解 首先我们需要拉取我们的代码 git clone https://gitee.com/hejiale010426/Gateway.git cd Gateway 然后我们打开Program.cs #region FreeSql类型转换 Utils.TypeHandlers.TryAdd(typeof(Dictionary<string, string>), new StringJsonHandler<Dictionary<string, string>>()); Utils.TypeHandlers.TryAdd(typeof(RouteMatchEntity), new StringJsonHandler<RouteMatchEntity>());...
基于.NetCore开发博客项目 StarBlog - (30) 实现评论系统
前言 时隔五个月,终于又来更新 StarBlog 系列了~ 这次是呼声很大的评论系统。 由于涉及的代码量比较大,所以本文不会贴出所有代码,只介绍关键逻辑,具体代码请同学们自行查看 GitHub 仓库。 博客前台以及后端涉及的代码主要在以下文件: StarBlog.Web/Services/CommentService.cs StarBlog.Web/Apis/Comments/CommentController.cs StarBlog.Web/Views/Blog/Widgets/Comment.cshtml StarBlog.Web/wwwroot/js/comment.js 管理后台的代码在以下文件: src/views/Comment/Comments.vue 实现效果 在开始之前,先来看看实现的效果吧。 讨论区的这部分UI使用 Vue 来驱动,为了开发效率还引入了 ElementUI 的组件,看起来风格跟博客原本的 Bootstrap 不太一样,不过还挺和谐的。 无须登录即可发表或回复评论,但需要输入邮箱地址并接收邮件验证码。...
.net 温故知新【13】:Asp.Net Core WebAPI 缓存
缓存指在中间层中存储数据的行为,该行为可使后续数据检索更快。 从概念上讲,缓存是一种性能优化策略和设计考虑因素。 缓存可以显著提高应用性能,方法是提高不常更改(或检索成本高)的数据的就绪性。 在最新的缓存控制规范文件RFC9111中,详细描述了浏览器缓存和服务器缓存控制的规范,其中有一个最重要的响应报文头Cache-Control。 该报文头的设置会影响我们的缓存,包括浏览器端和服务端。 RFC911:https://www.rfc-editor.org/rfc/rfc9111#name-cache-control 在Cache-Control中,如果设置max-age=10,则表示告诉浏览器缓存10s,而为什么浏览器要认这个表示呢,就是上面我们说的前后端都要根据RFC标准规范去实现,就是硬件的统一插口,不然其他生成出来的就用不了。 那么在Asp.net Core 中只需要在接口上打上ResponseCacheAttribute并设置max-age的时间即可。 首先建一个Asp.Net Core WebAPI 项目,写一个获取学生的Get接口。 namespace...
Asp.Net Core webapi+net6 使用资源筛选器(过滤器) 做缓存
写一个特性类,用来做标记 [AttributeUsage(AttributeTargets.Method)] //只对方法有效 public class ResourceFilterAttribute : Attribute { } 我这里使用了MemoryCache来做缓存,也可以使用字典来做,但一定要加上static,否则字典每一次请求都会new一个实例,缓存的东西就丢了 private static Dictionary<string,object> caCheDic=new Dictionary<string, object>(); 过滤器代码实现 public class ResourceFilter : IAsyncResourceFilter { private readonly IMemoryCache cache; public ResourceFilter(IMemoryCache cache) { this.cache = cache; } public async Task...
Job-based Asynchronous Processing using Hangfire and AsyncFlow
Meet Ahmed, a diligent developer at a budding startup that specializes in data analytics. Their platform allows companies to upload vast amounts of raw data, transforming it into insightful visual reports. One sunny afternoon, Ahmed's team releases a new feature enabling batch processing of huge datasets. As Ahmed sips his coffee, watching real-time user logs, he notices something alarming. A client, Acme Corp., uploads a massive dataset - one much larger than Ahmed ever anticipated.
10 Things to Do to Migrate MVC/Web API to ASP.NET Core
Web, ASP.NET MVC, ASP.NET Core 0 Comments It is possible to migrate your .NET Framework ASP.NET MVC application to any version of .NET/.NET Core—including the latest version. Some parts of the process are more ... challenging ... than other parts, though, so you need to be prepared.
How to achieve progressive disclosure UI for Non-JS users
What Is Progressive Disclosure UI? Progressive Disclosure UI is an user interface where the content and actions change based on user responses. For example, see the below scenario. Based on the user response to the question — ‘ Do you have the doctor appointment?’, the UI changes dynamically. If the user response is Yes, then the next question will be ‘Is this your first visit?’. Otherwise it will be ‘Do you want to schedule an appointment?’. Please see the flow chart below.
Work with data in ASP.NET Core Apps
Read in English Read in English Feedback Edit Twitter LinkedIn Facebook Email Article 11/04/2022 26 minutes to read In this article Tip This content is an excerpt from the eBook, Architect Modern Web Applications with ASP.NET Core and Azure, available on .NET Docs or as a free downloadable PDF that can be read offline. Data access is an important part of almost any software application.
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with Builder.services. AddMemory
Contact Builder.services. AddMemory, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack