Metaparticle.Runtime
0.1.3-beta
See the version list below for details.
dotnet add package Metaparticle.Runtime --version 0.1.3-beta
NuGet\Install-Package Metaparticle.Runtime -Version 0.1.3-beta
<PackageReference Include="Metaparticle.Runtime" Version="0.1.3-beta" />
paket add Metaparticle.Runtime --version 0.1.3-beta
#r "nuget: Metaparticle.Runtime, 0.1.3-beta"
// 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 Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.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,320 | 1/11/2018 |
0.1.3-beta | 995 | 12/12/2017 |
0.1.0-beta | 947 | 11/25/2017 |