Grace icon indicating copy to clipboard operation
Grace copied to clipboard

Extension for Generic HostBuilder

Open stephenlautier opened this issue 5 years ago • 23 comments

It would be good to provide an extension method similar to the aspnet one you already have UseGrace on the IHostBuilder (Microsoft.Extensions.Hosting) similar to https://github.com/dotnet/orleans/blob/master/src/Orleans.Runtime/Hosting/GenericHostExtensions.cs#L21

What do you think?

Perhaps another minor thing, the options themselves would be a delegate (it feels nicer), similar to the orleans one, its more consistent with new apis from microsoft etc...

stephenlautier avatar Jun 12 '19 14:06 stephenlautier

UseGrace already exists as an extension method on IHostBuilder in addition to IWebHostBuilder. Have you tried it?

silkfire avatar Jun 12 '19 16:06 silkfire

No, i couldnt find it. In which package it is then?

stephenlautier avatar Jun 12 '19 18:06 stephenlautier

@stephenlautier it was added as part of the 7.0.0 release here.

ipjohnson avatar Jun 12 '19 18:06 ipjohnson

ahh oki.. im using 7.x, but in my case its not an aspnet project and the package is in Grace.AspNetCore.Hosting so i didnt had that one installed.

Perhaps it makes more sense if its out of aspnet, but i can install that one as well

stephenlautier avatar Jun 12 '19 20:06 stephenlautier

You make a valid point. There probably needs to be another package for just hosting.

ipjohnson avatar Jun 12 '19 21:06 ipjohnson

Yes, I think it feels a bit extra from your end to include another package, but it makes more sense

stephenlautier avatar Jun 12 '19 22:06 stephenlautier

Might be a silly question, when using the generic host UseGrace is there a way to get the IInjectionScope?

In aspnet you would hook void ConfigureContainer(IInjectionScope scope) on Startup

stephenlautier avatar Jun 13 '19 16:06 stephenlautier

Is there a way to copy all DI from IExportLocatorScope to IInjectionScope or export as ServiceCollection?

As I'm trying to copy all DI from the generic host to a new WebHost

Left it here as its a bit regarding this for now until .net 3.0 is released, and because I'm using the generic host.

Just to give you a better context i have the following:

image

I have a container already defined, and ideally i want to flow the container in the other one

stephenlautier avatar Jun 14 '19 12:06 stephenlautier

Hmm I don’t think so, then again im not entirely sure I understand what your end goal is.

Can you explain a bit more? Are there two hosts going in the same app sharing one container? Two containers with the same registrations?

ipjohnson avatar Jun 14 '19 12:06 ipjohnson

Concurrency issue :P .. just updated the post above, see if you understand exactly what I mean.

But basically what im trying to do is similar as you said, specifically having an Orleans and Web and they share the same DIs.

But my other issue https://github.com/ipjohnson/Grace/issues/225#issuecomment-501771045 on how to get the IInjectionScope currently is a bit worse

stephenlautier avatar Jun 14 '19 12:06 stephenlautier

The only option i found so far is by copy/pasting your code and hook it there

hostBuilder.UseServiceProviderFactory(new GraceServiceProviderFactory(graceConfig));

class GraceServiceProviderFactory
{
  public IInjectionScope CreateBuilder(IServiceCollection services)
  {
         var container = new DependencyInjectionContainer(_configuration);

   	  container.Populate(services);

   	  ConfigureServices(container); // <-- hooking it here

	  return container;
  }
}

So perhaps one option is to do it as following:

hostBuilder.UseGrace(cfg, (IInjectionScope scope) => {
    
});

But perhaps having an additional extension so its additive would be essential

// or perhaps ConfigureGraceServices so it will be less ambigous 
hostBuilder.ConfigureServices((IInjectionScope scope) => {
    
});

Unless im missing something out of how it should be done properly

stephenlautier avatar Jun 14 '19 14:06 stephenlautier

Ok I think I understand a bit more. My only concern about doing what you want to do is would there be any issue with having both orleans and asp.net registrations in the same container.

