1

I have a interface and class like this:

public sealed class UserService : IUserService
{
    private readonly ILifetimeScope container;
    public UserService()
    {
        this.container = this.ConfigureBuilder(new ContainerBuilder()).Build();
    }

    private ContainerBuilder ConfigureBuilder(ContainerBuilder builder)
    {
        builder.RegisterType<UserConfiguration>()
            .AsImplementedInterfaces();
        builder.Register(c => this.GetCredentialHelperAsync(c))
            .As<ICredentialProvider>()
            .SingleInstance();
        return builder;
    }

    private async Task<CredentialProvider> GetCredentialHelperAsync(IComponentContext componentContext)
    {
       //This seems to causing problem as I think I cannot access component when I am build. 
        var config = componentContext.Resolve<UserConfiguration>();
        if (config.User == null)
        {
            //ADD the user and set credentials
        }
        var userCredentials = new List<IUserCredential>();
        return new UserCredentialProvider(userCredentials)
    }
}

When I use autofac and I want to register this class as singleton, how do I do that. Problem is the value of user credentials is determined after reading credentials from file at run time.

With the above code I am getting exception building UserCredentialProvider as I think we cannot access objects during build. Is there a way out?

CharithJ
  • 46,289
  • 20
  • 116
  • 131

0 Answers0