StructureMap GetInstance传递参数

时间:2018-07-28 15:43:40

标签: .net-core structuremap

如何使用StructureMap将字符串connectionString,ILogger记录器传递到Application类中

我可以创建

_application = container.GetInstance<Application>();

但是我无法传递参数

static void Main(string[] args)
{
    var connectionString = "...";
    ILogger logger = xxxx;

    var container = Container.For<ConsoleRegistry>();
    _application = container.GetInstance<Application>();
    _application.Start(appSettings.RunCandleFetch);
    Console.ReadLine();
}

public class ConsoleRegistry : Registry
{
    public ConsoleRegistry()
    {
        Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
        });
    }
}

private class Application
{
    public Application(string connectionString, ILogger logger)
    {
    ...
    }
}

1 个答案:

答案 0 :(得分:2)

此文档页面应该非常有帮助:http://structuremap.github.io/resolving/passing-arguments-at-runtime/

这应该对您有用:

var args = new ExplicitArguments();
args.SetArg("connectionString", connectionString);
args.SetArg("logger", logger);

var app = container
    .GetInstance<Application>(args);