0

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?

Rory
  • 40,559
  • 52
  • 175
  • 261

2 Answers2

1

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.

  • 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 Jan 17 '10 at 10:33
  • Any pointers to where I'd look in the 2.2 source to create a patch to make this configurable? Many thanks! – Rory Jan 18 '10 at 00:14
  • 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. –  Jan 18 '10 at 07:47
  • Yes you can - and probably various other plugins & cases where code is deployed as a dll would also find this an issue. With MS Enterprise Library classes you can solve this problem by passing an IConfigurationSource to various objects to specify where the config will come from, instead of it using the default Configuration. This would be a useful enhancement to SubSonic (for me at least!:) - presumably there's somewhere that a level of indirection could be added to allow this. I'll let you know if I get anywhere with this. – Rory Jan 18 '10 at 11:28
1

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)

Community
  • 1
  • 1
Rory
  • 40,559
  • 52
  • 175
  • 261