Uploaded by Reham Reda

ASP

advertisement
ASP.NET Core
ASP.NET Core
Middleware
Request pipeline
Dependency Injection
(DI)
• Dependency Injection (DI) is a powerful design
pattern that promotes modular and maintainable
code.
• It involves injecting dependencies into classes
rather than hard-coding them, making the code
more flexible and testable.
Dependency Injection (DI)
• It's like ordering a pizza. If you make the
pizza at home, you're doing everything
yourself (creating the pizza and eating it).
• But if you order pizza, someone else (the
delivery person) brings it to you, and you
can focus on enjoying it without worrying
about the details of how it was made.
Real-world project
example: Building
a Blog Platform
1. Service Definition:
• Imagine creating a
service BlogService
that handles blogrelated operations.
Real-world project
example: Building a
Blog Platform
2. Service
Implementation:
• Implement
interface with
actual logic.
Real-world project
example: Building a
Blog Platform
3. Configure
Dependency Injection
in Program.cs:
• Register the
BlogService in
Program.cs to
enable DI.
Real-world project
example: Building a
Blog Platform
4. Controller Usage:
• In a controller, inject
IBlogService for
blog-related
functionality.
Tag Helper
• Tag Helpers are defined in .cshtml
files.
• They make it easier to create and
maintain HTML markup by enabling
server-side code to participate in
the rendering of HTML elements.
Tag Helper
• Here is an Example:
Attributes
are processed by the ASP.NET Core
framework to generate the appropriate
URL for the specified controller and
action.
Action Method
• ActionResult is a base class that represents the result of an action method in a
controller. It encapsulates the information that is sent to the client as a response.
Hang fire
• Hang fire allows you to kick off method calls outside of the request processing pipeline
in a very easy, but reliable way.
• These method invocations are performed in a background thread and called background
jobs.
• Library consists of three main components: client, storage and server:
Hang fire
Client
You can create any kind of background jobs using Hangfire:
• Fire-and-forget (executed only once immediately after execution to offload the method
invocation which means running background jobs outside application flow)
• Delayed (executed only once but not immediately to perform the call after some time)
• Recurring (fire many times on the specified CRON schedule to perform methods hourly,
daily and so on).
• Continuations are executed when its parent job has been finished.
<< The control is returned to a caller just after Hangfire serializes the given information and
saves it to the storage.
Hang fire
Storage
• Hangfire keeps background jobs and other information that relates to
the processing inside a persistent storage.
• Persistence helps background jobs to survive on application restarts,
server reboots, etc. This is the main distinction between performing
background jobs using CLR’s Thread Pool and Hangfire.
• Different storage backends are supported: SQL Azure, SQL Server 2008
R2 (and later of any edition, including Express) and Redis
Note that:
Redis is caching system that is used to store frequently accessed data temporarily.
This can improve application performance by reducing the need to fetch data from the
primary storage source, especially for read-heavy workloads.
Hang fire
Server
• Background jobs are processed by Hangfire Server.
• It is implemented as a set of dedicated (not thread pool’s) background
threads that fetch jobs from a storage and process them.
• Server is also responsible to keep the storage clean and remove old data
automatically.
Download