Dinamically load modules and Container reference

Hello,
I’ve a quite huge WPF application that relies on ASP.NET with SS in order to get json. I’ve divided all my services in macro-areas and I’ve deployed them inside a modules folder in my main web application.

I’ve overwritten the CreateServiceController in order to specify where to look assemblies as

   protected override ServiceController CreateServiceController(params Assembly[] assembliesWithServices)
    {
        return new ServiceController(this, LoadServiceAssemblies);
    }

    private IEnumerable<Type> LoadServiceAssemblies()
    {
        var files = Directory.GetFiles(ModuleHelper.ModuleFolder, ModuleHelper.ModulePattern);
        Log.Debug("Found {files} files to load", files.Length);

        List<Type> lst = new List<Type>();

        Parallel.ForEach(files, f =>
        {
            var assembly = Assembly.LoadFile(f);

            var types = assembly.GetTypes();

            var assignable = types.Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(Service)));

            lst.AddRange(assignable);
        });

        return lst;
    }

In my Confugure method I’ve created an instance to the Container called ContainerWrapper that’s used as

public override void Configure(Container container)
{
container.Register(new ContainerWrapper { Container = container });

        var log = new LoggerConfiguration()
                   .ReadFrom.AppSettings()
                   .Enrich.With(new UsernameEnricher())
                   .MinimumLevel.Debug()
                    .CreateLogger();
...}

And the ContainerWrapper class is defined as

public class ContainerWrapper
{
    public Container Container { get; set; }
}

Till now everything works fine .When I try to call a web service I got an exception on the Container instance here

public class ServiceBase : Service
{

    protected NameValueCollection EntityMapping { get; private set; }

    protected ServiceBase()

    {
        SetStoredProcedure();
    }

    private void SetStoredProcedure()
    {
        var container = TryResolve<ContainerWrapper>();//Here I got a null

        if (container == null) throw new NullReferenceException("ContainerWrapper is null");
        EntityMapping = container.Container.ResolveNamed<NameValueCollection>("SpName");
    }
}

Before my refactoring everything was working fine

It was

private AppHost() //Tell ServiceStack the name and where to find your web services
        : base("Tesoreria", typeof(AppHost).Assembly,
              typeof(DiagnosticsService).Assembly,
              typeof(OperationsRegisterService).Assembly,
              typeof(ContractBookingService).Assembly)

What am I doing wrong? is a new Container created when I override the CreateServiceController?

Thanks

No new Container instance is created. The AppHost Container is assigned once in the ServiceStackHost constructor.

I can’t see what the issue is, can you resolve ContainerWrapper immediately after registering it?

Otherwise since you’re trying to resolve from the ServiceBase Constructor can you try using HostContext.TryResolve<ContainerWrapper>() instead?

I’m assuming you’re ServiceBase is just your base service class and you’re not trying to create a new instance of it before ServiceStack is initialized right?

1 Like