Grace
Grace copied to clipboard
Extension for Generic HostBuilder
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...
UseGrace
already exists as an extension method on IHostBuilder
in addition to IWebHostBuilder
. Have you tried it?
No, i couldnt find it. In which package it is then?
@stephenlautier it was added as part of the 7.0.0 release here.
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
You make a valid point. There probably needs to be another package for just hosting.
Yes, I think it feels a bit extra from your end to include another package, but it makes more sense
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
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:
I have a container already defined, and ideally i want to flow the container in the other one
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?
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
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
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.
Did you want me to look at how to run both in the se container?
Run in the second container you mean?
Sorry if i confused you, i had 2 issues here.
- Configure DI using
IInjectionScope
when using Generic Host viaUseGrace
- 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)
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?
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
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.
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
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.
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.
Yes currently thats what im doing pretty much, don't worry about that it will change anyway
@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 great, sure I'm not blocked in any way so I can wait. Thanks for your help 👍