blue-pharmacist-31672
10/26/2021, 10:14 AMIServiceProvider
with Pulumi in .NET as documented here.
However, I'm getting an error of System.ApplicationException: Failed to resolve instance of type MyStack from service provider.
As far as I am aware I have configured the stack in my service provider but I cannot work out how to solve this error. Looking at the source code it seems like it should work.
My entry point for the application is simply
static Task Main()
{
var serviceCollection = new ServiceCollection();
var serviceProvider = serviceCollection
.AddSingleton<Stack, MyStack>()
.BuildServiceProvider();
return Deployment.RunAsync<MyStack>(serviceProvider);
}
And my Pulumi Stack
class MyStack : Stack
{
public MyStack(System.IServiceProvider provider)
{
}
}
A trivially simple application, but I've had the same issue on the main application I'm building. Am I doing something wrong with my use of the IServiceProvider
? It's not specified in the docs so I am not sure.
Any help is greatly appreciated. Thank you.brave-planet-10645
10/26/2021, 3:02 PMMain()
method should look like this:
static Task Main()
{
var serviceCollection = new ServiceCollection();
var serviceProvider = serviceCollection
.AddSingleton<MyStack>()
.BuildServiceProvider();
return Deployment.RunAsync<MyStack>(serviceProvider);
}
So you're not registering Stack
in thesingleton, just MyStack
bored-oyster-3147
10/26/2021, 3:15 PMDeployment
is trying to resolve for MyStack
which you provided via RunAsync<MyStack>
but you only have it registered as Stack
IServiceProvider
than you will want to make sure that it is registered as Transient
instead of Singleton
. Pulumi relies on the stack actually being instantiated within the deployment so if you run multiple deployments against the same service provider instance than only the first deployment will actually work when it is registered as singleton.
But if you're actually just running it as a console application in practice, like your example, than singleton will suffice.bumpy-grass-54508
10/26/2021, 4:03 PMTask<int>
or you can get into some trouble if your stack throws any exceptions:
https://github.com/pulumi/pulumi/issues/7050#issuecomment-851580328worried-city-86458
10/26/2021, 7:00 PMblue-pharmacist-31672
10/27/2021, 11:02 AM