DEV Community

Derek Comartin - CodeOpinion
Derek Comartin - CodeOpinion

Posted on • Originally published at codeopinion.com on

1 1

Co-Hosting Orleans and ASP.NET Core

Orleans and ASP.NET Core

With the release of Orleans 3.0 comes the ability to co-host with ASP.NET Core (or any other framework that uses the generic host builder).

What this means is you can run Orleans and ASP.NET Core in the same process.

The advantage to this is both services will share the same service provider, logging, etc that is configured with the host builder.

Orleans and ASP.NET Core

The extension method UseOrleans() is available now on the IHostBuilder. Just like you would configure the ASP.NET Core via ConfigureWebHostDefaults , you can configure the Orleans silo.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Orleans.Hosting;
namespace Orleans.AspNetCore.Demo
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseOrleans(siloBuilder =>
{
siloBuilder.UseLocalhostClustering();
})
.ConfigureLogging(logging =>
{
logging.AddConsole();
});
}
}
view raw Program.cs hosted with ❤ by GitHub

Benefits

One of the nice benefits here is that because both services share service registrations, an ASP.NET Core MVC controller can have the IClusterClient injected into it.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Orleans.AspNetCore.Demo.Grains;
namespace Orleans.AspNetCore.Demo.Controllers
{
public class HomeController : Controller
{
private readonly IClusterClient _clusterClient;
public HomeController(IClusterClient clusterClient)
{
_clusterClient = clusterClient;
}
public async Task<IActionResult> Index()
{
var orderNumberGenerator = _clusterClient.GetGrain<IOrderNumberGenerator>("Demo");
var orderNumber = await orderNumberGenerator.GenerateOrderNumber();
return View(orderNumber);
}
}
}

Also since logging is configured it is shared between both ASP.NET Core and Orleans. When I run my demo application, you can see both services in my console logs.

Health Checks

Another benefit of co-hosting Orleans and ASP.NET Core is you can provide a frontend via ASP.NET Core to expose status of your Orleans silo. I’ve written about the ASP.NET Core Health Checks over on the Telerik Blog.

If you’re running in something Docker, AWS ECS or Kubernetes, this would provide you information to determine the health/liveness of your services

Orleans 3.0

If you want more info on the 3.0 release, check out the Announcement for all the details.

Also, check out other related posts:

Follow @codeopinion

The post Co-Hosting Orleans and ASP.NET Core appeared first on CodeOpinion.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay