Access IServiceProvider when using generic IHostBuilder












1















I'm using IHostBuilder in a .NET Core 2.1 console application. Main looks like this:



    public static async Task Main(string args)
{
var hostBuilder = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
// Register dependencies
// ...

// Add the hosted service containing the application flow
services.AddHostedService<RoomService>();
});

await hostBuilder.RunConsoleAsync();
}
}


Before, with IWebHostBuilder, I had the Configure() method that let me do this:



public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Resolve something unrelated to the primary dependency graph
var thingy = applicationBuilder.ApplicationServices.GetRequiredService<Thingy>();
// Register it with the ambient context
applicationBuilder.AddAmbientThingy(options => options.AddSubscriber(thingy));

// Use MVC or whatever
// ...
}


This allowed me to register something ambient (using the Ambient Context pattern), not part of the application's main dependency graph. (As you can see, I still use the container to instantiate it, which is certainly preferable to newing it up manually. We could see it as a secondary, ambient dependency graph.)



With the generic host builder, we never seem to get access to the built IServiceProvider or the IApplicationBuilder. How do I achieve the same registration in this case?










share|improve this question























  • Just realized it's host builder and not web. checking it out.

    – Nkosi
    Nov 26 '18 at 16:05











  • I get what you want to do. What I am having trouble understanding is where you would like to do this.

    – Nkosi
    Nov 26 '18 at 17:33











  • Reviewing HostBuilder the source, the provider is the last thing built before creating the host. Not seeing much extensibility points available to do what you have described so far. Maybe if you explain where exactly you want to this ambient configuration, we may be able to see if it is in fact possible.

    – Nkosi
    Nov 26 '18 at 17:55













  • @Nkosi I seem to have found an answer that works. I have posted it below. I think it also answers your question of where I would like to do this.

    – Timo
    Nov 27 '18 at 16:05
















1















I'm using IHostBuilder in a .NET Core 2.1 console application. Main looks like this:



    public static async Task Main(string args)
{
var hostBuilder = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
// Register dependencies
// ...

// Add the hosted service containing the application flow
services.AddHostedService<RoomService>();
});

await hostBuilder.RunConsoleAsync();
}
}


Before, with IWebHostBuilder, I had the Configure() method that let me do this:



public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Resolve something unrelated to the primary dependency graph
var thingy = applicationBuilder.ApplicationServices.GetRequiredService<Thingy>();
// Register it with the ambient context
applicationBuilder.AddAmbientThingy(options => options.AddSubscriber(thingy));

// Use MVC or whatever
// ...
}


This allowed me to register something ambient (using the Ambient Context pattern), not part of the application's main dependency graph. (As you can see, I still use the container to instantiate it, which is certainly preferable to newing it up manually. We could see it as a secondary, ambient dependency graph.)



With the generic host builder, we never seem to get access to the built IServiceProvider or the IApplicationBuilder. How do I achieve the same registration in this case?










share|improve this question























  • Just realized it's host builder and not web. checking it out.

    – Nkosi
    Nov 26 '18 at 16:05











  • I get what you want to do. What I am having trouble understanding is where you would like to do this.

    – Nkosi
    Nov 26 '18 at 17:33











  • Reviewing HostBuilder the source, the provider is the last thing built before creating the host. Not seeing much extensibility points available to do what you have described so far. Maybe if you explain where exactly you want to this ambient configuration, we may be able to see if it is in fact possible.

    – Nkosi
    Nov 26 '18 at 17:55













  • @Nkosi I seem to have found an answer that works. I have posted it below. I think it also answers your question of where I would like to do this.

    – Timo
    Nov 27 '18 at 16:05














1












1








1


0






I'm using IHostBuilder in a .NET Core 2.1 console application. Main looks like this:



    public static async Task Main(string args)
{
var hostBuilder = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
// Register dependencies
// ...

// Add the hosted service containing the application flow
services.AddHostedService<RoomService>();
});

await hostBuilder.RunConsoleAsync();
}
}


Before, with IWebHostBuilder, I had the Configure() method that let me do this:



public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Resolve something unrelated to the primary dependency graph
var thingy = applicationBuilder.ApplicationServices.GetRequiredService<Thingy>();
// Register it with the ambient context
applicationBuilder.AddAmbientThingy(options => options.AddSubscriber(thingy));

