<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michele</title>
    <description>The latest articles on DEV Community by Michele (@mikescandy).</description>
    <link>https://dev.to/mikescandy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1032012%2F8e951db4-3a20-473d-8121-2054a3666730.jpeg</url>
      <title>DEV Community: Michele</title>
      <link>https://dev.to/mikescandy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mikescandy"/>
    <language>en</language>
    <item>
      <title>Getting started with Microsoft Orleans</title>
      <dc:creator>Michele</dc:creator>
      <pubDate>Wed, 22 Feb 2023 23:10:06 +0000</pubDate>
      <link>https://dev.to/mikescandy/getting-started-with-microsoft-orleans-fij</link>
      <guid>https://dev.to/mikescandy/getting-started-with-microsoft-orleans-fij</guid>
      <description>&lt;p&gt;I'm not going to spend time to introduce the Microsoft Orleans framework here, as the &lt;a href="https://learn.microsoft.com/en-us/dotnet/orleans/overview" rel="noopener noreferrer"&gt;MSDN documentation&lt;/a&gt; does a pretty good job at that.&lt;/p&gt;

&lt;p&gt;The building blocks of an application using Orleans are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The silo&lt;/li&gt;
&lt;li&gt;The client&lt;/li&gt;
&lt;li&gt;The grains interfaces&lt;/li&gt;
&lt;li&gt;The grains implementations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a production environment these blocks usually live in separate projects, but we don't need to do that to start the simplest possible silo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step one
&lt;/h3&gt;

&lt;p&gt;Create a new web application. This will host the silo and also act as the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new web -o GettingStartedOrleans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step two
&lt;/h3&gt;

&lt;p&gt;Inside the GettingStartedOrleans folder, the program.cs file should look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () =&amp;gt; "Hello World!");

app.Run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Orleans 7 provides three main nuget packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Microsoft.Orleans.Server&lt;/li&gt;
&lt;li&gt;Microsoft.Orleans.Client&lt;/li&gt;
&lt;li&gt;Microsoft.Orleans.Sdk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When building a silo, we only need to reference Server, which in turn will reference Client and Sdk. This means that on a silo, we always have a client available.&lt;/p&gt;

&lt;p&gt;When building an application that connects to an Orleans silo, we only need to reference Client.&lt;/p&gt;

&lt;p&gt;Finally, when building other libraries that contain Orleans related code (e.g. Grains and Grain Interfaces), we should just reference SDK.&lt;/p&gt;

&lt;p&gt;In our case, we'll add Microsoft.Orleans.Server to our project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package Microsoft.Orleans.Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step three
&lt;/h3&gt;

&lt;p&gt;Open the program.cs file and add this line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Host.UseOrleans(silobuilder=&amp;gt;silobuilder.UseLocalhostClustering());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;before&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var app = builder.Build();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the application now, we'll have a web application and an Orleans silo running side by side, with the web application already connected to the silo.&lt;br&gt;
The Orleans SDK automatically registers an instance of IClientService and connects to the Orleans silo, so that when we write our code, all we will need to do is to inject the IClientService and use.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step four
&lt;/h3&gt;

&lt;p&gt;At this point we can create our first grain. To do that we first write the grain interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface IPingGrain : IGrainWithGuidKey
{
  ValueTask&amp;lt;string&amp;gt; Pong(string name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our Ping grain interface inherits from IGrainWithGuidKey. The Guid will be used to identify a unique grain.&lt;/p&gt;

&lt;p&gt;The implementation will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public sealed class PingGrain : IPingGrain
{
    public ValueTask&amp;lt;string&amp;gt; Pong(string name) =&amp;gt; ValueTask.FromResult(name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step five
&lt;/h3&gt;

&lt;p&gt;All is left now is to call the grain from our api.&lt;br&gt;
We can modify the existing route with something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.MapGet("/hello/{name}", async (string name, IClusterClient clusterClient) =&amp;gt;
    await clusterClient.GetGrain&amp;lt;IPingGrain&amp;gt;(Guid.NewGuid()).Pong(name));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;anject IClusterClient in the api call. It's been automatically registered and initialized by the framework&lt;/li&gt;
&lt;li&gt;simply await the Pong method from the IPingGrain interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusions
&lt;/h3&gt;

&lt;p&gt;We can start the project with a simple&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling our Api with (change the port if different)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl http://localhost:5000/hello/michele
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will return&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;michele
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sample demonstrates how quickly we can start a web application and an Orleans silo in a development environment. This will allow us to quickly experiment with the framework and understand the fundamental building blocks.&lt;/p&gt;

&lt;p&gt;In the next post we'll introduce request and response objects, which will bring us in the magic world of serialization!&lt;/p&gt;




&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@alinnnaaaa?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Alina Grubnyak&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/ZiQkhI7417A?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
