• Stars
    star
    105
  • Rank 317,272 (Top 7 %)
  • Language
    C#
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An HTTP API for Microsoft Orleans

Orleans.Http

Orleans.Http

Orleans HTTP Endpoints

Build Package Version NuGet Downloads Gitter

Orleans is a framework that provides a straight-forward approach to building distributed high-scale computing applications, without the need to learn and apply complex concurrency or other scaling patterns.

Orleans.Http is a package that use ASP.Net Core as a frontend endpoint for Orleans grains without the need of controller classes.

This package leverages ASP.NET Core Endpoint Routing to expose grains as HTTP endpoints without need of have to implement Controllers. The request is received and processed by grains methods itself. The idea is to perform in a similar way to Controllers without having to add any boilerplate code to your Orleans project.

At the silo startup, the package look for usage of [Route] and [HttpXXX] attributes on grain interface methods and register a route on ASP.NET Core endpoints route table. When a request arrive to that route, the parameters are extracted from the request, mapped to grain method parameters, and the grain is invoked. If the grain return Task<T>, the returning object is serialized back to the caller using one of the registered IMediaTypeHandler and added to the Response body.

Installation

Installation is performed via NuGet

On your Silo Project

From Package Manager:

PS> Install-Package Orleans.Http -prerelease

.Net CLI:

# dotnet add package Orleans.Http -prerelease

Paket:

# paket add Orleans.Http -prerelease

On your Grain Interface Project(s)

From Package Manager:

PS> Install-Package Orleans.Http.Abstractions -prerelease

.Net CLI:

# dotnet add package Orleans.Http.Abstractions -prerelease

Paket:

# paket add Orleans.Http.Abstractions -prerelease

Serialization and parameter mapping

The serialization of both request and response body is handled by implementations of IMediaTypeHandler. The runtime checks the Content-Type header of the request and lookup for a IMediaTypeHandler registered implementation that can deserialize the body with that partticular media type. If one matches, the deserialization happens and the deserialized object is passed to the parameter of the grain method being invoked that is marked with the [FromBody] attribute. If there is no serializer registered for that Content-Type, it will just set the parameter as null.

Whenever the method returns Task<T>, the runtime will check if there is an Accept header and use it to lookup for a IMediaTypeHandler that will then serialize the response back to the caller in the Response body. If there is no Accept header, it will then try to serialize the response using the Content-Type header instead.

Parameters can be also mapped from both the Route and the Query string by using [FromRoute] and [FromQuery] parameter attributes respectively. Those attributes works only for primitive types.

By default, Json (using System.Text.Json), XML and Forms are provided with Orleans.Http package. Protobuf (protobuf-net) is also available by adding the Orleans.Http.MediaTypes.Protobuf package.

Custom Serializers

If you want to implement your own IMediaTypeHandler to handle a new (or existig) media type, just implement and add an instance of the following interface to the DI context and it will be invoked by the runtime:

public interface IMediaTypeHandler
{
    string MediaType { get; } // This should be a unique media type i.e. application/mystuff
    ValueTask Serialize(object obj, PipeWriter writer);
    ValueTask<object> Deserialize(PipeReader reader, Type type, CancellationToken cancellationToken);
}

Authentication / Authorization

In a similar way to ASP.NET Core Controllers, this package also allow the developer to leverage ASP.NET Core Authentication/Authorization middleware. Just add [Authorize] attribute to the grain method interface and it will just work the same way. Make sure to configure your ASP.NET Core Authentication/Authorization middleware.

Routes and Attributes

The routes are generated in a similar way as ASP.NET Core Attribute Routing. By default, no route is generated and all grains are considered private.

The Pattern property of the attributes define the route to be used. If the Pattern property is ommited, the default route is used as the following pattern:

{optionalPrefix}/{optionalTopLevelPattern}/{grainTypeName}/{grainId}/{methodName}

By default, if an attribute Pattern property is set, it is required to somewhere into the pattern to add the {grainId} string otherwise the route will fail to be registered. Optionally, you can also add {grainIdExtension} in case of grains that have Compound keys.

The following attributes are under the Orleans.Http.Abstractions package:

Note: All routes described by the attribute takes into consideration the optional prefix set on yor Startup class when you call MapGrains("prefix").

[Route]

This attribute can be used on both interface and methods.

When applied to a Grain Interface, this attribute Pattern property value is added as the first node of the grain route.

When added to a Grain Method, this attribute tells the runtime to route ALL HTTP verbs to that particular Pattern for that method.

[HttpXXX]

This attribute can be applied only to methods.

The value of Patttern property is used to generate the route of that method for a particular HTTP Verb. If the patttern starts with /, it will ignore all the prefixes and will be used as the de-facto route for that method.