// Use MVC or whatever
// ...
}


This allowed me to register something ambient (using the Ambient Context pattern), not part of the application's main dependency graph. (As you can see, I still use the container to instantiate it, which is certainly preferable to newing it up manually. We could see it as a secondary, ambient dependency graph.)



With the generic host builder, we never seem to get access to the built IServiceProvider or the IApplicationBuilder. How do I achieve the same registration in this case?










share|improve this question














I'm using IHostBuilder in a .NET Core 2.1 console application. Main looks like this:



    public static async Task Main(string args)
{
var hostBuilder = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
// Register dependencies
// ...

// Add the hosted service containing the application flow
services.AddHostedService<RoomService>();
});

await hostBuilder.RunConsoleAsync();
}
}


Before, with IWebHostBuilder, I had the Configure() method that let me do this:



public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Resolve something unrelated to the primary dependency graph
var thingy = applicationBuilder.ApplicationServices.GetRequiredService<Thingy>();
// Register it with the ambient context
applicationBuilder.AddAmbientThingy(options => options.AddSubscriber(thingy));

// Use MVC or whatever
// ...
}


This allowed me to register something ambient (using the Ambient Context pattern), not part of the application's main dependency graph. (As you can see, I still use the container to instantiate it, which is certainly preferable to newing it up manually. We could see it as a secondary, ambient dependency graph.)



With the generic host builder, we never seem to get access to the built IServiceProvider or the IApplicationBuilder. How do I achieve the same registration in this case?







c# dependency-injection .net-core .net-core-2.1 ambientcontext






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 15:54









TimoTimo

3,09612635




3,09612635













  • Just realized it's host builder and not web. checking it out.

    – Nkosi
    Nov 26 '18 at 16:05











  • I get what you want to do. What I am having trouble understanding is where you would like to do this.

    – Nkosi
    Nov 26 '18 at 17:33











  • Reviewing HostBuilder the source, the provider is the last thing built before creating the host. Not seeing much extensibility points available to do what you have described so far. Maybe if you explain where exactly you want to this ambient configuration, we may be able to see if it is in fact possible.

    – Nkosi
    Nov 26 '18 at 17:55













  • @Nkosi I seem to have found an answer that works. I have posted it below. I think it also answers your question of where I would like to do this.

    – Timo
    Nov 27 '18 at 16:05



















  • Just realized it's host builder and not web. checking it out.

    – Nkosi
    Nov 26 '18 at 16:05











  • I get what you want to do. What I am having trouble understanding is where you would like to do this.

    – Nkosi
    Nov 26 '18 at 17:33











  • Reviewing HostBuilder the source, the provider is the last thing built before creating the host. Not seeing much extensibility points available to do what you have described so far. Maybe if you explain where exactly you want to this ambient configuration, we may be able to see if it is in fact possible.

    – Nkosi
    Nov 26 '18 at 17:55













  • @Nkosi I seem to have found an answer that works. I have posted it below. I think it also answers your question of where I would like to do this.

    – Timo
    Nov 27 '18 at 16:05

















Just realized it's host builder and not web. checking it out.

– Nkosi
Nov 26 '18 at 16:05





Just realized it's host builder and not web. checking it out.

– Nkosi
Nov 26 '18 at 16:05













I get what you want to do. What I am having trouble understanding is where you would like to do this.

– Nkosi
Nov 26 '18 at 17:33





I get what you want to do. What I am having trouble understanding is where you would like to do this.

– Nkosi
Nov 26 '18 at 17:33













Reviewing HostBuilder the source, the provider is the last thing built before creating the host. Not seeing much extensibility points available to do what you have described so far. Maybe if you explain where exactly you want to this ambient configuration, we may be able to see if it is in fact possible.

– Nkosi
Nov 26 '18 at 17:55







Reviewing HostBuilder the source, the provider is the last thing built before creating the host. Not seeing much extensibility points available to do what you have described so far. Maybe if you explain where exactly you want to this ambient configuration, we may be able to see if it is in fact possible.

– Nkosi
Nov 26 '18 at 17:55















