Introduction to Microsoft Orleans - CodeWithStu

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is chu if you're new here this channel is all about me breaking down different technologies into easily digestible chunks in this video i'm going to give you an overview of the microsoft orleans framework including a demo of how to get started so let's take a look at some of the questions that we're going to be answering today firstly we're going to be taking a brief look at the virtual actor model so that we can better understand microsoft orleans next we're going to take a look at the concept of a grain and some of its features including the life cycle of a grain then we're going to take a look at defining a silo and some of its responsibilities before going into a demo and taking a look at this all in action to understand microsoft orleans as a framework we must first understand the virtual actor model within the virtual actor model actors are unit of isolation in the context of a process or cluster typically actors implement a q-like system to process requests one at a time this allows developers to maintain certain guarantees with their applications around application state they can be implemented either as a unit of work pattern or based on something like a domain aggregate the virtual actor model is most applicable for use cases where the clustering and logical sharding are primary concerns of the domain an example of this could be maintaining information about the player in a large scale game now we have a basic understanding of the virtual actor model let's take a look at how it's implemented in microsoft or lanes grains are microsoft orleans version of actors they typically have two parts associated with them an interface and a corresponding implementation the implementation must inherit from the type green and be registered in di the identity of the grain is deterministic in nature and can be either a number a good or a simple string depending on the application's requirements we'll see how to manage this identity a bit later on when it comes to the grain state you can have a choice to have as a volatile entity or have the state persisted into a data store of your choosing and after a period of inactivity or under heavier load the grain may be deactivated to free up machine resources each grain has three distinct life cycle phases the first phase is the activation phase this is where the grain is requested by the application instantiated from di and designed its identity the second phase is when the grain is actually running the application can call the grain one or multiple times depending on the context of the application after each call the crane may also assist its state to the data store lastly we have the deactivation phase as mentioned previously this may be due to application load or a period of inactivity or could even be deactivated from within the grain itself a grain may also persist state at this stage within microsoft orleans a grain is activated by a silo a silo is a host of one or more grains service workers and other components in the microsoft audience framework a silo is responsible for the activation and deactivation of grains across a cluster including which grains are activated on which silo typically there is a one-to-one mapping between a silo and a container or compute instance although there can be more depending on the infrastructure setup microsoft have design sites that can be easily integrated alongside other components such as asp.net core through the use of the generic hosting model by adding more silos to the system through clustering we can achieve great levels of fault tolerance and scalability for our applications now we've taken a look at what is the virtual actor model a green and a silo it is now time for us to dive into the code and take a look at how to set up microsoft orleans now that we've taken a look at some of the concepts behind microsoft or lanes let's go and take a look at how we can integrate those with an asp.net core application here i have an empty solution with nothing in it so far so i can show you everything from the start so i'm going to right click on the solution inside the solution explorer and add a new project by going to add a new project i'm going to ensure that i have asp.net core web application selected which can also be found in the menu on the right hand side and select next in the project name i'm going to give it a simple hello orleans name then click create i'm going to ensure that this is an asp.net core 3.1 application and i have the api selected in the main window for the purposes of this demo i'm going to turn off https because i'm not going to configure that here today lastly i'm just going to click create once this is done you will see the default setup where we have a weather forecast controller program and a starter i'm going to right click on the project and manage its new get packages to install some stuff from microsoft orleans in the browse tab i'm going to search for microsoft orleans in here there are three packages that i want to integrate firstly i want the microsoft.orleans.client this makes sure that we have all the necessary bits and pieces for using a client in our application i'm going to select microsoft.alliance.cognito.ms build which basically allows us to have a proxy between a client and a server and lastly i'm going to install the microsoft orleans.server package in a clustering scenario it is very important that we have a transparent proxy which microsoft orleans provides between the client and the server and that's what this microsoft all instant code generator package does for us this is all the packages that we're going to need for now so let's close it down the first thing that i'm going to do is set up the server side by going to program.cs finding the default host builder that it's created for me and appending that dot use orleans infrastructure i'm going to pass in a builder lambda function and inside i'm going to do builder.use localhost clustering this is the only bit that's required for our servers in this demo occasionally you may not get the method that's available so to fix this we're going to go to the top of the usings and add using orleans.hosting which resolves the error next we're going to build our grain so i'm going to add a new class to the project by right clicking on it clicking add and then new class and i'm going to call this hello world green initially i'm going to add a class to inherit from the type grain which is in the orleans namespace i also need a way to make sure that the grain can be communicated with across a cluster even though the cluster is going to be local for now and to do this i'm going to back the grain with an interface which is a required step so we're going to declare a public interface i hello world grain and we also need to give this some form of identity so we're going to inherit from eye grain with string key here you'll notice you can have the other types of identity that i mentioned previously such as an integer key a guide key or some form of compound key for today i'm going to stick with a string key and lastly write in the method that i wish to invoke inside of the cluster and this is just going to be a simple say hello2 method that takes a string of name now the last bit of hookup for the interface is to say i'm also going to inherit from this over here say i hello world grain and implement the interface i'm just going to keep track of the number of times that we've had this invoked so i'm going to create a new invocation account and lastly i'm going to return a string from here and say hello name which is the name we're going to get from our controller in a bit from this your primary keying string and what was this get primary key string is going to be the identity of the grain that we have passed in and because we know that eye hello our grain inherits from eye grain with string key we know that it's going to be safe to get the primary key as a string then we're going to say i've said hello and then we're going to grab the invocation count making sure it's incremented after we get it they said hello invocation amount times now to make sure that orleans knows about this we need to make sure that this is in di so i'm now going to go to the startup have a look for the configure services method and make sure that is bound in transient scope so we're going to go services.and transient pass in the interface which is i hello world grain and the concrete type of hello wild grain service types such as singletons will not work because it is assumed that each grain can be instantiated more than once so therefore it needs to be isolated so please make sure that you're using the transient scope here the last bit for us to do is set up our controller method so i'm going to create a new class call it hello world controller and make sure that this inherits from controller so that we get some helper methods here now let's create a new constructor by typing the cu tool keyword and pressing tab which expands this out and taking in an i cluster client and this lives inside the orleans namespace and this is our gateway to be able to access a cluster from our api next we need to create a new method so we're going to have an async task of i action result and we're just going to call it hello for now and we're going to take a string name now we're going to have the result equal to the cluster client.getgreen and the green that this is the green type that we want in this tea so i want the i hello world grain because we always reference the grains by the interface not the implementation and then we need to pass it in an identity and because we know that the identity is a string i'm just going to pass in stew and then i'm going to add the say hello to async because this is the method that i want to invoke and the get grain returns me an instance of the i hello world grain and here i'm just going to simply pass through the name that has passed to me just need to make sure that i await this and the two more bits that we need to do is just really make sure that we return okay just pass in the result and ensure that we have a route that's assigned to this so i'm going to say the pro is always going to be hello and then i'm just going to pass in the name as a template to make sure everything's banned up correctly all being well now i'm going to switch this over to the hello orleans on my execution task i'm going to hit ctrl f5 to run debugging or run without debugging and you should see a console that appears that gives you information about all links starting up i've got a window open already which is the localhost 5000 which is the default and i'm going to pass through the path hello john and now we can see the result from our grain which is hello john from stu i said hello zero times if i refresh this a few times you can see that the invocation count is going to be increasing to learn more about microsoft orleans including how to integrate diagnostic tooling click on the playlist that's on screen now if you like this video click the like button below and don't forget to subscribe to get notifications for when new videos are available i'll see you in the next video
Info
Channel: CodeWithStu
Views: 4,706
Rating: 4.8545456 out of 5
Keywords: actor model, aws dynamodb, dynamodb, microsoft orleans, microsoft orleans 3.0, microsoft orleans dynamodb, microsoft orleans explained, microsoft orleans tutorial, orleans, orleans 3.0, orleans dynamodb, orleans tutorial, virtual actor, virtual actors, .net, .net 5, .net core, .net core 3.1, .net core microservices, .net core tutorial, .net tutorial, c#, codewithstu, im5tu, aws, microsoft orleans use cases, microsoft orleans persistence, microsoft orleans framework
Id: yM-gpuw1uhM
Channel Id: undefined
Length: 14min 44sec (884 seconds)
Published: Sun Nov 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.