Registering a specific module constructor

I would like to enter different lines in each of my module constructors. I am registering a factory method that creates a module. Then I can call container.Resolve<T>()and everything will be fine. For some reason, although Nancy is trying to resolve my module, she throws an error

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Plugin.HomeModule ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: System.String

public class HomeModule : NancyModule
{
    public HomeModule(string text)
    {
    }
}

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    base.ConfigureApplicationContainer(container);
    container.Register<HomeModule>((ctr, param) => { return new HomeModule("text"); });
    HomeModule module = container.Resolve<HomeModule>();
}

I also tried registering ConfigureRequestContainer()with the same results. I tried container.Register<HomeModule>(new HomeModule("some text"));as well AsSingleton(). I can register the implementation for the string type with container.Register<string>("text"), but this will lead to the introduction of the same string in all modules.

, Nancy ?

+3
3

, - , , , factory. . - , , ,

+2

, , , .

public CustomModuleCatalog()
{
    // The license type is read from db in Global.ascx.
    // So I want to register a module based on a namespace. 
    // The namespace is the same like the license name.
    if(WebApiApplication.LicenseType == LicenseType.RouteOne)
    {
        var assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
        var modules = assemblyTypes.Where(t => t.Namespace != null && t.Namespace.EndsWith("MyCustomNamespace"));
        var nancy = modules.Where(t => t.IsAssignableFrom(typeof(INancyModule)));
        foreach (var type in nancy)
        {
            var nancyType = (INancyModule)type;
            _modules.Add(type, (INancyModule)Activator.CreateInstance(type));
        }
    }
}

public IEnumerable<INancyModule> GetAllModules(NancyContext context)
{
    return _modules?.Values;
}

public INancyModule GetModule(Type moduleType, NancyContext context)
{
    if (_modules != null && _modules.ContainsKey(moduleType))
    {
        return _modules[moduleType];
    }
    return null;
}
0

All Articles