https://pulumi.com logo
#general
Title
# general
b

blue-pharmacist-31672

10/26/2021, 10:14 AM
Hello there, I'm trying to use an
IServiceProvider
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
Copy code
static Task Main()
    {
        var serviceCollection = new ServiceCollection();
        var serviceProvider = serviceCollection
            .AddSingleton<Stack, MyStack>()
            .BuildServiceProvider();

        return Deployment.RunAsync<MyStack>(serviceProvider);
    }
And my Pulumi Stack
Copy code
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.
b

brave-planet-10645

10/26/2021, 3:02 PM
Hey, your
Main()
method should look like this:
Copy code
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
👍 1
b

bored-oyster-3147

10/26/2021, 3:15 PM
^. The
Deployment
is trying to resolve for
MyStack
which you provided via
RunAsync<MyStack>
but you only have it registered as
Stack
Also note that if this is just a trivial example and in practice you will be running multiple deployments against the same
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.
💯 1
b

bumpy-grass-54508

10/26/2021, 4:03 PM
also make sure your Main returns
Task<int>
or you can get into some trouble if your stack throws any exceptions: https://github.com/pulumi/pulumi/issues/7050#issuecomment-851580328
☝️ 1
w

worried-city-86458

10/26/2021, 7:00 PM
I'm running a console app but with multiple passes for preview then update. Use a transient lifetime. https://github.com/gitfool/Pulumi.Dungeon/blob/master/Cli/HostBuilderExtensions.cs#L43
b

blue-pharmacist-31672

10/27/2021, 11:02 AM
@brave-planet-10645 Thank you so much! That worked perfectly.
Thank you @bored-oyster-3147 and @bumpy-grass-54508 for your tips on improving this as well 😄