Transcripts
1. Introduction: Hi, Welcome to this Microservices course
with ASP.net Core. My name is S1 and I'll be leading you
through this course. I'm a software engineer and Microsoft most
valuable professional. I designed this course
so that you can learn microservices from
absolute beginning. In this course, we will not
use any copy paste technique. In fact, we will cover
everything from scratch. But in order to
take this course, you must have a knowledge of
ASP.net Core RESTful APIs. Well, three to six months of rest API knowledge is enough
to start this course. By the end of this course, you'll be able to dilute the micro-services of
local desktop application. Alright, so what are
you waiting for? Take this course and
let's start learning.
2. Prerequisites: Before starting this course, just make sure to download the video studio 2022
community edition. If you're using some
other edition of video studio like
professional or enterprise, which supports dot and
core, then it's okay. We'll Studio Community
Edition is free of cost. If you can't afford the
enterprise or some other edition, then just go ahead with
that community edition. I have attached a download link of film studio so that you can download and install it
in your machine accordingly. I also want to tell you
that in this course, I'll use the shortened
version six. Make sure you're using the
latest version of dotnet. In order to complete
this course, you must have a three
to six months of experience with dotnet
Core RESTful APIs. If you are an absolute beginner, that I will recommend
you to please pause this course and just
go to my rest API, a beginner course,
where you will learn the rest API
development from scratch. And right after the
rest APA course, you can take this
microservice course. Alright, that's all I
want to talk about. I hope you are on the same page. Now let's start learning.
3. Course Structure: In this course, you learned micro-services from
absolute beginning. So let me show you
what is coming up over the next few sections. In the next section, we'll
start out with the basics like what is microservices and
how microservices works. Then we'll set up the
projects for microservices. Right after that. We'll create a microservice for vehicles. Then we'll create the micro
service for customers. And right after that customers
microservice will cover the Azure Service Bus for
communicating the microservice. In this section, we'll send the data to the
Azure Service Bus. Then we'll create the
reservations microservice. And right after that will fetch the data from Azure
Service Bus queue. Once we will cover these things, then we will also
cover the API Gateway with dotted curve
using microservices. So these are the things which
we'll cover in this course. All right? The time is very short and there are a lot
of things to cover. So I will see you in
the next section.
4. Monolithic Architecture: Before talking about the
microservices architecture, we first need to understand
the monolithic architecture. A monolithic application
is built as a single unit. Let's take a simple example. Suppose you have
been asked to create a vehicle test
drive application. In this scenario,
our application will consist of three parts. The database, which consists of one or more than one table
with some relations. The client side user interface, which consists of HTML pages, are some user interface which will be used
on the client side. The server side application, which will handle all
the HTTP requests, execute the domain
specific logic, retrieve, update and delete the
data from the database, and populate the data to the client side application so that the client
can view the data. So that's our monolith
architecture works. In monolith architecture, all the components
are interconnected. If we want to change anything
in some particular service, then we need to redeploy
the whole application because all the things are
interconnected and dependent. Changing, wanting
in the code will break the entire application. And to be honest, this
is really annoying if you're making some
enterprise level application. So to overcome this problem,
we need microservices. And in the next lesson, we'll discuss the
microservice architecture. I'll see you in the next one.
6. Microservices Communication: In the last lesson, we have talked about the
microservice architecture. Now let me tell you how we'll perform that
communication between the microservices and simple
monolithic application. We have tables and relations and to services
can talk to each other with the help of
relations because these services share
the same database. Now, in case of microservices, each service contains
its own data store. How is it possible to communicate with
other microservice? Well, there are
multiple ways where one microservice can talk
to other microservice. But first, let's
take an example. Suppose we have a simple
e-commerce application with some services
like customers, products, basket, inventory,
payments, orders, and email. The customer will
add the product in the shopping cart or basket, and then he will decide how
much products he will need. Let's say the
customer wants to add five products in the basket. But there are only two products available for this
basket service with dark to the inventory service
via HTTP calls, rest APIs. This communication is internal microservice
communication. Now once the customer
will make the payment, then he will place the order. The customer placed the order, but the order is still pending. And from the backend, the admin after
application will check whether the payment
is successful or not. If the payment is successful, then the admin will
send the e-mail to the customer that the order
has been placed successfully. And this process is asynchronous because
once the customer will place the order than the order data will go to some external service provider like Azure messaging
or Rabbit MQ. And these are message brokers. Now let me tell you
what is message broker. A message broker
is a software that enables the
applications, systems, and services to communicate with each other and
exchange information. Okay, what happens here is when the customer will
place the order, then the data will go
to the message broker. Or in short, we will send the
data to the message broker. On the other hand,
the evil service will interact with
the message broker, will get the order details
from this message broker. And if the payment
is successful, then this e-mail service will send an e-mail to the customer. The order has been
placed successfully. This type of communication, which happens with
message broker, is an external
microservice communication or asynchronous communication. The other type of
communication which we have discussed earlier is asynchronous communication or internal microservice
communication. Because without knowing
the inventory items left, we can't increase the
items in the basket. Okay, That's all
from this lesson. Now, I'll see you
in the next one.
7. When To Use Microservices: In this lesson, we'll
talk when we should use microservices and when we should not use the
microservices. But before this, let's compare the microservices
monolithic application. Now let's talk about
the deployment. Deploy the monolithic
application. You have to publish or redeploy
the whole application. But in case of microservices, you can deploy
this microservice. Then you can also deploy
the other micro-services. If we'll talk about
the scalability, then it's much easier to scale the microservice
application. Now let's talk about the
platform dependent feature. The simple monolithic
applications are platform dependent. It means if we have created
this first service in dotnet core and MS SQL database, then we also need to create the other services in
the same language. But in case of microservices
is not the case. You can get this first
service in dominant core. And with Microsoft SQL database. Then you can get
the other services in some other
programming language. And you can also use some
other database providers like MongoDB or MySQL, etc. But working on
microservices will also increase the
development speed. Because in enterprise
applications, each developer is associated with some particular service. And it means there are
less chances of errors. Highlight in
monolithic application $1 per asteroid, the whole code. The developer can face many errors and it will reduce
the coding speed as well. Okay, so enough talk about the monolithic and
microservice applications. Now let's talk about
the important part that when we should use microservices and then we
should not use microservices. If your application is simple, an intermediate, like a
normal cinema ticket booking. And if you're not
comfortable with different programming languages and development environment, then you don't need to
write the microservices. You can continue with a
simple model architecture. But if your application
is very complex, like e-commerce
activation, right? Booking, flight, reservation, and
applications like Netflix. You should probably think
about microservices. So, So the choice is
totally up to you. There are a hundreds
of advantages and disadvantages of
using microservices. But in this course, our
focus is entirely on microservices because
it's a market demand to learn new things. Okay, That's all
from this lesson. Now, I'll see you in
the next section.
8. What Microservices we will create: In this course, we'll
create the microservices for local disk
drives activation. Let's assume that there is some famous automobile
company like Tesla. And people like me want to test drive of some
particular vehicle. So we'll create a microservices for vehicle desktop application. And I know that application
is quite small, but this will give you a perfect Jumpstart in the
world of microservices. Okay, let me tell you the basic
flow of this application. We'll create three
microservices. The first will be used to
handle the lifecycles data. The second issue is to
handle the customers data, and the third is used to
handle the reservations data. Now let's say there is some
mobile or web application where we'll consume
these microservices. So first of all, we'll display
the vehicles list data. And this data will come from
the vehicle microservice. And after selecting
some vehicle, will get further
details of the vehicle. Now when we press the
book desktop option than we have been asked
to provide the details. And once we provide
these details, then this data goes to the
customers microservice. And similarly, this
data will also go to the Azure
messaging service. Now, there is also an admin
side of the aggregation. With the admin can add, update and delete the Lakers. And the admin can also
send the e-mails to those customers who
are interested in the desktop of the
company vehicles. So when the admin will open the application and click
the reservation option, then you will see the
list of customers who are interested to test some
particular vehicle drive. And this data will come from Azure messaging
service bus, okay, then the admin can
send that email to these customers
for confirmation. So we'll get these microservices
inside this course. Okay, let's start
from the next lesson.
9. Create Solution For Microservices: In this lesson, we're
going to create the solution and
microservices project. It's better if you will, Katie, different we'll Studio
projects for microservices. But this will also consume
more RAM and processing power. Instead of opening the
different instance of field Studio will just open one incidence
of fetal studio. And inside this will create one solution which contains all the micro service projects. Okay, Let's open
the real studio. At the time of recording
this course with studio 2022 is the latest version. Okay, then create a new project. Here we need to search for
blank or empty solution. You'll see that this blank
solution template will create an empty solution
containing no projects. Alright, let's select
this and click Next. Here we need to provide
a name to our solution. Let's give some user-friendly
name because we are making the microservices for
our vehicle test drives. So we'll name this solution
to local test drive space. This option. Here we go. We'll see an empty solution
inside the video studio. Now, inside the solution, we're going to add two different
core Web API projects. Okay, so let's right-click
on the Solution. Then we'll get a new project. And inside the search bar, we're going to search
for dotnet Core. Just scroll down
and we'll see an ASP.net Core Web API
project template. Just select this and click Next. We need to provide a
name to our project. Let's name it legal API. And click Next. Here you'll see the
framework I'm using, dot and six, which is the
latest favorite at this time. We don't need
authentication and darker. And that's why we don't need
to change anything here. Alright, then make sure this
enabled open API support is checked because this will add the swagger API
documentation for us. Also, in doctrine six, we have an option to
use the minimal APIs. In minimal APIs, we
don't use controllers. We'll just add the code
in the startup class. But this is somehow
for beginners. So instead of using
the minimal APIs, we'll choose the
controller approach. So make sure you have checked this is use Controllers option, that gate this project. Okay, here we go. Our vehicle API project
has been created. Now just like this
vehicle API project, we'll add two more projects. So let's right-click
on the Solution. Then Add New Project and choose the dotted curve
map API project template. Right after that, we will name this project to customers API. Alright, let's get this project. Here we go. Now we have two
projects, vehicles, API and the customers API. It's time to add
the third project, which is reservations API. So we'll right-click
on the Solution. Then Add New Project, choose ASP.net Core
Web API template. Then we'll leave this
project to reservations API. And let's create this. We have added T projects
inside the solution. Now in the next lesson, we'll install the NuGet
packages for microservices. I'll see you in the next lesson.
10. Install Nuget Packages: In the last lesson, we
have kicked the solution and T projects for
microservices. Now in this lesson,
we're going to add the NuGet packages inside
these micro service projects. Currently will need
the NuGet packages. One is for Entity
Framework Core, second is for SQL Server
database connection, and the third is for
Migration purpose. Okay, we need to install these two NuGet packages inside these three
micro service projects. Instead of adding the packages one-by-one inside
these micro-services, let's right-click on
the Solution and then select Manage NuGet
packages for solution. Now, inside the search bar, Let's search for Entity
Framework Core NuGet package. And here I'm going to install this NuGet package inside
all these projects. Okay, so let's install this NuGet package for
Entity Framework Core. Here we go. The package has been
installed successfully. Now let's search
for other package, which is for SQL
Server database. And again, we're
going to install the package inside
these projects. Once will install this package. Then we'll search for this
package for Migration purpose, which is Microsoft
dot core dot tools. And again, we're
going to install this package inside
all the projects. We have installed all
the necessary packages. Now as we'll move forward, then we will also install
other NuGet packages. But till then we
have enough material to start our
microservice project. So let's start writing our first micro-service in
the next section.
11. Vehicle Service Overview: In this section,
we're going to get the micro service for vehicles. Let me tell you the basic
flow of this vehicle. Microservice, this way
called micro service, will be based on five
different methods. Get vehicles which will return
all the lists of vehicles. Get vehicle by ID, which will return the
specific vehicle detail according to the vehicle ID. Add method will be used
to add them later record. Update method will be used
to update the vehicle record and delete vehicle will be used to delete the
vehicle, record. These two methods
get vehicle and get wiggled by IDR GET methods. This ad vehicle
is a post method, it is a PUT method, and this delete vehicle is
a delete action method. In this course, we will not
get the mobile application or web application
because our focus is entirely on the
micro-services. Okay, So let's start creating the vehicle microservice
in the next lesson.
12. Create Models For Vehicle Service: In this lesson, we'll start getting the vehicle
micro-service. First, we'll start
with a model class. Before adding the mortar class, let me delete the
unnecessary code like this weather forecast and this weather forecast
controller class. Also delete the
weather forecast and Bedford costs controller
from the other two projects. Okay. Now let's
get folder inside the vehicle microservice
project and we name it models. Then inside this model's folder, Let's add a new C-Sharp class. And I'm going to
name this class to Waco. Alright, here we go. Inside this class, Let's add some properties like
idea of the vehicle, name of the vehicle, price of the vehicle, in which URL of the vehicle. Then we'll add the property for displacement of the vehicle. Right after that, we'll
add the maximum speed, and we'll also add
the vehicle length, width, and height properties. You can also add more properties in this vehicle
class if you want. But I have added just
a few properties to keep things simple. Alright? Once you add these properties inside
this vehicle class, then we'll create
the DB context. So let's get the DB context
in the next lesson.
13. DbContext For Vehicles Service: Earlier in this course, we have created the
modal class for vehicle. And now in this lesson, we're going to create
the DB context class. The DB context class
is an important class. It acts as a bridge between your model class
and your database. And it's also responsible for converting LINQ queries
into SQL queries, send them to database. What I mean is
when we'll perform the crud operations
with the database, then we'll use the link queries. And this DVI contexts will
convert those LINQ queries into SQL queries and then
send them to the database. So before adding the
DB context class, Let's create a new
folder and name it data. Then in this data folder, I'm going to add a new class. Then we named this class
to API DB context. Let's add this class. Then we just need to derive
this class from DB context. And this DB context comes from the Entity Framework
Core namespace. And we have to resolve the
namespace for this class. Then inside the EPA
DB context class, I'm going to add one property. Let's add a property. Now the question is, what this property is
going to written? Well, it's going to return
the DB set of vehicle. Okay, The saltiness phase
for this little glass. Then we named this
property to Lakers. At the runtime Entity
Framework will map this TV set property with
a database table name. For example, if we name this
property here as vehicles, than Entity Framework
will get a table inside a database with the same
name, that is vehicles. Alright, now let's provide the connection string to
this DB context class. For this purpose, we'll use to our add method that
is on configuring. Once we will write this
on configuring method, then remove this line. And here we'll add
these options builder. Then call the use
sequel server method, which will take the
connection string. So we'll go to the SQL
Server Object Explorer. Here, we'll see the server name. Let's copy the server name. Then in the use SQL Server
method will add server equals. Then add the name of the
SQL Server which we copied. Right after this step, we will use database equals. And here we need to provide some user-friendly
name of our database. Because we are making a
microservice of vehicle. So we'll leave our
database to legal API DB. Alright, now in the next lesson, we'll add the migrations to the database inside
a SQL Server. So I'll see you in
the next lesson.
14. Migration In Vehicle Service: In the last lesson, we have
set up the connection string. And now in order
to get a database, we need to add the migrations. To enable the migrations. First, we'll need a
NuGet package that is Microsoft dot core dot tools. And we have already installed
this NuGet package. So we'll go to the
Package Manager console. And there we're going
to add them migrations. But before adding
the migrations, just select this project in which we are adding
the migrations. For example, if you are adding the migration in
vehicle microservice, then select the vehicle
microservice project. In the Package Manager console, set the default project
to Microservice. Similarly, when we'll add the migrations in
some other project, then we need to change
the default project. At this time, we want to add the migrations in the
Michel's microservice. So make sure vehicle
project is selected. Then in the Package
Manager console, we'll add a command that
is add dash migration, and then provide a user-friendly names
with the migration. Hi limit, initial Create,
and let's hit Enter. This will generate
the migration script. This script will be used to
add the database and tables, but we have not actually
created the database yet. So again, in the Package
Manager console, let's use the command that is update dash database,
and let's hit Enter. Now this will add the
database and tables inside the SQL Server
Object Explorer. Let me go to the SQL
Server Object Explorer. Let's refresh the server. And it will expand this server. Then we will see the
database folder. It will expand this further. Then we'll see our database. Inside this will
also see the table. Let's open the table. And here you'll see that inside
this table we have all the properties which we have defined in the
vehicle model class. Okay, so far we have added
the database and the table. Now let's continue over
in the next lesson.
16. Create Vehicle Service: Earlier we have worked with the interfaces and
now in this lesson, begin to work with the services. Basically in the
vehicle interface, we have defined these methods. And now we'll get a
vehicle service in which we'll add the eye Legal
Methods implementation. Let's get a new folder
and rename IT services. Now inside this folder, Let's add a new C-Sharp class and rename it local service. Now inside this vehicle
service, right, the implementation
of methods that we have written in the
vehicle interface. So let's implement this
vehicle service class from a vehicle interface. Then resolve the namespace
for this interface. Alright, we need to
implement the methods for this by vehicle interface. And we'll use the
veal Studio feature of implementing the methods. If you can't see this, then press Control period, and then implement this I
vehicle interface methods. Alright, so we'll studio
built-in feature will add all the methods in the
vehicle service class. Now, we just need to add the
logic inside these methods. First, we'll start with
our vehicles method. This method will return
the list of vehicles and we're going to add the
logic inside this method. But first, we'll
need a data source. We want to access the
vehicles DB set property which we have written in
the APA DB context class. So to access the
DB set property, we'll add a field of type ABA DB context
is all the namespace. Then we'll leave this
field to DB context. Okay, right after that, we'll add a constructor. And here we'll create
a new instance of API DB context and assign
this to DB context. Now with this DVI contexts, we can pick the vehicles
property which we have defined in the API
DB context class. So we'll simply go to this
Get all vehicles method. Let's start our work. This method will return the list of vehicles from the database. So we'll simply use
DB context dot. Then we'll pick the vehicles
D, we said property. And right after that
will make this method is synchronous and use the
await operator here. Now, we'll store the result in a variable intimate Lakers. Okay, you'll see the arrow here. And that's because
this DB context dot vehicles within
the list of records. So we need to convert
this into two list. After converting
this into two lists, we still get an error. And that's because
these two lists will work with synchronous call. But this time we're using
the asynchronous calls. So let's use to list method. Alright. This is all the
namespace for these two lists, async, which comes from this Microsoft dot Entity
Framework Core namespace. Let's written this vehicles. Then we'll go to
another getMethod. And first of all, let's
make this method Async. Then we'll use the
await keyword. Right after that, we'll use DB context and pick
the Lakers property. After that, let's
call a helper method. That is fine. But we're using the async call. So let's use the
fine async method. And inside this
find async method, we need to pass this ID, this line with just certain specific vehicle against the ID, which will pass in this method. We'll store the result in a
variable and name it like. Alright. Now just
written this vehicle. Right after this getMethod
will go to the add method. First, we'll make
this method async, await keyword and
choose DB context. Then pick the vehicles
dividend property. And right after that, we'll call the helper method
that is add async. Inside this add method, we'll
pass this vehicle object. Now, this line will add the
data inside the database. But we have to call the
CF gene this method, because in order to perform
some insert, update, and delete operations, we have to use the CF
gene, this method. And because this
is an AC method, so let's use CFCs async here. Then we'll go to
the update method. First, we'll make
this method Async, then use the await keyword. And right after that we will use DB context dot
vehicles property. Now we'll call the find
async and pass this ID. Basically this line will find that vehicle record inside
a database against this ID. Okay, let's store the
result in a variable and rename it like an object. Alright, now with
this vehicle object, we can pick the label
glass properties. So let's pick W2
class properties and assign it accordingly. Now once we are done, then we'll call the Save
Changes is emitted. Alright, let's move towards
the delete legal method. First, we'll make this
async await keyword. And after this, we'll use DB context vector
v equals property. And let's call the
fine async method. Now, in satisfying async
will pass this ID parameter. As always, this line will fetch the vehicle record from
the database against this ID value stored in a
variable and name it Michael. Then we're going to
remove this vehicle. In order to do that,
Let's call that DB context dot labels, device ID property, and call the built-in helper
method that is removed. Inside this remove method, just pass this vehicle. Remember, in case if we move, we don't have an async method. So we just have to use
the remove method here. And as I said earlier, when bulls use the insert, update and delete operation, then we also need to call
the Save Changes method. Let's use the Save
Changes async here. We have completed
the service layer. Now, I'll see you
in the next lesson.
17. Extension Methos and IOC in Vehicle Service: Earlier, we have created
down Vico Service class, which is responsible for
the basic crud functions. Now in this lesson we're
going to work with the dependency injection inside the door open core project. Dependency injection, the
design pattern that makes a class independent
of its dependencies. In dependency injection,
we don't rely on concrete implementation
of a class. Rather we rely on
the interfaces. This makes our code
more maintainable. And almost in every enterprise project dependency
injection pattern is used heavily. Let's say we have an interface
and the service class. Now in future, we'll
add a controller class. And they are, Let's say
if we want to access the methods of vehicle
service class, then we need to
create an instance of a vehicle service class. Now, instead of
making the object, will use the dependency
injection IoC, Inversion of Control container, which will handle all
the dependencies. Basically in the
IOC container will disturb our interface and
vehicle service class. And it's the IOC container which will resolve the
dependencies for us. Now we don't need to create
an object of legal service for accessing the methods
of vehicle service class. We just need to pass the vehicle interface
in the constructor. And with the help
of this interface, we can access the
matters easily. Now by default, ASP.net Core comes with the built-in
IOC container. So we just need to use it. Let me tell you that in
Dart and six version, microsoft has moved the
Startup dot CS class. Earlier we register
the IOC container in the startup class. Now in doctrine six, Microsoft.net team has just used a single program.cs class, which will handle all the stuff. From now we'll work
with the program class. But if you're using some
older version of Dalton core, then you'll see the
static class here. Okay, We'll go to
the program file. They will choose
builder dot service. And right after that, we'll pick the ad Scope
method in which we'll add i vehicle interface and
the legal service class. Now, let's take responsibility
of this L scoped, which will create a new instance of vehicle service class. If you're using the older
version of Dortmund core, then you'll add this line in the configured service
method of startup class. Okay, So we have registered the dependencies in
the program dot CSS. Now, in the next lesson, we'll create the controller. So I'll catch you
in the next lesson.
18. Vehicles Controller: In the last lesson, we have
worked with the inversion of control container and we have resolved the dependencies
in the IOC container. Now, in this lesson, we'll
add a controller and inject the interface in the constructor of
the controller. So let's go to the
controllers folder and I'm going to add a
new controller class. Make sure you have
selected this API option. And then just pick this template
with readwrite actions. This template will
already contains some code and we just
need to modify this. Then I'm going to
name this controller to vehicles controller. Make sure that controller must ends with the
keyword controller, because it's a
naming convention. Let's add it. And here we go. You can see that this controller contains
some built-in code for GET, post, PUT, and delete methods. We'll modify these methods
according to your choice. First of all, let's
create a field of type, vehicle interface, resolve the namespace, and give it a name which is
underscore vehicle service. Now we're going to add
a constructor by using this snippet CTE OR, and twice hit the Tab key. Okay, now let's inject the eye vehicle interface
inside this constructor. Right after that, we'll assign this vehicle service to this
underscore vehicle service. Now with this underscore
vehicle service will pick all the methods that we have written in the
I vehicle interface. Let's go to this getMethod. Currently it's returning
the list of string. Let's remove this code. And instead of string, we'll use the eye
innumerable of Laker. Just make sure to solve
namespace for the modal class. Okay, now we'll add
the async keyword along with this method. And right after that, we'll use this underscore
vehicle service and pick this get
all vehicles method, which will return
the list of labels, will get an error. And it says the type of this
method must be dusk. Use the task. Because
this call is async. So we also need to use
the await keyword here. Now we'll go to the other
overload of getMethod. And this method is
returning the string. Instead of using the
string will make the type of this method to
dusk off label. Use the async keyword. Then let's return underscore vehicle service dot get
labeled by ID method, which will take the ID
parameter as past this id here. And don't forget to
use the let keyword. Okay, now let's go
to the post method. And they will first
make the type of this method to task and
use the async here. Right after that we
will use the await. Then use underscore
vehicle service and pick the ad legal method. This ad vehicle method will take the object of vehicle class. Instead of using the string, let's add the
vehicle class object and pass this vehicle here
inside the add method. Similarly, we'll go
to the put method. And this PUT method is
just like the post method. There's only a slight
difference between these two methods and that
is the use of ID parameter. First will make the type
of this method to task. Then use the async. And right after that we'll pass the vehicle here as a parameter. Now we'll use the await. Then choose underscore
vehicle service, and pick the update
vehicle method, which will take the
id and the vehicle object. Okay, let's pass it. Now we'll shift to
the delete method. And here, let's pick
the type of this method to dusk and use
the async keyword. Now, let's use the await here. And along with that, we'll use underscore Maker service
dot delete method, which will take an ID parameter. Okay, so we have completed the functionality of
the controller class. Now let's run this API and test that good functionality
in the next lesson.
19. Run Vehicle Microservice: In the last lesson, we have worked with the
controller class. Now in this lesson we will run our project and Tesla
vehicles microservice. So let's quickly run this
project inside of our browser. And here's you'll
see the struggle documentation page for our API. At this moment, data equals table inside a
database is empty. So first we will go
to the post request and click this triad option. Then we need to add
the JSon data here. Just remove the ID because
ID is auto incremented, so we don't need to
pass the ID here. Now, let's add some decent data and execute this request. Here we go. Let's add more data. Now let's go to the get request. Choose tried out, and
execute this request. As soon as we will execute this request will
get all the records. Let's say we just want to get
the data of this vehicle, will simply copy the
idea of this vehicle. Then we'll go to the
other getMethod. And let's pass this id
and execute the request. This time, instead of
getting all the Legos data, we only get a single
vehicle data. Okay, now let's say
we want to update. This radical will go
to the update method. And let's add the ID of the
vehicle and gender JSon data. Now, let's execute the request to check whether
this vehicle data has been updated or not, will again send a get request. And this time you'll see this vehicle data
has been updated. Okay, now let's say we want
to delete this vehicle. So we'll go to the
delete request. We're going to pass the vehicle
ID and just execute it. Now let's check
whether this data has been deleted or not. So we'll send a get request. And here we go. We don't get any data. Alright, so far, we have worked with the vehicle microservice. Now, in the next section, we'll work with our
customers, microservice. So I'll see you in
the next section.
20. Overview Customer Microservice: In this section,
we're going to create the micro service for customers. So let me tell you
the basic flow of this customer's microservice. This Christmas micro
service will be based on a single method which
is at customer. Instead the client side. After mobile application
or web application, the customer can
add the details. And behind this,
the post method or add customer method
will be executed. You can also add other methods in discussion as micro service, just like we have added in
the vehicle microservice, like get all customers, get customer by id, update customer and
delete customer. But our requirement is just one method which is
an add customer method. So let's start writing
these customers micro service in
the next lesson.
21. Create Models For Customers Service: In this lesson, we're going to start getting that customers microservice will open this customers
micro service project. And then first of all, we'll add a modal class. And for this purpose, we'll get a new folder
and name it models. Now inside this model's folder, we're going to add a
new C-Sharp class. We name it customer. Okay, here we go. Inside this Christmas class. Let's add some properties
like id of the customer, name of the customer, e-mail, and phone
number of the customer. Okay, here we have added the necessary properties
for customer mortal glass. Now let me tell you that
what the customer wants to book a test drive for
some particular vehicle. Then we also need to handle
that vehicle service data. For example, let's say that the customer wants to book a test drive against
this vehicle. So that means we also
need to send them vehicle data to the
customer, microservice. Normal rest APIs. We can achieve this via
relations between tables. But here, the vehicle
microservice has a separate database and the customer microservice
will not create a database after
vehicle microservice, but the customer microservice will replicate the
data of the vehicle. Microservice is the general
rule of microservice. That to microservices can
not share that database, but they can share the data. So we'll apply this principle to our customers microservice. And here in Santa customers micro service will
need to model classes. One for customer N14 vehicle. When the customer will
book the test drive. Then we'll also send them vehicle name to the
customer, microservice. In the vehicle class, we just need two properties. Now, equal id and
then legal name. One customer can book a
single vehicle at one time. So the vehicle ID will be the foreign key in
the customer table. Okay, we'll add a property
in the customer class, and let's name it Michael ID. Now let's add a
property of glass Michael and name it Michael. This will create a
one-to-one relation between the customer
and the vehicle class. But where is the vehicle class? Let's get our vehicle class
in this model's folder. And here we go. Now we'll
add two properties, one for vehicle ID and the
other is for legal name. Once we'll add the
vehicle class, then you don't see any error
in the customer class. Okay. Now, let me tell
you that the ID in the vehicle class will
be auto incremented. But we want to pass that ID, which will get when the customer wants to
book some vehicle. Let's say this vehicle
has the following ID. So when the customer wants to book a test drive
of this vehicle, then we need to store the same vehicle ID in the vehicle table. On top of this id will add a key attribute and use that database
generated option to none. That means ID in this
table is primary key, but it's not auto-increment it. Okay, we have
created two classes, one for customer N14 vehicle. Now, in the next lesson, we'll get the DB context class. So I'll see you in
the next lesson.
22. DbContext For Customers Service: In the last lesson,
we have created the modal class for
customer and vehicle. And now in this lesson, we're going to create
the DB context class. I've already told you
that DB context act as a bridge between your model
class and your database. So we'll save our time and
create a folder, name it data. Then inside this data
folder within the class. So instead of
creating a new class, let's copy the API
DB context class from the vehicle microservice. And then we're going to
paste this class inside the data folder of
customers microservice. Okay, now here we need
to fix the namespaces because now we are inside a customer's
microservice project. So we'll add the namespace
for customers project. Then we need to DB
set properties. One is for vehicle and the
other is for customer. Db set property is
already present. So we'll add a new
property of type, customer, name it, customers. Now let's provide the
connection string to this DB context class. We defined our connection string inside this own
configuring method. This time we have working with the customers micro
service projects. So we will not change anything here other than
the database name. Okay, so I'll see you
in the next lesson.
23. Migrations in Customer Service: In the last lesson,
we have set up the connection
string and we have also created that
DB context class. Now, in order to
create the database, we need to add them migrations. To enable the migrations, make sure you have installed
this NuGet package inside your customer's
Microsoft Project. Okay, now we'll go to the
Package Manager console. And let's add the migrations. But before adding
the migrations, the select the project in which we're going to add
the migrations. This time we want to add the migrations in the
customer's microservice. So make sure customer
project is selected. Then in the Package
Manager console will also select the
customer project. Okay, Now let's add a
command that is add dash migration and provides a user-friendly name
to the migration. I limit initial create
unless it enter. Okay, here we go. This will generate the migration script. The script will be used to
add the database and tables. But we have not actually
created the database yet. Again, in the Package
Manager console, we'll use the command update dash database. And
let's hit Enter. Now this will add the
database and tables inside the SQL Server
Object Explorer. Let me go to the SQL
Server Object Explorer. There. Let's refresh the server. And if we expand this server, then we'll see the
Databases folder. If we will expand this further, then we'll see our database
for customers microservice. And if we will
expand this again, then inside the tables will see the tables for vehicles
and customers. Okay, So far, we've added
a database and the tables. Now, let's continue our
work in the next lesson.
24. Create Interface For Customers Service: In the last lesson, we have
worked with the migrations. And now in this lesson, we're going to start
with the interfaces. So what we're gonna
do is we'll use the same strategy that we have used in the vehicle
micro-service. First of all, let's
add a new folder and name it interfaces. Then inside this folder, Let's add a new interface. We're going to name this
interface to customer. Alright, let's add
n Here we are. Now inside this interface, we're going to add a
method of die das, name this method
to add customer. This customer method
is basically used to add the customer information. And inside this method, Let's pass the parameter
of type customer. And for this customer,
we need to add the namespace for models folder. Alright, so we just
need one method for adding the customer and
which we have added. If you want to add other
methods like get customers, opted customer or
delete customer, then you can apply the same strategy which we have used in the vehicle interface
of legal microservice. But that's not a real
project requirement. We will go ahead with
this one method. Now, in the next lesson, we'll add the services from
this interface method. So I'll see you in
the next lesson.
25. Create Customers Service: Earlier we have worked
with the interfaces, and now in this lesson, we're going to work
with the services. So let's get a new folder
and rename IT services. Then inside this folder, we're going to add a
new C-Sharp class. Then we name it
customer service. Alright, Inside this
customer service, we write the implementation
of method which we have defined in the eye
customer interface. Let's implement this
customer service class from II customer interface. Alright, there's all the
namespace for this interface. Then we need to
implement the method for this high
customer interface. Now, we just need to add the logic inside this
add customer method. But before this,
let's add a field of type EPA DB context,
result, the namespace. Then we'll name this
field to DB context. Right after that, we'll
add a constructor. And here we will get
a new instance of API DB context and assign
this to DB context. Now, with this DB context, we can pick the DB set customers
and vehicles property, which we have defined in
the APA DB context class. Let me tell you the logic which will write inside this method. Suppose a customer wants to book a test drive
against this vehicle. So when he will provide the customer details
than a record for customer a against this
vehicle ID will be added. Now, customer B
also wants to book a test drive against
the same vehicle. So in that case, customer B record will be added. But as you know that Customer a and Customer B wants to book a test drive against the same vehicle and the
vehicle ID is obviously same. That means we'll get
the primary key error. Because the general
principle of database table is that primary key is unique. So what we will check, the customer will book a test
drive against some vehicle. Then we will first query
from the vehicles table and check whether the vehicle is present inside the
database or not. If no records exist. Then we'll first add a new vehicle inside
the vehicles table. And right after that, if the vehicle is already
there inside the database, then we'll set null to
this vehicle property of customer class because the
data is already there. And now there is no need to add the same legal record again. And right after that, we'll add the customer
inside the database. Okay, let's add this code
inside the add customer method. First of all, let's
make this method Async. Then use the await keyword. And right after that, we'll pick the DB context
dot vehicles DB set property called the first
or default async method. And inside this, we'll use
the lambda expression and check whether the ID of the legal stable is equal
to that vehicle ID, which we will get from
this customer object. Then let's store the
result in a variable and name it legal in dB. Okay, now we'll check if
the vehicle in dB is null. Then let's add the weight
given to the DB context dot vehicles dot add async. And inside this, let's
pass this customer. Then. As always, we'll use the DB context dot save
changes async method. And don't forget to
add the weight here. Okay? Now, if delta equal Racket is already present
inside the database, then there is no need to add the same vehicle again
in the database. Because this will cause a
violation of primary key. Because the primary key is
unique for every bucket. So in that case, we don't want to add
the new vehicle record. And we'll set customer
vehicle equals null. And right after that,
we'll add the customers inside the database by
using await keyword. Then we'll choose DB
context dot customers, dot, add async, and pass
this customer here. Right after that, we'll call
a Save Changes async method. Okay, here we go. We have completed
the service layer. Now, I'll see you
in the next lesson.
26. Extension Method and IOC in Customer Service: Earlier we have created the
customer service class. Now, in this lesson,
we're going to work with a dependency injection inside
the document Core project. As we discussed earlier,
that by default, ASP.net Core comes with the
built-in IOC container. So we just need to use it. Let me go to the
program.cs file. And they will use the
builder dot service. Right after that, we'll
pick the ad Scope method in which we will add the
customer interface and the customer service class. Now, it's the responsibility
of this ad scope, which we'll get a new instance
of customer service class. Okay, So we have registered the dependencies in
the program.cs class. Now, in the next lesson, we'll create the controller. So I'll get you in
the next lesson.
27. Customers Controller: In the last lesson, we have
worked with the inversion of control container and we have resulted dependencies
in the IOC container. Now, in this lesson, we'll
add a controller and inject the interface in the constructor of
the controller. So let's go to the
controllers folder. I'm going to add a
new controller class. Make sure that you have
selected this API option. And then just pick this template
with readwrite actions. This template will
already contains some code and we just
need to modify this. Then I'm going to
name this controller to customers controller. And let's add it. You can see that this
controller contains some built-in code for GET, post, PUT, and delete methods. In the customer's controller. We don't need all
these action methods. We just need a post method. So first of all, let's
remove the rest of the code inside the
customers controller. Okay, here we go. Now let's get a field of type II
customer interface. Here's all the namespace. And give it a name which is
underscore customer service. Then we're going to
add a constructor and inject the
customer interface inside this constructor. Right after that,
we'll assign this customer surveys to this
underscore customer service. Okay, now we'll go
to this post method. Let's make the type
of this method to task and use the async here. Right after that, we would
use the await keyword. Then we use underscore customer service and pick
the add customer method. This add customer
method will take the object of customer class. So instead of using
the string here, let's add the
customer class object and then pass this customer here inside the add
customer method. Okay, so we have completed the functionality of
the customer class. Now let's run this API and test add Casper functionality.
In the next lesson.
28. Run Customers Service: In the last lesson, we have worked with a
controller class. And now in this lesson, we'll run our projects and tests the customers
micro service. If you want to run
both customers and vehicles microservice, then right-click
on the Solution. Then click Set as data projects. Currently, single starter
project is selected. So we will choose multiple
starter projects and then pick the Start option for this customer and legal
project and hit Okay, right after that, let's
run the projects. And this time, we'll Studio
will build two projects, one for customers and the
second is for vehicles. Okay, let's go to
the vehicles project and execute a get request. And here we'll get
the list of labels. Let's say the customer
wants to book a test drive against
this vehicle. Obviously, the
customer will have a good-looking user
interface. For this purpose. You may have a mobile
application or web application for
booking the desk drive. But right now, we didn't have
micro-service development. So we need to pass the
vehicle ID manually. In case of web application
or mobile application, this will be done behind
the user interface. And the user who is using
the mobile application will have no idea what's
going on in the back-end. You'll just see the
user interface. So we'll suppose that
the customer wants to book a test drive for
this particular vehicle. So we'll switch to the
customers microservice. And there, Let's go
to this post request. And right after that, let's
add a customer's data. Then we need to pass the
vehicle ID and legal name here. Then let's see the post
request and here we go. Now check whether this
record is added or not. So we'll switch to
the SQL Server Object Explorer and let's refresh it. Open the database for
customers microservice. And here, if I will, open the customer table and you'll see that the
record has been added. Similarly, we'll open
the vehicles table and the record is present
here. Now let's see. Another user wants to look at desktop for the same vehicle. In the customer's microservice will add the details
of another user. And because he also wants to book a desktop for this vehicle, so we will not change
that label id. Okay, let's send a request. And here we go. Now in the customers table, if we refresh it, then
we have two customers. And if we refresh
the vehicle table, then here we just have
one vehicle because to customers wants to book a test drive for
the same vehicle. And that's why we only have one vesicle inside the database. Okay, so I hope you
got an idea of what's actually happening in the
customer's microservice. That's all from this lesson. Now, I'll see you in
the next section.
29. Azure Service Bus Overview: In this section, we're going to work with the Azure Service Bus. Let me tell you what
is Azure Service Bus? Azure Service Bus is basically a cloud messaging
service and it's fully managed enterprise
message broker with message queues and
publish subscribe topics. Basically, it's used to connect any
applications, devices, and services running
in the Cloud to any other applications
or services. With the help of Azure
messaging service, we can exchange data between two or more than two services. Earlier we have created the
customers microservice, and I want to tell you that men will add the
customer's data. Then we will also send a copy of data to the Azure
messaging service. And then we'll get the
reservations microservice. Then we'll get that data
from the Azure Bus. Tick off azure burst as
a queue where a center, which in our case is the
customer's microservice, will send a message to the
Azure messaging queue. And later the receiver, which will be the
reservation service, will get that message
from the Azure Queue. So in this section, we'll work with the center part of the Azure messaging bus. And later when we'll use that
as a vision microservice, then we'll work or the receiver part of
the Azure Service Bus. Okay, so let's start working on the Azure Bus from
the next lesson.
30. Setting Up Azure Service Bus: In this lesson, we need to create the Azure
Service Bus queues. So let's go to the Azure portal and we're going to select this, Create a Resource option. Okay, then in the search
bar, Research Service Bus. And just select this. You'll see the details
of service bus like a sack cloud based
messaging service, providing cues and topics. In this course, we'll
work with the queues. Let's create the Service Bus. First of all, we need to
choose the resource group. Think of this resource
group as a folder in which we can add
multiple things. Let's create a new
resource group at Gibson user-friendly name. Alright, then let's
add the namespace. Well, a namespace is actually the URL of your Service Bus. Make sure this is unique. Then we need to
choose the location where we want to get
the Service Bus. Just select any location, RG or nearby location. Then we have an option
of pricing with some plants like basic,
standard and premium. In this course, we will
use the basic plan. If you'll click this View
Full pricing details, then you'll see
all the plans with their prices and
functionalities. The basic plan is quite cheaper. So we will choose this because we're not working
with the topics. So this basic plan is
perfect for this course. Let's click Review
plus Create option. And this will perform
some validation check. Once the validation
is succeeded, then we'll select this option. Deployment starts. The deployment is in progress. So we need to wait here. We will see a message, we see
the deployment is complete. Now let's go to the
Resource option. We need to create the queue. Here. We'll select
this kid Q option. And then we have been
asked to name our cube. Let's provide some user-friendly
name to our queue. Then we'll go to the
default settings. Here I need to tell you that this message time is 14 days. It means if the message will
not retrieve from the queue, then after 14 days the message will be
removed from the queue. You can increase or
decrease the time. But I'll go with the default
settings. Let's get the Q. And R Q is currently
being created. Soon we'll see a
message which says your Q was successfully created. Now, in the next lesson, we'll send that
data to this cube. I'll see you in the next lesson.
31. Azure Sender Service: In the last lesson, we have created the Azure
Service Bus queue. And now in this lesson, we're going to send
that data from the Christmas micro-service to the Azure Service Bus queue. So first of all, let's
go to the studio. And they will right-click on the customer's
microservice project. Select Manage NuGet packages. And right after that, lets us this NuGet package
for Azure Service Bus. The name of this package is Azure dot messaging,
dot service plus. Alright, we're going to
install this NuGet package. And here we go. Once the installation is done, then we'll go to the Project
link of this package. And here you will see
Getting Started guide for using this package.
Just scroll down. And here we have a sample
code which tells us how to send a message to
the Azure Service Bus. First, we'll deduct
connection string for our Azure Service Bus. Then you'll need a queue name where we want to send a message. Right after that, we'll
create a service bus line, which takes a reference
to the connection string. Then we'll get a center, which we'll get a
reference to the queue. Then we'll get a
Service Bus message with take a message
that we want to send. And finally, the sent
message ac method will send a message to
the Azure Service Bus. Similarly, when we want
to receive the message, then we'll use this code. But right now our focus is to send a message to
the Azure Service Bus. So we'll focus on this code later when who worked on the
reservations microservice. Then we'll also talk about this received message code
for Azure Service Bus. Okay, so we'll go to
the video studio. And I'm going to
add the code for sending data to
Azure Service Bus. Let me go to the
Customer Service. And inside this add
customer method, we can to write the
code for sending data to Azure Service Bus. So we'll go to the GitHub
Documentation page of Azure Service Bus. And they will see this code, just copied that
part of this code. And then in the
customer service class, we'll paste this code. Now, we need to modify
this code a little bit. First of all, we need to
resolve the namespaces. Then we need to pass the connection string
and the queue name. In order to get the
connection string will switch to the Azure portal. And then we'll go to
the Azure Service Bus. Will open the Service Bus. And right after
that, we will see the Shared access policies. Just click this
and then we'll see this road manager shared
access key will select this. Then we'll see a primary
connection string. We're going to copy
this connection string. And right after that,
we'll switch it up. We'll studio space
disconnects this thing here. Okay, Now what about
the queue name? Again, we'll switch
to the Azure portal. And on the right-hand side, we'll see these cues.
Just select this. And here we'll see
our queue name. Will pass this queue name inside a customer service class. Alright, here we go. Now let me tell you
that this code is perfect to send the data
to the Azure Service Bus. But the problem here
is this code will just send a string message
to the Azure Service Bus. But we want to send
the customer object. So what we're gonna do is we'll first satellites the
customer object to JSON. And then we'll send
the customer object. So we'll use decent convert dot civilized object and 40s Jason convert to use the
neutral soft NuGet package. And the namespace. Then
industrialized object will pass this customer object. And this object will convert this Christmas object
to JSON string. Let's total result
in a variable. And we then made object as text. Then the Service Bus message will pass this object as text. Alright, now let's
run the project. And let's say the
customer wants to book the vehicle desk drive
against this vehicle ID. So we'll pass the
necessary details here. And then let's send the post
request. Okay, here we go. Now, if we go to
the SQL database, then we'll see that that
customer record has been added. Then we'll go to
the Azure portal. In the Azure Service. Let's go to the cues,
select our queue. And right after that, we will see this
service bus Explorer preview option, deselect this. We'll adapt this big option. Here we'll see the details in the JSON format which we send from the CRISPR
micro service. Alright, so we have edited
data to the Azure Bus queue. Now, when we work
with the reservations microservice then will consume this data from the
Azure Bus queue. So that's all from this lesson. Now, I'll see you in
the next section.
32. Reservations Service Overview: In this section,
we're going to get the microservice
far as iterations. Let me teach you the basic flow of this reservations
microservice. This reservation
microservice will be based on two methods. And these are get reservations
and update status. When the customer will book a
test drive of some vehicle, then one copy of that data will go to the
Azure Service Bus queue. And now in the
reservation service will call the get
reservations method. And inside that get
reservations method, first, we'll fetch the data from the Azure
Service Bus queue. Then we'll store the data
inside the database. And right after that will display the list
of reservations. And the other method
is update male status, which will be used to send a confirmation email
to the customers, will explore things as we
move forward in this course. Okay, so let's start writing that admissions micro
service in the next lesson.
33. Create Models For Reservations Service: In this lesson, we're
going to start getting the reservation
microservice will open this reservation
micro service project. They are, first of all,
we'll add a modal class. And for this purpose, we'll create a new folder
and name it models. Inside this folder, we need
a class which will hold the data which we want to fight from the Azure
Service Bus queue. Inside the Azure
Service Bus queue, you will see that we have
following JSon data. Let's copy this JSon data. And then we'll go to the
gym to C-sharp website. And they're just paste this DSM engineered
a CTL properties. Okay, here we go. We have two classes than glass. At the root class. Let's create these
glasses inside the reservations
microservice project. Inside that we'll studio. We're going to create
a new C-Sharp class. And then we named
this class to legal. Right after that, we'll
copy the properties of this vehicle class and
paste it inside this glass. Now we'll get another class. Well, Jason to CTL website has
named this class to route, but we want to give some
other name to this class. Let's say we want to
name this class to Reservation Data class, intimate reservations. Okay, now we'll copy these properties and paste it inside the reservation glass. Now, inside the
reservation class, Let's add one more property
which tells us whether the confirmation email has been sent to the customer or not. So we'll add a property of dipole and name
it is male scent. Then in that particular class, we can't make this
ID as a primary key because we'll get this id from the Azure
Service Bus queue. And what if two customers
booked the same vehicle? Which means the data that will get will be in the
following format. Where two different customers, the vehicle test drive for us same vehicle and the
vehicle ID is obviously same. And if you try to store
the same vehicle ID twice, then we'll get an
error of primary key, because primary key
must be unique. So we'll add another property in the vehicle class and name it V. Let's make this property
as a primary key by adding the key attribute and then dissolve the namespace.
Okay, here we go. We have created
the model classes. Now let's continue our
work in the next lesson.
34. DbContext For Reservations Service: In the last lesson,
we have created the model class for
reservation and vehicle. And now in this lesson, we're going to create
the DB context class. I've already told you
that DB context act as a bridge between your model
class and your database. So we will see a time and
create a folder, name it data. Then inside this data
folder within the class. So instead of
creating a new class, we're going to copy the
APA DB context class from the Waco microservice and then paste it inside
the data folder of reservation micro
service project. Now, we need to fix
the namespaces here, because now we're inside the reservation
microservice projects. So we will add the namespace
for reservations project. Then we need to DB
set properties. One is for radical and the
other is for reservation. So we're going to add
a new property of type reservation and
name it reservations. Okay, now let's provide
the connection string to this DB context class. We defined our connection string inside this on
configuring method. This time we're working with our reservation
microservice project. So we'll name our database
to reservation API db. Ok, now in the next lesson, we'll add the migrations to add the database
inside the SQL Server. So I'll see you in
the next lesson.
35. Migrations In Reservation Service: In the last lesson,
we have set up the connection
string and we have also created that
DB context class. Now, in order to
create a database, we need to add them migrations. To enable the migrations, make sure you have
installed the NuGet package inside your reservations
microservice project. Then we'll go to the
Package Manager console. And we're going to
add the migrations. But before adding
the migrations, just select the project in which we're going to add
them migrations. This time we want to add the migrations in doubt
reservation microservice project. So make sure as innovation
project is selected. Then in the Package
Manager console will also select the reservation
microservice project. Now let's add a command
that is at the migration. Then provide a user-friendly
name to the migration. I'm going to name it initial, create, ls, hit Enter. This will generate
the migration script. This script will be used to
add the database and tables, but we have not actually
created the database yet. So again, we'll switch to the Package Manager
console. Here. I'm going to add a
command that is update dash database unless it Enter. And this will generate
the database and tables inside the SQL
Server Object Explorer. So let's go to the SQL Server Object Explorer and then less effect the server. Okay, If we expand this server, then we will see the
Databases folder. If we will expand the databases, then we'll see our database for reservations microservice. And if we will
expand this again, then inside the tables
will see the tables for vehicles and reservations. Okay, so far we have added
the database and the tables. Now let's continue our
work in the next lesson.
36. Create Interface For Reservations Service: Earlier we have worked
with the migrations, and now in this lesson, we're going to start our
work with our interfaces. So what we can do is we'll use the same strategy that we have used in the legal micro-service. Let's add a folder and
name it interfaces. Then inside that folder, we're going to add
a new interface. And this time we're
going to name this interface to reservation. Okay, let's add it. And here we go. Now
inside this interface, let's add a method of die, das list of reservation. Then there's all
the namespace name. This method to get reservations. This get reservations method is basically used to
get the list of reservations which will get from the Azure
Service Bus queue. Once we will get
the reservations, then we will send a
confirmation email to those customers who book
the vehicle for test drive. So we will add another
method of type task. And then we'll name this
method to update male status. And this method will
take the reservation ID, will pass ID here
of integer type. Okay, so we have added
these two methods. One for getting televisions
list and the other is used to send a confirmation
email to the customers. Now, in the next lesson, we'll add the services from
the interface methods. Okay, so I'll see you
in the next lesson.
37. Create Reservations Service: We have worked with
the interfaces, and now in this lesson, we're going to work with the services. So we're going to
create a new folder and name it services. Then inside the folder, we're going to add a
new C-Sharp class. At this time we name it
has elevation service. Okay, now, inside the class who write the implementation of methods which we have defined in the
reservation interface. So let's implement
this exhibition class from reservation interface. Then result in namespace
for this interface. And right after that,
we need to implement the methods for this either
as a visual interface. Okay, so we just need to
add the logic inside this, get reservations and update
male status methods. But before this,
let's get a field of type API DB context,
all the namespace. Then we'll name this
field to DB context. Then we're going to
add a constructor. And here we'll create
a new instance of API DB context and assign
this to this DB context. Now with this DB context, we can pick the DB
set reservations, and vehicles property,
which we have defined in the APA
DB context class. Let me tell you the
logic which will write inside the get
reservations method. As you know that we have Azure Service Bus queue
where we have sent a data in the customer's
microservice and now in the reservation
microservice will fix that data from the
Azure Service Bus queue. Then we'll save that data inside the MS SQL
server database. And right after that
will return the data. So we'll fetch the data from the Azure Service Bus queue
later in this course. So I'll add a comment
here that we will add the Azure Service Bus
receiver code later. Then we'll return this DB ctx dot reservations,
da2 list async. Don't forget to add the async await and make sure to
resolve the namespaces. We have left this step
where it will fetch the data from the Azure
Service Bus queue. But don't worry,
we'll move forward, then we'll complete
dysfunctionality. Let's move to the second method, which is update status. This update meal status will add a logic to send a confirmation
e-mail to the customer. Let's say we have the
following data in their reservations table
for few customers. Now we want to send
a confirmation email to this customer. So we will use this update
male status method. We will pass the reservation ID. Then we'll check whether
the record is present in the reservations table for
this reservation ID or not. If the record is not null and this is male scent
property is false, then we'll send an
e-mail to the customer. And right after that, we'll
set that is mail sent property to true and
just save the changes. So let's add this code inside
this update status method. First we'll add this DB
context dot reservations dot find async method
and pass this id. Then we use async await. Right after that we will
store the result in a variable and just
name it as a result. Now we will check if the
reservation result is not null. And this is male-centered
property is false, then we'll add the logic to send that e-mail
to the customer. So for sending emails, let's initialize
the SMTP client. Then dissolve the namespace. In this SMTP client. First we'll set the
port number 2587. Then we'll add a
property credentials and initialize a new instance
of network credentials. Then inside this will pass
the email and password. For this tutorial. I'll
recommend you to make some extra e-mail
for testing purpose. Then we'll use the enable
SSL and set to true. Once we add that
part of the code, then we'll use this SMTP client
and call the send method. And descent method
will take four things from two, subject and polish. The first parameter,
which is from, we'll pass our email address. And in the second parameter, which is to the recipient, we need to pass the e-mail. That customer really
want to send that email. And we'll get this from
the database by using the reservation itself,
Dodd email property. Then in the subject, Let's pass a message. And also in the body, just add some text. Alright, this code will send a confirmation e-mail
to the customer. Now, once we will send the
e-mail to the customer, then we need to set this is
male scent property to true. And right after that, Let's call the Save
Changes async method. Okay, That's all
from this lesson. Now, I'll see you
in the next one.
38. Extension Method and IOC in Reservation Service: Earlier, we have created the
reservation service class. Now, in this lesson,
we're going to work with the dependency injection inside the doctrine
core project. As we discussed earlier, that bite fork.net core comes with a built-in
IOC container. So in this lesson,
we're going to use it. Let me go to the
program.cs class. And here we'll use the
builder dot service. And right after that, we'll
pick the ad Scope method in which we'll add the
ideas innovation interface and the reservation
service class. Now, it's this possibility
of this add scoped, which we'll get a new instance of the reservation
service class. Okay, So we have registered the dependencies in
the program.cs class. And now in the next lesson, we'll create the controller. Okay, I'll catch you
in the next lesson.
39. Reservations Controller: In the last lesson, we have
worked with the inversion of control container
and we have to solve the dependencies in
the IOC container. Now, in this lesson, we'll add a
controller and inject the interface in the
constructor of the controller. So let's go to the
controllers folder and I'm going to add a
new controller class. Make sure you have
selected this API option. And then just pick this template
with readwrite actions. Now let's name this controller to reservations controller. You can see that this controller contains some built-in
code for GET, post, PUT, and delete methods. In the reservations controller. We don't need all
these action methods. We just need two methods. One for getting data
that vision list. And the second is used to send the e-mail to the customer. For sending the e-mail
to the Customer, we can use the put method. So we'll remove other methods except the port
and the getMethod. Now, let's create
a field of type either salvation interface is all the namespace and
give it a name which is underscore
reservation service. Then we'll add a
constructor and inject the reservation interface
inside the constructor. Right after that, we will assign this reservation service to this underscore
reservation service. Okay, now we'll go
to this getMethod. First of all, let's change the
return type of this method to dusk I innumerable
of preservation. Then use the async here. And right after that, we
will use the await operator. Then we'll use underscores envision service and pick
the get reservations method. Okay, right after that, we
will store the result in a variable and name
it reservations. And finally, return
the reservations. Then we'll go to the put method. Here. We don't want to
pass the object because this update middle status will
just take an ID parameter. So we'll remove the other
parameter from this method. Then we'll turn the type
of this method to task. And right after that, we'll choose underscore
reservation service. Then pick the update
middle status method and pass this id here. Don't forget to add await async. Alright, so we have completed the functionality of
the controller class. Now in the next section, we'll fix that data from the
Azure Service Bus queue, and then we'll complete the admissions method of
reservation service class. Okay, I'll see you
in the next section.
40. Azure Service Bus Receiver Overview: In this section, we're going to work with the Azure Service Bus. Later in this course, we
have sent the data to the Azure Service Bus queue when we've worked with our
customers microservice. Now in this section, we'll fetch that data from the Azure Service Bus queue will target this
reservation project. And inside this, I'm going to
install the NuGet package. As always, we'll right-click
on the reservation project, then go to the Manage
NuGet packages. And here I'm going to search this new vote for Azure
messaging service bus. Alright, let's
install this package. And here we go. It's solution has
been completed. And now in the next lesson, we will write the code to fetch the data from the Azure
Service Bus queue. So I'll see you in
the next lesson.
41. Azure Consumer Service: In this lesson, we need to fetch the data from the Azure
Service Bus queue. In the last lesson,
we have installed this NuGet package for Azure
messaging service bus. Now, if you'll go to this
project link of the package, then we will see the sample code of how to use this package. Scroll down. And this is the
code I was talking about. We have used this
code earlier in the customer's
micro-service to send the data to the
Azure Service Bus. And now we'll use
this code to fetch the data from the
Azure Service Bus. There's only a slight
difference between the sender and the
receiver code. Earlier we have used
this code to send the data to the Azure
Service Bus queue. Now let me tell you
that we will still use these lines of code
for connection string, queue name, and
Service Bus client. But rather than using
the sender code, this time we'll use
the receiver code. We'll go to the customer
service project. In the service class. We have used these lines of
code for connection string, queue name, and
Service Bus client has copied these lines of code. And then we'll go to the
red emission service. And in the get
reservations method, I'm going to paste this code. Okay, now let's resolve
the namespaces. Then we'll go to the documentation
page of this nugget. And let's copy
these lines of code and paste it inside the
reservations method. Now, let me tell you that
currently this code is used to get a single string message
from the Azure Service Bus. But we want to get the list of records and the records
are in the JSON format. So this service bus received
message will be of type. I, need only list. Let's give some name
like received messages. Okay, right after that,
Let's say we want to fetch ten records in
a single request. So we will pass Then here. Then we check if the
received messages are null. Then return null here. Otherwise, bulls use the for
each loop for arbitration. So let's add a for-each loop. And here we want to hydrate
in this received messages. So we will use
Service Bus received message and name it
received message. Then we'll add in keyword and use the received messages,
which is our list. Okay, we'll cut
these lines of code and paste it inside
this foreach loop. Basically this line is
used to get the body of the data and convert
it into string. Okay, now the data that will get will be in the form of JSON, and we need to
convert the JSON data into C-Sharp objects. So we'll use just one convert. Dissolve the namespace. This decent convert comes
from the neutrals of package. Then we'll call this
satellites object. We want to map this body according to the
reservation class. We will pass reservation
here and body here. Then let's store the
result in a variable and name it message created. Once will fetch the data, then store it inside
the database. So we'll use DB context, dot reservations, add async and pass
this message created. Then use the await keyword. And right after that, Let's call the Save Changes
async method. Okay, This code is enough
to fetch the data from the Azure Service Bus and store the data in the
reservations table. Now, let's say we
have five lectures inside the Azure
Service Bus queue. And was will fetch those five records and store the result in
reservations table. Then we need to remove those five records from the
Azure Service Bus queue. And on the documentation page, you can see that we have
this complete message code. And it says that
in order to remove the message from the
queue or subscription, we need to call the
complete async method. We will use this line of code. What I'm gonna do is I'll copy this line and then paste it
inside the for each loop. Alright, we have added
all the code for fetching the data from
Azure Service Bus queue. Now, in the next lesson, we'll run the reservations
microservice project and check the functionality. So I'll see you in
the next lesson.
42. Run Reservations Service: In the last lesson, we
have created the code for getting data from Azure
Service Bus queue. Now, in this lesson
we're going to run the reservations micro-service to check the functionality. So before ending, that is
invasions micro-service. First, Ireland, the customers
microservice and try to add some data inside the
Azure Service Bus queue. Okay, here we go. We
have added few records. Now let's try to run that
as admissions microservice. And here we go. Our reservations micro
service is running. Now let's try to call the
get reservations endpoint. And behind the scenes, it will fetch the data from
the Azure Service Bus queue, and we'll add it inside the reservations and
the light goes stable. And finally, we'll
get the result here. Okay, here we go. We'll get the data. Now, if you'll go to
the reservations table and vehicles table, then we will see the
records which we got from the Azure
Service Bus queue. Now let's say we want to send a confirmation email
to this customer. So we'll pass the ID
of this customer in the update status method and send that request.
Okay, here we go. Now, if the event is
correct that it will send a confirmation e-mail
to the Customer here. I have checked it and you'll see that we've got a
confirmation email. Okay, That's all
from this lesson. Now, I'll see you in
the next section.
43. Api Gateway Overview: In this section, we'll
talk about an API gateway. So what exactly the
APA gateways and why we need API
Gateway microservices. Let me tell you that
an API Gateway is an API management tool that sits between a client and a
collection of back-end services. This definition is 100% correct. But let me make things easier. Suppose you have few
micro services like vehicles, customers
and reservations. And a client, which could be a mobile device or application, can talk to our microservice
with the different URLs. Instead of giving an
access to all these URLs, we'll add a gateway right after the client or in front
of the microservices. Now, in the API
gateway will configure all the code to access
these micro-services. But the client will not know
the URL of microservice. If the client wants to test
the vehicle microservice, then the client will send a
request to the API Gateway. And API Gateway will
send a request to the vehicle microservice
and get the response. And right after
that, API Gateway will return that
responds to the client. In short, API Gateway
is used to protect your APIs from our
use and abuse. Because in API Gateway, we can also use rate-limiting. Whenever microservices are very complex and when
we want to scale, then API Gateway will play
a very important role. Load balancing is also very
perfect with API Gateway. In short, we can
see that there are multiple benefits of
using API Gateway. And in the next lesson, we'll configure the NuGet
package for using API Gateway. I'll see you in the next lesson.
44. Setup Api Gateway: In this course, we'll use
the ocelot API gateway. So before moving forward, let's go and get a new
doctrine called API project. Will name this project
to API Gateway. Alright, let's add this project. Now, let's do a few things
inside this project. We don't need the controller
and the model classes. So we'll remove the
controllers folder. And then we will also delete
the weather forecast class. Okay, now the project
is empty and it just contains the
app settings class and the program class, which we need to configure
the API Gateway. Now inside this project to add the NuGet
package for ocelot. We'll search for this
API gateway package, which is ocelot. Then let's install this package. Okay, here we go. The
package has been installed. Now we'll go to
the program glass and they will configure
this ocelot package. First we'll add
builder dot services. Then we will use to add
ocelot method here. For this ADD ocelot method, we need to resolve
the namespace, that is ocelot dot
dependency injection. Again, we'll use f dot. Then we will add use
ocelot method here, which will configure
the middleware. And for this purpose, we'll add this namespace that
is ocelot dot middleware. Alright, so these two lines are important to
configure the ocelot. Now, in the next lesson, we'll map the routes
for API gateway. So I'll see you in
the next lesson.
45. Configure Routes: In the last lesson, we've installed a NuGet
package for ocelot. And now in this lesson, we're going to add the route for API gateway will go to the
app settings dot json file. And we'll start over. I'm going to tell
you a few things. Let's take a look
at this picture. We have a client and API
Gateway and a legal service. The client will know
the URL of API gateway. So the client will send a
request to that API Gateway. And API Gateway will further send the request
to the microservice. This URL which is
exposed to the client, is an upstream URL. And the URL of the microservice
is the downstream URL. Okay, just remember this, upstream and downstream
because we'll use it heavily inside
the API Gateway. Alright, let's go back to the
app settings dot json file. And we'll add the routes. So let's use the routes. Then open the square brackets. And inside this will
add the curly brackets. Now let's use the
downstream but doublet. The downstream part
template is basically the endpoint of
the microservice. I know you're thinking that how to find the APA end point. Let's say we want to find the API endpoint for local
micro service project. So we'll run this
vehicle microservice. Here, instead of a browser, we'll see the end points
for these methods. This slash api slash vehicles is the endpoint of this method. And similarly, we have
endpoints of all the methods. Okay, let's start
with this get method, which will return the
list of vehicles. As we know that the end point
for this request is slash api slash vehicles in the downstream path
template whose use slash api slash vehicles. Then we'll add the
downstream scheme. And here we need to set the
HTTPS because we want to access the HTTPS protocol
right after that. Use the downstream
hosts and ports. And then let's add
the square brackets. And inside this will add
the host and port number. The host is local
host because we are running down legal microservice
inside the local host. So this will be the
host and port number. We need to pass the port where vehicle microservice is running. Let's check in which poor we're running the vehicle
microservice. So if we will run the
vehicle microservice project that we'll see the port number. Let's copy this port number. Right after that. We'll go to the API Gateway
app settings file. There. Let's base this port number. Alright, then place
a comma separator after the square bracket. And outside this will add
upstream bot template. As we discussed earlier, the upstream part template
will be that URL, which is exposed to the client, will expose this slash gateway slash vehicles to the client. Alright, then we'll add
the upstream HTTP method. And here we need to define
this upstream part of that will target how
many HTTP methods? Let's add the Gettier. If will go to the web browser. Then we'll see that both GET and post head same endpoints. So we will also add boost in
the upstream HTTP method. Alright, that's it.
Just like that. We need to add the route
for other methods. So we'll add a comma. Then instead of adding the code, I'll just copy this
part and paste it here. Let's say this time
we worked out good, Get, update and delete methods. And these methods have
the same API endpoint. In the downstream path
template will add slash, api slash, vehicles slash. And then we'll add the ID
in the curly brackets. Now, in the upstream part
template will also change the URL to slash
gateway slash vehicles. Right after that, we'll add slash id in the
curly brackets. Then in the abstract method, we want to tell how many action matters this
route will target. With this route will target, GET, PUT, and delete. And we'll add these
HTTP methods here. Alright. Now just like this, we need to add the routes
for other micro-services like customers and reservations. So again, let's copy this
code and paste it here. This time, we're going to target the customers
microservice. So what I'm gonna do is I'll just run the customer's
Microsoft Project. Will see the routes.
We just have one route which is slash
api slash customers. So we'll add it here
at the port number. So we'll copy the port number of customers micro
service project. And then paste it here. Right after that, less than the upstream bot template to slash gateway slash customers. And obviously this case plus microservice will talk
with the post method. So we'll add post STP
action method here. Then we'll copy this
code and paste it here. This time we're going to target the reservations Microservice. Again, what I'm gonna do is run that envisions
microservice project. And here we'll see
the APA end points. We have two different
routes here. So first, we'll use the code for this slash api
slash reservations. In the app settings file
will change as template to slash api slash reservations. Then we'll add the port
number here forever. Reservations
microservice project. Right after that will change the downstream part
template accordingly. Then this outlet
darker getMethod. So make sure to add the
action method here. Okay, Let's copy
this code for as a vicious route and
paste it once again. This time we'll change
the API endpoint according to this PUT method. So we will use slash, api slash reservations, and then we'll add the ID
in the curly brackets. Similarly, we'll add the ID and the downstream
part template. Then in the upstream
HTTP method will pass. But because we want to die Good put action method from
this allowed URL. Okay, here we go. We have completed the
code for API Gateway. Now, in the next lesson, we'll check the functionality. So I'll see you in
the next lesson.
46. Test Api Gateway: In the last lesson,
we have added a quote for API Gateway. And now in this lesson, we're going to desktop micro
services via API Gateway. Alright, now I'm to run
all the four projects. So we'll right-click
on the Solution, then set starter
projects and just make sure this multiple starter
projects is selected. Then we'll start all the
projects and hit Okay, let's run the projects.
And here we go. This will load all
the four projects. Now we're using the API gateway. That means we will
not use the vehicle, customer or reservations
project directly. We'll choose an API Gateway. Api Gateway project. Let's add API endpoint for
vehicle. And hit Enter. Here we go. We'll get w equals theta. Similarly, you can
check other end points. I want to tell you
that this API gateway doesn't contain the
documentation for APIs. So in order to check the
other end points like post, PUT and delete unit is
use the Postman tool. You've already covered
how to test our APIs via Postman inside
my last APA course. Okay, That's all
from this lesson. Now, I'll see you
in the next one.