routeGrainProviderPolicy optional argument

This argument allows you to set the route grain provider used for the speicified endpoint. This allows you to override the default behaviour and provide an IGrain that the request will go to. To register a route grain provider policy, use the extension method UseRouteGrainProviders(Action<IRouteGrainProviderPolcyBuilder>) on IApplicationBuilder In your Startup class. The default RouteGrainProvider policy may also be set there. Registering the policy takes in a generic type argument, this type must implement IRouteGrainProvider, optionally, if your implementing type is registered with dependency injection, it will use that type's lifetime, otherwise it will have a scoped lifetime by default.

Example usage

Using the .Net Core Generic Host configure Orleans the same way you would and add the ASP.NET Core to it:

    var hostBuilder = new HostBuilder();
    hostBuilder.UseConsoleLifetime();
    hostBuilder.ConfigureLogging(logging => logging.AddConsole());
    hostBuilder.ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseUrls("http://*:9090");
        webBuilder.UseStartup<Startup>();
    });

    hostBuilder.UseOrleans(b =>
    {
        b.UseLocalhostClustering();

        b.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(Startup).Assembly));
    });

    var host = hostBuilder.Build();
    host.Start();

On your Startup class:

public class Startup
{
    public const string SECRET = "THIS IS OUR AWESOME SUPER SECRET!!!";
    public void ConfigureServices(IServiceCollection services)
    {
        // Inject IHttpContextAccessor on your grain to get access to the HttpContext
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        // Add the authentication services if you want to use it. 
        // For this example, we're using a simple Jwt
        services.AddAuthentication(opt =>
        {
            opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(opt =>
        {
            opt.RequireHttpsMetadata = false;
            opt.SaveToken = true;
            opt.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SECRET)),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

        services.AddAuthorization();

        // Add the GrainRouter service and any IMediaTypeHandler instances you want
        services
            .AddGrainRouter()
            .AddJsonMediaType()
            .AddProtobufMediaType();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // Enable ASP.NET Core Endpoint Routing
        app.UseRouting();

        // Enable Authentication
        app.UseAuthentication();

        // Enable Authorization
        app.UseAuthorization();

        // Configure ASP.NET Core endpoints
        app.UseEndpoints(endpoints =>
        {
            // Call MapGrains([prefix]) with an optional prefix for the routes.
            endpoints.MapGrains("grains");

            // You can add any other endpoints here like regular ASP.NET Controller, SignalR, you name it. 
            // Orleans endpoints are agnostic to other routes.
        });

        //Optionally register route grain provider policies
        app.UseRouteGrainProviders(routeGrainProviders =>
        {
            routeGrainProviders.RegisterRouteGrainProvider<YourCustomRouteGrainProvider>("SomePolicy");

            //You may also optionally set the defaulty policy, so it it used by every endpoint without a specified policy
            routeGrainProviders.SetDefaultRouteGrainProviderPolicy("SomePolicy");
        });
    }
}

Contributions

PRs and feedback are very welcome!

More Repositories

1

OrleansDashboard

πŸ“Š A developer dashboard for Microsoft Orleans
C#
686
star
2

Orleankka

Functional API for Microsoft Orleans http://orleanscontrib.github.io/Orleankka
C#
486
star
3

SignalR.Orleans

SignalR backend based on Orleans.
C#
286
star
4

DesignPatterns

πŸ“ Design patterns for Project Orleans
243
star
5

Orleans.Clustering.Kubernetes

Orleans Membership provider for Kubernetes
C#
182
star
6

Orleans.Sagas

A distributed saga implementation for Orleans
C#
149
star
7

Awesome-Orleans

😎 Awesome Microsoft Orleans Projects
130
star
8

Orleans.Providers.MongoDB

A MongoDb implementation of the Orleans Providers: Membership, Storage and Reminders.
C#
98
star
9

Orleans.Activities

Workflow Foundation (.Net 4.x System.Activities workflows) over Microsoft Orleans framework, providing stable, long-running, extremely scalable processes with XAML designer support.
C#
83
star
10

meetups

πŸ“† A repository to organise virtual meetups to discuss Orleans and other distributed systems programming on .NET
79
star
11

OrleansTestKit

Unit Test Toolkit for Microsoft Orleans
C#
74
star
12

Orleans.Redis

Redis support packages for Orleans
C#
68
star
13

OrleansFabricSilo

Orleans running on Service Fabric
C#
59
star
14

Orleans.SyncWork

This package's intention is to expose an abstract base class to allow https://github.com/dotnet/orleans/ to work with long running CPU bound synchronous work, without becoming overloaded.
C#
54
star
15

Orleans.Providers.EntityFramework

An Entity Framework Core implementation of Orleans Grain Storage. More providers to come later.
C#
50
star
16

Orleans.HttpGateway.AspNetCore

C#
49
star
17

Orleans.Indexing

C#
46
star
18

Orleans.StorageProvider.Redis

A Redis implementation of the Orleans Storage Provider model. Uses the Azure Redis Cache to persist grain states.
C#
42
star
19

Orleans.Consensus

Raft implemented on Orleans
C#
41
star
20

Orleans.CosmosDB

Orleans providers for Azure Cosmos DB
C#
40
star
21

OrleansTemplates

C#
37
star
22

Announcements

πŸ“’ Orleans related public announcements - watch this repo to stay up-to-date
36
star
23

Orleans.Providers.RabbitMQ

Orleans providers for RabbitMQ.
C#
32
star
24

Orleans.MultiClient

A library to simplify access to multiple Orleans Clusters from a single point.
C#
30
star
25

Orleans.EventSourcing.Snapshot

Snapshot storage provider for orleans event sourcing
C#
30
star
26

OrleansMonitor

Use this library to monitor the Silos in your Orleans Deployment
C#
29
star
27

Orleans.Indexing-1.5

C#
27
star
28

Orleans.Containers

Distributed storage and processing of objects in Orleans
C#
26
star
29

orleans.eventsourcing

Event Sourcing support for Microsoft Orleans
C#
20
star
30

OrleansBlobStorageProvider

DEPRECATED - now part of the orleans codebase :shipit:
C#
13
star
31

Orleans.StorageProviders.SimpleSQLServerStorage

Ultra low friction Orleans Storage Provider using SQLServer
C#
13
star
32

OrleansShardedStorage

A library and test application to shard Orleans grains across multiple Azure Storage Accounts
C#
12
star
33

Orleans.Extensibility.IdentityServer

Makes Orleans the backend storage for IdentityServer4
C#
11
star
34

orleans.storageprovider.ravendb

Orleans StorageProvider for RavenDB
C#
11
star
35

orleans-samples

Sample projects
C#
10
star
36

Orleans.TelemetryConsumers.ElasticSearch

Telemetry Consumers posting to ElasticSearch
C#
10
star
37

OrleansCassandraUtils

MSR Orleans integration with Cassandra with grain persistence, clustering and reminder table support
C#
10
star
38

Orleans.Streams.Kafka

C#
8
star
39

Orleans.Telemetry.SerilogConsumer

Serilog ITelemetryConsumer and ILogConsumer implementations for Orleans.
C#
7
star
40

node-orleans

A node.js client for codename orleans
JavaScript
7
star
41

Orleans.IdentityStore

Use Orleans as an identity store for ASP Core
C#
7
star
42

Orleans.Dashboard

A Microsoft Orleans Dashboard application based on Blazor
CSS
6
star
43

OrleansCouchbaseProvider

C#
6
star
44

Orleans.TelemetryConsumers.MetricsTracker

The purpose of MetricsTracker isn't to visualize data directly, but to provide a simple, stable, performant, and effective streaming data source to feed a growing collection of useful Orleans monitoring, diagnostics & visualization tools.
JavaScript
6
star
45

Orleans.Persistence.Minio

Minio implementation of Orleans Grain Storage Provider
C#
5
star
46

Orleans.StorageProvider.Arango

πŸ₯‘ ArangoDB Storage Provider for Microsoft Orleans
C#
4
star
47

orleans.storageprovider.documentdb

DocumentDB storage provider for Orleans
C#
4
star
48

Orleans.Clustering.Cassandra

A Cassandra implementation of the Orleans Membership Provider
C#
4
star
49

orleans.serialization.json

Json serialization related libraries for Orleans.
C#
3
star
50

Orleans.TelemetryConsumers.Datadog

Orleans integration package for Datadog.
C#
3
star
51

Orleans.Persistence.Cassandra

A Cassandra implementation of the Orleans Storage Provider
C#
3
star
52

presentations

πŸ“ƒ A repository of presentations and other marketing material for Microsoft Orleans
3
star
53

WebHostCompatibilityLayer

C#
2
star
54

Orleans.Providers.Firebase

Orleans providers for Firebase realtime database.
C#
2
star
55

Orleans.AspNetCore.SignalR

SignalR backplane based on Microsoft Orleans for ASP.NET Core
2
star
56

OrleansStatsDUtils

Orleans statistics provider for StatsD
C#
2
star
57

orleans.storageprovider.cloudservicewrapper

Azure Cloud Service Wrapper class for Orleans Storage Providers to enable configuration of settings using the Azure service configuration file
C#
1
star