As part of the introduction to this chapter, we looked at the difference between stateful and stateless microservices. The Service Fabric application templates available are then further divided into Reliable Services (stateful/stateless) and Reliable Actors (stateful/stateless). When to use which one is something that will depend on the specific business requirement of your application.
To put it simply though, if you wanted to create a service that should be exposed to many users of your application at any one time, a Reliable Service would probably be a good fit. Think of a service exposing the latest exchange rates that can be consumed by many users or applications at once.
Again, looking back to the introduction of this chapter, we used the example of an online web store with a shopping cart. A Reliable Actor could be a good fit for every customer buying items, so you could have a shopping cart actor. The Reliable Actor as part of the Service Fabric framework is based on the Virtual Actor pattern. Have a look at the article on the Virtual Actor pattern at http://research.microsoft.com/en-us/projects/orleans/.
To show you how easy it is to create a microservice using a stateless actor service as an example, we will use Visual Studio to publish a service to the Service Fabric cluster and call that service from a console (client) application.
To complete this recipe, you must ensure that you have installed your local Service Fabric cluster on your local machine.
sfApp
, and click on OK:UtilitiesActor
:sfApp
UtilitiesActor
UtilitiesActor.Interfaces
IUtilitiesActor
interface. This interface will simply require that UtilitiesActor
implements a method called ValidateEmailAsync
that takes an e-mail address as a parameter and returns a Boolean value indicating whether it is a valid email address or not:namespace UtilitiesActor.Interfaces { public interface IUtilitiesActor : IActor { Task<bool> ValidateEmailAsync(string emailToValidate); } }
UtilitiesActor
project and view the class. It will be underlined with a red squiggly line because it does not implement the interface member ValidateEmailAsync()
:NotImplementedException
. It is here that we will implement the code to validate the e-mail address:namespace UtilitiesActor { internal class UtilitiesActor : StatelessActor, IUtilitiesActor { public Task<bool> ValidateEmailAsync(string emailToValidate) { throw new NotImplementedException(); } } }
Finally, you will notice the ActorEventSource
code. This is simply just to create
Event Tracing for Windows (ETW) events that will help you see what is happening in your application from the diagnostic events window in Visual Studio. To open the diagnostic events window, go to View, Other Windows and click on Diagnostic Events Viewer:
internal class UtilitiesActor : StatelessActor, IUtilitiesActor { public async Task<bool> ValidateEmailAsync(string emailToValidate) { ActorEventSource.Current.ActorMessage(this, "Email Validation"); return await Task.FromResult(Regex.IsMatch(emailToValidate, @"A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0- 9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9- ]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0- 9])?)", RegexOptions.IgnoreCase)); } }
System.Text.RegularExpressions
namespace. Without it, you will not be able to use the regular expressions. If you added the regular expression in your code without adding the reference, Visual Studio will display a red squiggly line under the Regex
method:using
statement to your project. This will bring the regular expression namespace into scope:sfApp.Client
and click on the OK button:sfApp.Client
project and select Add Reference from the context menu:UtilitiesActor.Interfaces
project:packages
to your project folder structure. Browse to this folder and add your Service Fabric DLLs from there. After you have added the required DLLs, your project should look like this:Program.cs
file of your console application, you need to add the following code to the Main
method:namespace sfApp.Client { class Program { static void Main(string[] args) { var actProxy = ActorProxy.Create<IUtilitiesActor> (ActorId.NewId(), "fabric:/sfApp"); WriteLine("Utilities Actor {0} - Valid Email?: {1}", actProxy.GetActorId(), actProxy.ValidateEmailAsync ("[email protected]").Result); WriteLine("Utilities Actor {0} - Valid Email?: {1}", actProxy.GetActorId(), actProxy.ValidateEmailAsync ("invalid@[email protected]").Result); ReadLine(); } } }
All we are doing is creating a proxy for our actor and writing the output of the e-mail validation to the console window. Your client application is now ready.
sfApp
service and click on Publish from the context menu:Local.xml
. When you are done, click on the Publish button:http://localhost:19080/Explorer
, you will notice that the service you created has been published to your local Service Fabric cluster:sfApp.Client
project, and select Debug and Start new instance from the context menu:ActorId
method, and give it any string value:var actProxy = ActorProxy.Create<IUtilitiesActor>(new ActorId("Utilities"), "fabric:/sfApp"); WriteLine("Utilities Actor {0} - Valid Email?: {1}", actProxy.GetActorId(), actProxy.ValidateEmailAsync("[email protected]").Result) ; WriteLine("Utilities Actor {0} - Valid Email?: {1}", actProxy.GetActorId(), actProxy.ValidateEmailAsync("invalid@[email protected]").Resu lt); ReadLine();
Utilities Actor
now has a logical name (the same name you passed as a string value when creating a new ActorId
method):Creating your Service Fabric application and publishing it locally is a perfect solution for testing your application before publishing it to the cloud. Creating small independent microservices allows developers many benefits related to testing, debugging, and deploying efficient and robust code that your applications can leverage to ensure maximum availability.
3.141.30.210