Access IServiceProvider when using generic IHostBuilder
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
add a comment |
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
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
ReviewingHostBuilder
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
add a comment |
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
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
c# dependency-injection .net-core .net-core-2.1 ambientcontext
asked Nov 26 '18 at 15:54
TimoTimo
3,19322636
3,19322636
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
ReviewingHostBuilder
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
add a comment |
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
ReviewingHostBuilder
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
add a comment |
1 Answer
1
active
oldest
votes
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();
So theAddAmbientThingy
is being invoked directly on theIHost
?
– Nkosi
Nov 27 '18 at 16:13
Indeed, I have added it as an extension toIHost
, because that actually contains anIServiceProvider
property that can resolve dependencies.
– Timo
Nov 27 '18 at 17:17
Perfect. Nice approach
– Nkosi
Nov 27 '18 at 17:18
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
So theAddAmbientThingy
is being invoked directly on theIHost
?
– Nkosi
Nov 27 '18 at 16:13
Indeed, I have added it as an extension toIHost
, because that actually contains anIServiceProvider
property that can resolve dependencies.
– Timo
Nov 27 '18 at 17:17
Perfect. Nice approach
– Nkosi
Nov 27 '18 at 17:18
add a comment |
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();
So theAddAmbientThingy
is being invoked directly on theIHost
?
– Nkosi
Nov 27 '18 at 16:13
Indeed, I have added it as an extension toIHost
, because that actually contains anIServiceProvider
property that can resolve dependencies.
– Timo
Nov 27 '18 at 17:17
Perfect. Nice approach
– Nkosi
Nov 27 '18 at 17:18
add a comment |
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();
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();
answered Nov 27 '18 at 16:04
TimoTimo
3,19322636
3,19322636
So theAddAmbientThingy
is being invoked directly on theIHost
?
– Nkosi
Nov 27 '18 at 16:13
Indeed, I have added it as an extension toIHost
, because that actually contains anIServiceProvider
property that can resolve dependencies.
– Timo
Nov 27 '18 at 17:17
Perfect. Nice approach
– Nkosi
Nov 27 '18 at 17:18
add a comment |
So theAddAmbientThingy
is being invoked directly on theIHost
?
– Nkosi
Nov 27 '18 at 16:13
Indeed, I have added it as an extension toIHost
, because that actually contains anIServiceProvider
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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