There are 4 rate limiting algorithms:
Fixed window : TheAddFixedWindowLimiter method uses a fixed time window to limit requests. When the time window expires, a new time window starts and the request limit is reset.I have written some articles on asp.net core mvc. which is useful in our day to day programming with asp,net core 2.0 and above
There are 4 rate limiting algorithms:
Fixed window : TheAddFixedWindowLimiter method uses a fixed time window to limit requests. When the time window expires, a new time window starts and the request limit is reset.To use Redis for distributed caching in ASP.Net core you need to follow below steps:
3.configure redis connection in AppSettings.json
"ConnectionStrings": {
"redis": "localhost:6379"
}
4.You need to inject IDistributedCache dependency in Constructor or endpoint.
IDistributedCache provides GetString("StringKey") and SetString("StringKey","StringValue") API, to set your object in Cache you need to serialize it.
app.MapGet("/weatherforecast", (HttpContext httpContext, IDistributedCache cache) =>
{
var strCachedForcast = cache.GetString("Forecast");
if (string.IsNullOrEmpty(strCachedForcast))
{
var cachedForecaste = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
}) .ToArray();
strCachedForcast = JsonSerializer.Serialize< WeatherForecast[]>(cachedForecaste );
cache.SetString("Forecast", strCachedForcast);
}
return JsonSerializer.Deserialize<WeatherForecast[]>(strCachedForcast);
})
.WithName("GetWeatherForecast")
.WithOpenApi();
If you will call transient service within singleton service, transient service will be treated as singleton service, i.e. the instance of transient service will be available till the lifetime of singleton service/application.
public interface ISingleton
{
void SingltonMethod();
}
public class Singleton : ISingleton
{
public Singleton(ITransient trnasient)
{
Console.WriteLine("Singleton ctor called");
Transient = trnasient;
}
public ITransient Transient { get; }
public void SingltonMethod()
{
Console.WriteLine("Singleton Method");
Console.WriteLine(" Transient instance Id :" +Transient.GetHashCode());
}
}
public interface ITransient
{
void TransientMethod();
}
public class Transient : ITransient
{
public Transient()
{
Console.WriteLine("Transient ctor called");
}
public void TransientMethod()
{
Console.WriteLine("Transient Method");
}
}
public interface ITransientA
{
void TransientMethod();
}
public class TransientA : ITransientA
{
private readonly ITransient trns;
public TransientA()//(ITransient trns)
{
Console.WriteLine("TransientA ctor called");
//this.trns = trns;
}
public void TransientMethod()
{
Console.WriteLine("Transient A Method");
//trns.TransientMethod();
}
}
Service Registration:
builder.Services.AddSingleton<ISingleton, Singleton>();
builder.Services.AddTransient<ITransient, Transient>();
builder.Services.AddTransient<ITransientA, TransientA>();
Service injection:
app.MapGet("/", (ISingleton service,ITransient transient , ITransientA transientA) => {
service.SingltonMethod();
transientA.TransientMethod();
Console.WriteLine("transientA hashcode :"+ transientA.GetHashCode());
/transient.TransientMethod();
Console.WriteLine("transient hashcode :" + transient.GetHashCode());
});
Output:
Custom middleware before
Transient ctor called
Singleton ctor called
Transient ctor called
TransientA ctor called
Singleton Method
Transient instance Id :39449526
Transient A Method
transientA hashcode :50346327
Transient Method
transient hashcode :50874780
Custom middleware After!
Custom middleware before
Transient ctor called
TransientA ctor called
Singleton Method
Transient instance Id :39449526
Transient A Method
transientA hashcode :11404313
Transient Method
transient hashcode :64923656
Custom middleware After!
clearly, we can see that instance of transient dependency within singleton is same all the time, while in other injections we are getting new instance.
Although technically, we can create and update resource with both Put and Post methods but as per standard we should use Post method to create a new resource while we should use Put method to update existing resource.
There are few differences between these two methods:
Caching is used to stored frequently used data or calculated values to reduce recalculation, fetching data from DB server, which improves performance of application.
ASP.Net Core provides an interface IDistributedCache which exposes APIs to implement caching.
Useful IDistributedCache Methods :
GetString(key)
GetStringAsync(key)
SetString(key, value, options)
SetStringAsync(key,value, options)
Refresh(key)
RefreshAsync(key)
Remove(key)
RemoveAsync(key)
Caching service stores data in Key-Value pairs we can set value for a given key and retrieve value by key from Cache.
We can pass options in SetStrings methods which is object of DistributedCacheEntryOptions class.
DistributedCacheEntryOptions class has following useful methods:
AbsoluteExpiration : used to specify an absolute expiry date.
AbsoluteExpirationRelativeToNow : used to specify a relative expiry date.
SlidingExpiration : used to specify a period of inactivity, after which the item will be ejected from the cache if it hasn’t been read.
How to Add data to Cache: below code sets (string) key and (string) value in Cache for 2 min.
await cache.SetStringAsync("cacheKey", "String_val_to_Cache"
new DistributedCacheEntryOptions {
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2)
});
How to Configure Distributed Caching Service: To use distributed cache service we need to configure in Program.cs
builder.Services.AddDistributedMemoryCache(opts => {
opts.SizeLimit = 200;
});
DistributedMemoryCache is an implementation of IDistributedCache which is added in service collection by AddDistributedMemoryCache(), I will add In-Memory caching functionality to the application. There are two other implementations available using appropriate NuGet packages.Option pattern allows you to read values from configuration file ( i.e. AppSettings.json) in a strongly typed model.
It's a 4-step process:
1. add values in AppSettings.json with appropriate section and setting keys.
2.you need to create a class with similar property names, mentioned in AppSettings.json file.
4.Finally, we need to add dependency of IOption<MySettings> in Controller constructor to use settings in Controller.
Note: Using IOption will not allow us to change settings in AppSetting.js while application in running mode. we need to restart app to get updated settings.So to overcome this limitation of IOption<T> we can use IOptionSnapShot<T> which will update option model as we change settings in AppSettings.json without restarting application.
Property name in class and keys defined in appsetting must match otherwise you will get null value for the unmatched property. or entire option object null if section and keys are not matched.
Include and exclude columns from entity: By convention all the public properties with Getter and Setter will be included in the Model. To Exclude any property we can apply [NotMapped] attribute.
public class Blog {
public int BlogId { get; set; }
public string Url { get; set; }
[NotMapped]
public DateTime LoadedFromDatabase { get; set; }
}
or
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Blog>().Ignore(b => b.LoadedFromDatabase);
}
Column Names Mapping: By Convention Column name will be mapped to the property having same name. we can change this behavior by applying attribute or changing on OnModelCreating().
public class Blog {
[Column("blog_id")]
public int BlogId { get; set; }
public string Url { get; set; }
}
OR
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Blog>()
.Property(b => b.BlogId)
.HasColumnName("blog_id");
}
We can also specify datatype of a column in Attribute or using fluent API
[Column(TypeName = "varchar(200)")]
or using fluent API
eb.Property(b => b.Url).HasColumnType("varchar(200)"); (rest of the code would be similar to above onModelCreating.
Primary Key Configuration
By convention property named 'Id' or <TypeName>Id would be configured as primary key. explicitly we can declare any column as primary key by applying [Key] attribute or in OnModelCreating()
[Key]
public string LicensePlate { get; set; }
OR
modelBuilder.Entity<Car>().HasKey(c => c.LicensePlate);
We can also configure composite primary Key like:
modelBuilder.Entity<Car>().HasKey(c => {c.LicensePlate, c.Model} );
Foreign Key Shadow Property
Shadow properties are most often used for foreign key properties, where they are added to the model by convention when no foreign key property has been found by convention or configured explicitly.
Middleware is piece of code that's assembled into an app pipeline to handle requests and responses. Each middleware component in the re...