views:

125

answers:

2

I'm using SubSonic 2 from within a project and I'd like to specify a different .config file from the default App.config. How can I tell SubSonic to use a specific config file?

+1  A: 

You can't - SubSonic works from Provider settings that are set for the executing environment. You could, if you wanted, use a connectionStrings.config and put that somewhere else, but SubSonic uses ConfigurationManager to open up the app's config and find it's goodies.

Rob Conery
Oh, that's a little sad. For example, when building a plugin for Internet Explorer, my assembly will build to a dll and I don't have any control of the config file for the process - I'm not going to be able to put an iexplore.exe.config file in the C:\program files\internet explorer\ directory. But thanks for the answer!
Rory
Any pointers to where I'd look in the 2.2 source to create a patch to make this configurable? Many thanks!
Rory
Interesting - can you use .NET for a plugin for IE? Either way - it's not a SubSonic thing, it's a .NET thing. Your executable (exe, dll, whatever) looks for it's configuration somewhere and typically this means an app.config or web.config. If this won't work for you I don't know if SubSonic is your best choice.
Rob Conery
Rory
+1  A: 

It appears that you can do this by setting two properties of SubSonic objects: DataService.ConfigSection and DataProvider.DefaultConnectionString. Once those are set SubSonic won't try to look for the default application configuration file, it'll just use the details you've given it.

For example:

        // Part 1: set the config section:
        string configPath = Assembly.GetExecutingAssembly().Location + ".config";
        ExeConfigurationFileMap execfg = new ExeConfigurationFileMap();
        execfg.ExeConfigFilename = configPath;
        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(execfg, ConfigurationUserLevel.None);
        DataService.ConfigSection = (SubSonicSection)configuration.GetSection(ConfigurationSectionName.SUB_SONIC_SERVICE);

        // Part 2: set the connection string
        string connectionString = configuration.ConnectionStrings.ConnectionStrings["xLocal"].ConnectionString;
        DataProvider provider = DataService.Provider;
        provider.DefaultConnectionString = connectionString;

This appears to work well, however I am sometimes experiencing a long delay on the 2nd to last line DataProvider provider = DataService.Provider;. I'm not sure if this is to do with what I'm doing here or it's a general assembly-loading problem. I've documented this problem in another question here: Call to System.Web.Configuration.ProvidersHelper.InstantiateProviders taking ages and ages (from SubSonic)

Rory