dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
10.07k stars 2.02k forks source link

Client only with Generic Host Builder is asp.net core 3.0 #5758

Open rossbuggins opened 5 years ago

rossbuggins commented 5 years ago

When using asp.net core 3.0 web api, whats the best way to use the orleans builders for a client only to connect to a remote cluster - with DI etc on the Controller APIs for access? Is there something in the builder to configure just as a client?

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })

                .UseOrleans(builder =>
                {
                   **IsThereARunAsClientOptionHere????**
                });
sergeybykov commented 5 years ago

There's a ClientBuilder class for that. Cluster client's initialization doesn't require as much as building a silo does. Plus, one can configure multiple clients to talk to multiple clusters if needed in certain cases. Because of that, UseOrleans is only used for building a silo.

rossbuggins commented 5 years ago

I was thinking of something along these lines to how I wanted to use this: #5760

lfzm commented 5 years ago

@rossbuggins It is recommended to use it Orleans.MultiClient

emilekberg commented 5 years ago

To me it would make sense to be able to add client like this as well, since we have WebBuilder in aspnetcore 3 in a similar manner, and the client is normally used together with a webservice of some sort? It would get them to share DI and similar things "automatically."

ReubenBond commented 5 years ago

I'm happy for some kind of contribution towards this. IHostBuilder.AddClusterClient(x) or similar. I would want guards (exceptions on .Build()) to ensure that silo + client are not added to the same host builder.

MV10 commented 4 years ago

I've been thinking it really needs to be set up as an IHostedService so that Close and Dispose can be called transparently at the correct point in the app lifecycle (eg. in response to StopAsync). These days even my little quick-and-dirty console test apps use Generic Host because it's so handy to pull in all the bits and pieces through DI with extension methods, and I don't have to write (cut/paste) the same old code to read config.

public class ClusterClientHostedService : IHostedService
{
    private readonly IClusterClient client;

    public ClusterClientHostedService(IClusterClient client)
    {
        this.client = client;
    }

    public Task StartAsync(CancellationToken cancellationToken)
        => Task.CompletedTask;

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        await client?.Close();
        client?.Dispose();
    }
}
MV10 commented 4 years ago

This is semi-orthogonal to the idea of clean extensions to set up a client, but it does fully implement the Generic Host idea mentioned above for correctly closing and disposing a client under that type of app lifecycle (see README for details):

https://github.com/MV10/OrleansHostedClientService

Since the client app would need to identify things like application parts, logging, etc, any PR to do this stuff in Orleans itself would require a lot more than IHostBuilder.AddClusterClient() -- you'd have to get into a full-blown set of ClientBuilder extensions to support configuration delegates along the same lines of ISiloBuilder.

I'm tied up on a personal project I'm trying to mostly hammer out during my two-week holiday vacation, but I may take a stab at this later, I don't think it would be all that difficult.

MV10 commented 4 years ago

Related design discussions in #4744

ghost commented 2 years ago

We've moved this issue to the Backlog. This means that it is not going to be worked on for the coming release. We review items in the backlog at the end of each milestone/release and depending on the team's priority we may reconsider this issue for the following milestone.