That said I could add an over load that takes an IInjectionScope, this would allow you to use one container for both features. Then you could inject an IInjectionScope into your long running service and pass it into the UseGrace overload.

ipjohnson avatar Jun 14 '19 18:06 ipjohnson

Did you want me to look at how to run both in the se container?

ipjohnson avatar Jun 18 '19 10:06 ipjohnson

Run in the second container you mean?

Sorry if i confused you, i had 2 issues here.

  1. Configure DI using IInjectionScope when using Generic Host via UseGrace
  2. Copy DI container from one to another (from Generic Host to WebHost)

The first one currently is the "biggest" issue as the only workaround I found was the one I gave you above https://github.com/ipjohnson/Grace/issues/225#issuecomment-502120591 (had to copy paste all the file basically), so I believe from your end ideally handle that scenario because currently I can replace the DI but I cannot hook to the IInjectionScope, since in this case we dont have a something similar to Startup.ConfigureContainer(IInjectionScope scope)

stephenlautier avatar Jun 18 '19 13:06 stephenlautier

I think I can find some time this weekend to work on the first part. The second part I'm a little unclear on.

When you say copy DI container does that mean the registrations are copied over or the container itself. I.E. if you had a singleton registered in the orleans container would you get the same instance of the singleton in the MVC container or would you have 2 instances of the singleton.

How would this normally be handled if you were using the built in DI?

ipjohnson avatar Jun 20 '19 10:06 ipjohnson

Great! Well, I think either or is good for me, ideally the same singleton tho.

In generic host, I believe that's how it will work. If you register any DI on the generic host itself as a singleton you will get the same instance in both aspnet and in Orleans. So for now since .netcore 3 is not out and the hostbuilder is not available for aspnet, we need to manually re-register everything on the WebHost.

So best copy with the same instance, if not possible create everything from scratch. If both are complex we can live without, but it would be nice if its possible.

If you want to see the example I was working on its here, feel free https://github.com/sketch7/orleans-heroes/blob/feature/generic-host/Heroes.Server/Program.cs#L51 https://github.com/sketch7/orleans-heroes/blob/feature/generic-host/Heroes.Server/ApiHostedService.cs#L59

stephenlautier avatar Jun 20 '19 13:06 stephenlautier

You mention the idea that once 3.0 is out you'll have access to the host builder. How would you do it in 3.0 with access to the host builder?

The example will be really helpful. I have a couple ideas that I should be able to test.

ipjohnson avatar Jun 20 '19 13:06 ipjohnson

I think they simply use the same DI container in the new one, from the generic host to the hosted service (no real magic), however since there's no aspnet generic host we need to use WebHost.CreateDefaultBuilder() and that will create everything from fresh which we need to reconfigure as its not aware of the generic host

stephenlautier avatar Jun 20 '19 14:06 stephenlautier

So reusing the container from the orleans host to the aspnet host should be pretty easy. I'll prototype an overload for UseGrace that takes an IInjectionScope and just reuse the container.

ipjohnson avatar Jun 20 '19 16:06 ipjohnson

So I took a look yesterday and tried to re-use the container and it seemed like it was a no-go because the kestrel kernel was already up and running. I think the reason they don't share a container is because you have colliding singleton registration.

I think maybe the best way to do this is two separate containers but you put your registration in a configuration module or maybe just a resuable method.

ipjohnson avatar Jun 24 '19 10:06 ipjohnson

Yes currently thats what im doing pretty much, don't worry about that it will change anyway

stephenlautier avatar Jun 24 '19 13:06 stephenlautier

@stephenlautier I am going to release a hosting only package but I want to wait for the official release of .net core 3.0 before I put together so it will be a little longer.

ipjohnson avatar Jun 30 '19 19:06 ipjohnson

@ipjohnson great, sure I'm not blocked in any way so I can wait. Thanks for your help 👍

stephenlautier avatar Jul 01 '19 08:07 stephenlautier