.NET Core中的鉴权授权正确方式(.NET5)
前后端分离的站点一般都会用jwt或IdentityServer4之类的生成token的方式进行登录鉴权。这里要说的是小项目没有做前后端分离的时站点登录授权的正确方式。二、传统的授权方式这里说一下传统授权方式,传统授权方式用session或cookies来完成。1.在请求某个Action之前去做校验,验证当前操作者是否登录过,登录过就有权限2.如果没有权限就跳转到登录页中去3.传统登录授权用的AOP-Filter:ActionFilter。
[CallerArgumentExpression] and throw helpers: Exploring .NET Core 6 - Part 11
In this post I describe a new feature in C# 10, the [CallerArgumentExpression] attribute, how it can be used to create "throw helpers", and the addition of a throw helper to ArgumentNullException.
Avoid Using Exceptions to Determine API Status Codes and Responses
It's typical for API endpoints to call application or domain services. In the case of success, the API can simply return Ok and the result of the service call. But for non-success cases, should you use exceptions to communicate from the service to the endpoint, so that it can return an appropriate status code and response? Assuming you read the title of this article, you probably already know the answer I'm going to give. But first let's look at a (bad) example. Consider the following method found in a service that will be called by an API endpoint (controller action)
Source generator updates: incremental generators: Exploring .NET Core 6 - Part 9
In the previous post, I described the LoggerMessage source generator that can give performance benefits without the boilerplate. In this post I look at the updates to the source generator API in .NET 6, why the changes were made, and how to update your source generators to use them.
How to use JSONPatch in .net core
REST API’s often require a PATCH method to partially update resources. A typical scenario for this would be when updating a survey questionnaire type workflow or even, as in the example I will demonstrate, updating a customers shopping cart in an e-commerce Microservice. As a customer updates their cart by adding, removing or editing products in the cart you’ll need to cater for a number of operations.
Improving logging performance with source generators: Exploring .NET Core 6 - Part 8
In this post I look at the new LoggerMessage source generator, showing how and when to use it. I start by showing the existing ways to use ILogger, pitfalls to watch out for, and approaches to improve performance. I then show how the source generator simplifies this. We look at the code it generates, and do some simple benchmarks to see the performance benefits this brings.
Build client web assets for your Razor Class Library
In this post we’ll show you how to integrate npm and webpack into the build process for your Razor Class Library. For folks who want a solution that just works, we’ll look at the new experimental Microsoft.AspNetCore.ClientAssets package that handles this integration for you automatically provided you have the right tools installed. We’ll also walk through all the details of how to set this up using MSBuild for folks that want to be able to control and customize the process. While the focus of this post is npm and webpack, the solution that we provide here can easily be adapted to other tools like yarn, rollup, etc.
.Net Core中使用ElasticSearch(一)
在官网下载Es,注意版本号,不同大版本号之间差异很大。我安装的是7.14.0版本1.1 安装成服务cmd 进入bin目录下执行elasticsearch-service.bat install1.2 安装插件ik分词器,分词器的版本和ES版本需要一致elasticsearch-plugin.bat install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.14.0/elasticsearch-analysis-ik-7.14.0.zip1.3 配置账号密码修改config目录下面的...
Analyzers for ASP.NET Core in .NET 6: Exploring .NET Core 6 - Part 7
Analyzers have been built into the C# compiler for some time now. Anyone can write their own analyzer, distribute it as a NuGet package, and hook into the compiler to find potential code issues. In this post I look at the changes to the existing ASP.NET Core analyzers in .NET 6, as well as the new ones added for the new minimal APIs. This post ended up a little long, so I've included shortcuts to each of the analyzers I describe in case you want to skip ahead...
Google Sheets API with .NET Core
In this article, we’re going to explore the Google Sheets API using the Google Sheets Client Library for .NET. We’ll start with configuring the Google Sheets API in the Google developer console. Then we’ll build an ASP.NET Core application that can read and write data into a google sheet. To downloa...
Dapr + .NET Core实战(十一)单机Dapr集群负载均衡
第十篇讲过了K8S集群下如何使用Dapr运行程序,但是很多人一直在问如何单机下进行Dapr的负载,这节课我们来聊聊如何单机进行Dapr的负载。首先要说的是单机下,通过 dapr run --app-id xxxxx这个命令是没办法直接运行dapr集群的,因为同一个app-id只能通过dapr run运行一次。这时候我们就需要利用docker来实现dapr的单机集群部署。下面我们就来实现dapr的单机集群部署。
Building a middleware pipeline with WebApplication: Exploring .NET Core 6 - Part 4
In my previous post, I looked at the code behind WebApplicationBuilder, including some of its helper classes like ConfigureHostBuilder and BootstrapHostBuilder. At the end of the post, we had created an instance of the WebApplicationBuilder and called Build() to create a WebApplication. In this post, we look at a bit of the code behind WebApplication, and focus on how the middleware and endpoints are configured.
Exploring the code behind WebApplicationBuilder: Exploring .NET Core 6 - Part 3
This is the third post in the series: Exploring .NET Core 6. Part 1 - Looking inside ConfigurationManager in .NET 6; Part 2 - Comparing WebApplicationBuilder to the Generic Host; Part 3 - Exploring the code behind WebApplicationBuilder (this post). In my previous post, I compared the new WebApplication to the Generic Host. In this post I look at the code behind WebApplicationBuilder, to see how it achieves the cleaner, minimal, hosting API, while still providing the same functionality as the generic host.
Dapr + .NET Core实战(四)发布和订阅
发布订阅是一种众所周知并被广泛使用的消息传送模式,常用在微服务架构的服务间通信,高并发削峰等情况。但是不同的消息中间件之间存在细微的差异,项目使用不同的产品需要实现不同的实现类,虽然是明智的决策,但必须编写和维护抽象及其基础实现。 此方法需要复杂、重复且容易出错的自定义代码。Dapr为了解决这种问题,提供开箱即用的消息传送抽象和实现,封装在 Dapr 构建基块中。业务系统只需调用跟据Dapr的要求实现订阅发布即可。
.Net Core with 微服务 - 分布式事务 - 可靠消息最终一致性
前面我们讲了分布式事务的2PC、3PC , TCC 的原理。这些事务其实都在尽力的模拟数据库的事务,我们可以简单的认为他们是一个同步行的事务。特别是 2PC,3PC 他们完全利用数据库的事务能力,在一阶段开始事务后不进提交会严重影响应用程序的并发性能。TCC 一阶段虽然不会阻塞数据库,但是它同样是在尽力追求同时成功同时失败的一致性要求。但是在很多时候,我们的应用程序的核心业务为了追求更高的性能、更高的可用性,可以允许在一段时间内的数据不一致性,只需要在最终时刻数据是一致就可以了。基于以上场景我们可以采用基于可靠消息服务的最终一致性分布式事务处理方案。
.net Core 基于EF Core 实现数据库上下文
在做项目时,需要将某一些功能的实体建立在另一个数据库中,连接不同的数据库用以存储记录。通过查找资料,实现EF Core上下文。
RabbitMQ从零到集群高可用(.NetCore5.0) - RabbitMQ简介和六种工作模式详解
RabbitMQ是一个开源的消息代理和队列服务器,用来通过普通协议在完全不同的应用之间共享数据,RabbitMQ是使用Erlang(高并发语言)语言来编写的,并且RabbitMQ是基于AMQP协议的。1.1 AMQP协议Advanced Message Queuing Protocol(高级消息队列协议)1.2 AMQP专业术语:(多路复用->在同一个线程中开启多个通道进行操作)Server:又称broker,接受客户端的链接,实现AMQP实体服务Connection:连接,应用程序与broker的网络连接Channel:网络信道,几乎所有的操作都在channel中进行,Channel...
Implement a Cross-Platform Apache Kafka Producer and Consumer with C# and .NET
Sometimes you’d like to write your own code for producing data to an Apache Kafka® topic and connecting to a Kafka cluster programmatically. Confluent provides client libraries for several different programming languages that make it easy to code your own Kafka clients in your favorite dev environment. One of the most popular dev environments is .NET and Visual Studio (VS) Code. This blog post shows you step by step how to use .NET and C# to create a client application that streams Wikipedia edit events to a Kafka topic in Confluent Cloud. Also, the app consumes a materialized view from ksqlDB that aggregates edits per page. The application runs on Linux, macOS, and Windows, with no code changes.
How to Create a new Solution and Projects using dotnet CLI
I create a lot of samples, demos, open source projects, etc. and I like to use the fairly standard repository layout of having a solution file in the root and project files in a src subfolder. Achieving this in Visual Studio is tedious, but fortunately this series of dotnet CLI commands quickly create the structure for you.
ELK+kafka docker快速搭建+.NetCore中使用
ELK开源实时日志分析平台。ELK是Elasticsearch,Logstash,Kibana 的缩写。Elasticsearch:是个开源分布式搜索引擎,简称ESLogstash:是一个完全开源的工具,可以对日志进行收集,过滤,存储到ESKibana: 也是一个开源和免费的工具,这里主要用作ES的可视化界面工具,用于查看日志。环境:centos7.9一、搭建ES先要调高jvm线程数限制,修改sysctl.confvim /etc/sysctl.conf修改max_map_count调大,如果没有这个设置,则新增一行vm.max_map_count=262144改完保存后, 执行下面命令让...