Ryan Peters Ryan Peters - 3 years ago 217
ASP.NET (C#) Question

Environment-specific StructureMap configuration?

Back when I used StructureMap 2.6, I was able to have it pull configuration data from a config by setting PullConfigurationFromAppConfig to TRUE. This was helpful for allowing me to override instances depending on the build configuration.

In the newest version, this appears to be no more. How can I go about specifying custom instances via a configuration file, specifically the web.config (where I am currently doing transforms based on build config). Is this possible? Is there another IoC framework that DOES support this? Thanks.

Answer Source

XML support has been dropped from most DI containers because it is considered an antiquated way to configure the container. XML is very brittle and there is no compile-time type checking, so it is often more trouble than it is worth to configure an application using it.

Even if you find a DI container that supports XML now, it is not likely to support it in the future.

The way around this is of course to put in however many conditional configuration settings you need for your use case. For example, you could use something along the lines of

    <add key="DIConfig" value="Test" />
    <!--<add key="DIConfig" value="Production" />-->

and then in your composition root use something like

if (ConfigurationManager.AppSettings["DIConfig"] == "Test")
    // load test configuration
else if (ConfigurationManager.AppSettings["DIConfig"] == "Production")
    // load production configuration

If you need to, you can make more configuration file settings (or even your own config section) to make the configuration more granular than this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download