@Nkosi I seem to have found an answer that works. I have posted it below. I think it also answers your question of where I would like to do this.

– Timo
Nov 27 '18 at 16:05





@Nkosi I seem to have found an answer that works. I have posted it below. I think it also answers your question of where I would like to do this.

– Timo
Nov 27 '18 at 16:05












1 Answer
1






active

oldest

votes


















1














Apparently, instead of calling the RunConsoleAsync() extension, we can split up the simple steps that that method takes, allowing us to do something between building and starting:



        await hostBuilder
.UseConsoleLifetime()
.Build()
.AddAmbientThingy(options => options.AddSubscriber(thingy))
.RunAsync();





share|improve this answer
























  • So the AddAmbientThingy is being invoked directly on the IHost?

    – Nkosi
    Nov 27 '18 at 16:13













  • Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

    – Timo
    Nov 27 '18 at 17:17











  • Perfect. Nice approach

    – Nkosi
    Nov 27 '18 at 17:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484777%2faccess-iserviceprovider-when-using-generic-ihostbuilder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Apparently, instead of calling the RunConsoleAsync() extension, we can split up the simple steps that that method takes, allowing us to do something between building and starting:



        await hostBuilder
.UseConsoleLifetime()
.Build()
.AddAmbientThingy(options => options.AddSubscriber(thingy))
.RunAsync();





share|improve this answer
























  • So the AddAmbientThingy is being invoked directly on the IHost?

    – Nkosi
    Nov 27 '18 at 16:13













  • Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

    – Timo
    Nov 27 '18 at 17:17











  • Perfect. Nice approach

    – Nkosi
    Nov 27 '18 at 17:18
















1














Apparently, instead of calling the RunConsoleAsync() extension, we can split up the simple steps that that method takes, allowing us to do something between building and starting:



        await hostBuilder
.UseConsoleLifetime()
.Build()
.AddAmbientThingy(options => options.AddSubscriber(thingy))
.RunAsync();





share|improve this answer
























  • So the AddAmbientThingy is being invoked directly on the IHost?

    – Nkosi
    Nov 27 '18 at 16:13













  • Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

    – Timo
    Nov 27 '18 at 17:17











  • Perfect. Nice approach

    – Nkosi
    Nov 27 '18 at 17:18














1












1








1







Apparently, instead of calling the RunConsoleAsync() extension, we can split up the simple steps that that method takes, allowing us to do something between building and starting:



        await hostBuilder
.UseConsoleLifetime()
.Build()
.AddAmbientThingy(options => options.AddSubscriber(thingy))
.RunAsync();





share|improve this answer













Apparently, instead of calling the RunConsoleAsync() extension, we can split up the simple steps that that method takes, allowing us to do something between building and starting:



        await hostBuilder
.UseConsoleLifetime()
.Build()
.AddAmbientThingy(options => options.AddSubscriber(thingy))
.RunAsync();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 16:04









TimoTimo

3,09612635




3,09612635













  • So the AddAmbientThingy is being invoked directly on the IHost?

    – Nkosi
    Nov 27 '18 at 16:13













  • Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

    – Timo
    Nov 27 '18 at 17:17











  • Perfect. Nice approach

    – Nkosi
    Nov 27 '18 at 17:18



















  • So the AddAmbientThingy is being invoked directly on the IHost?

    – Nkosi
    Nov 27 '18 at 16:13













  • Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

    – Timo
    Nov 27 '18 at 17:17











  • Perfect. Nice approach

    – Nkosi
    Nov 27 '18 at 17:18

















So the AddAmbientThingy is being invoked directly on the IHost?

– Nkosi
Nov 27 '18 at 16:13







So the AddAmbientThingy is being invoked directly on the IHost?

– Nkosi
Nov 27 '18 at 16:13















Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

– Timo
Nov 27 '18 at 17:17





Indeed, I have added it as an extension to IHost, because that actually contains an IServiceProvider property that can resolve dependencies.

– Timo
Nov 27 '18 at 17:17













Perfect. Nice approach

– Nkosi
Nov 27 '18 at 17:18





Perfect. Nice approach

– Nkosi
Nov 27 '18 at 17:18




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484777%2faccess-iserviceprovider-when-using-generic-ihostbuilder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)