Metaparticle.Runtime 0.1.3-beta

.NET Standard 2.0
This is a prerelease version of Metaparticle.Runtime.
There is a newer prerelease version of this package available.
See the version list below for details.
NuGet\Install-Package Metaparticle.Runtime -Version 0.1.3-beta
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package Metaparticle.Runtime --version 0.1.3-beta
<PackageReference Include="Metaparticle.Runtime" Version="0.1.3-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Metaparticle.Runtime --version 0.1.3-beta
#r "nuget: Metaparticle.Runtime, 0.1.3-beta"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Metaparticle.Runtime as a Cake Addin
#addin nuget:?package=Metaparticle.Runtime&version=0.1.3-beta&prerelease

// Install Metaparticle.Runtime as a Cake Tool
#tool nuget:?package=Metaparticle.Runtime&version=0.1.3-beta&prerelease

Metaparticle/Package for .NET Core Tutorial

This is an in-depth tutorial for using Metaparticle/Package for .NET Core.

For a quick summary, please see the README.

Initial Setup

Check the tools

The docker command line tool needs to be installed and working. Try: docker ps to verify this. Go to the install page if you need to install Docker.

The mp-compiler command line tool needs to be installed and working. Try mp-compiler --help to verify this. Go to the releases page if you need to install the Metaparticle compiler.

Get the code

$ git clone https://github.com/metaparticle-io/package
$ cd tutorials/dotnet/
# [optional, substitute your favorite editor here...]
$ code .

Initial Program

Inside of the tutorials/dotnet directory, you will find a simple .NET Core project.

You can build this project with dotnet build.

The initial code is a very simple "Hello World"

using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace web
{
    public class Program
    {
        const int port = 8080;
        public static void Main(string[] args)
       	{
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseKestrel(options => { options.Listen(IPAddress.Any, port); })
                .Build()
                .Run();
    	}
    }
}

Step One: Containerize the Application

To build a container from our simple application we need to add a dependency to our build file, and then update the code.

Run:

dotnet add package Metaparticle.Package --version="0.1.2-beta"
dotnet add package Metaparticle.Runtime --version="0.1.0-beta"

Then update the code to read as follows:

using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using static Metaparticle.Package.Driver;

namespace web
{
    public class Program
    {
        const int port = 8080;
        [Metaparticle.Runtime.Config]
        [Metaparticle.Package.Config(Repository = "docker.io/docker-user-goes-here/simple-web", Publish = false)]
        public static void Main(string[] args) => Containerize(args, () =>
       	{
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
				.UseKestrel(options => { options.Listen(IPAddress.Any, port); })
                .Build()
                .Run();
    	});
    }
}

You will notice that we added a Metaparticle.Package.Config annotation that describes how to package the application. You will need to replace your-docker-user-goes-here with an actual Docker repository path.

You will also notice that we wrapped the main function in the Containerize function which kicks off the Metaparticle code.

There is also the Metaparticle.Runtime.Config annotation. This is what actually runs your application. There will be more on that later.

You can run this new program with:

dotnet run

This code will start your web server again. But this time, it is running inside a container. You can see this by running:

docker ps

Step Two: Exposing the ports

If you try to access the web server on http://localhost:8080 you will see that you can not actually access the server. Despite it running, the service is not exposed. To do this, you need to add a [Metaparticle.Runtime.Config ...] annotation to supply the port(s) to expose.

The code snippet to add is:

...
    [Metaparticle.Runtime.Config(Ports = int[] {8080})]
...

This tells the runtime the port(s) to expose. The complete code looks like:

using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using static Metaparticle.Package.Driver;


namespace web
{
    public class Program
    {
        const int port = 8080;
        [Metaparticle.Runtime.Config(Ports = new int[] {port})]
        [Metaparticle.Package.Config(Repository = "docker.io/your-docker-user-name-here/simple-web",
                                     Publish = true,
                                     Verbose = true)]
        public static void Main(string[] args) => Containerize(args, () =>
       	{
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
				.UseKestrel(options => { options.Listen(IPAddress.Any, port); })
                .Build()
                .Run();
    	});
    }
}

Now if you run this with dotnet run your webserver will be successfully exposed on port 8080.

Replicating and exposing on the web.

As a final step, consider the task of exposing a replicated service on the internet. To do this, we're going to expand our usage of the Metaparticle.Runtime.Config tag. First we will add a replicas field, which will specify the number of replicas. Second we will set our execution environment to metaparticle which will launch the service into the currently configured Kubernetes environment.

Here's what the snippet looks like:

...
    [Metaparticle.Runtime.Config(Ports = new int[] {port}, Executor = "metaparticle", Replicas = 4)]
...

And the complete code looks like:

using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using static Metaparticle.Package.Driver;


namespace web
{
    public class Program
    {
        const int port = 8080;
        [Metaparticle.Runtime.Config(Ports = new int[] {port}, Executor = "metaparticle", Replicas = 4)]
        [Metaparticle.Package.Config(Repository = "docker.io/your-docker-user-name-here/simple-web", Publish = true, Verbose = true)]
        public static void Main(string[] args) => Containerize(args, () =>
       	{
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
				.UseKestrel(options => { options.Listen(IPAddress.Any, port); })
                .Build()
                .Run();
    	});
    }
}

You can run this using:

dotnet run

After you compile and run this, you can see that there are four replicas running behind a Kubernetes Service Load balancer:

$ kubectl get pods
...
$ kubectl get services
...

Still looking for more? Continue on to the more advanced sharding tutorial

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Metaparticle.Runtime:

Package Downloads
Metaparticle.Package

Package library for language idiomatic containeriziation

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.4-beta 1,042 1/11/2018
0.1.3-beta 858 12/12/2017
0.1.0-beta 802 11/25/2017