gettingstartedakka

CSharp · Console

/Program.cs

using System;
using Akka;
using Akka.Actor;
using Akka.Configuration;
using System.Threading.Tasks;
public class Program
{
	//Create an (immutable) message type that your actor will respond to
	//C# might get case types in C#6
	public class Greet
	{
		public Greet(string who)
		{
			Who = who;
		}
		public string Who { get;private set; }
	}
	
	//Create the actor class
	//This is the new lambda actor api that replaces UntypedActor
	//Atleast untill we get case types and pattern matching
	public class GreetingActor : ReceiveActor
	{
		public GreetingActor()
		{
			Receive<Greet>(greet => Console.WriteLine("Hello {0}", greet.Who));
		}
	}
	
	public static void Main()
	{
		//create a new actor system (a container for your actors)
		var system = ActorSystem.Create("MySystem",GetConfig());
		
		//create your actor and get a reference to it.
		//this will be an "ActorRef", which is not a reference to the actual actor instance
		//but rather a client or proxy to it
		var greeter = system.ActorOf(Props.Create<GreetingActor>(),"greeter");
		
		//send a message to the actor
		greeter.Tell(new Greet("Akka.NET"));
		
		//hack to make .NET Fiddle wait 
		Task.Delay(2000).Wait();
	}
	
	//What is this?
	//We need to reconfig Akka.NET to work under medium trust for .NET Fiddle
	//The default dispatcher uses ThreadPool.UnsafeQueue, reconfigure
	//to use a safer dispatcher
	private static Config GetConfig()
	{
		return ConfigurationFactory.ParseString(@"
					akka.actor.default-dispatcher {
							type = ""Akka.Dispatch.TaskDispatcher""
							throughput = 30
						}");
	}
	
}
;