7

Using the Roslyn June 2012 CTP:

Is there a way to provide the Roslyn C# Interactive/REPL with a .config file for the code being explored? A simple example scenario is code which depends on a connection string which it would usually obtain from the app.config/web.config.

Stuart
  • 5,106
  • 2
  • 23
  • 18

3 Answers3

4

There isn't a way to do this right now, though it is something that we're thinking about for the future.

In the meantime, could you factor your code to take the connection string as a parameter, and just pass it into the method in the Interactive Window?

Kevin Pilch
  • 11,485
  • 1
  • 39
  • 41
  • 1
    Thanks Kevin, that's what I ended up doing. I hope .config functionality makes it into a future version of the REPL as I come across a fair bit of code that has dependencies on a config file. BTW - I'm a big fan of what you guy's are doing with Roslyn, it rocks! – Stuart Jun 07 '12 at 19:24
  • 1
    One thing I'd like to add: Having something like a C# scripting language can replace things like .config files and can be applied to dependency injection in general. Imagine the script file being deployed with the binary, and on startup it can execute the script and load whatever it returns into a ConfigurationManager or whatever else. Dependency injection would work similarly, just invoke the script to resolve what constructor/properties need to be called to instantiate a particular object. So basically, +1M votes for having .config in Roslyn (it's a baby step in the right direction). – gzak Nov 26 '12 at 19:44
3

This is actually possible now (and may have been at the time this question was asked). Simply create a LoadConfig.csx file with the following:

#r "System.Configuration"

using System;
using System.IO;
using System.Linq;


var paths = new[] { Path.Combine(Environment.CurrentDirectory, "Web.config"), Path.Combine(Environment.CurrentDirectory, "App.config") };
var configPath = paths.FirstOrDefault(p => File.Exists(p));

if (configPath != null)
{
    // Set new configuration path
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath);  

    // Reset current configuration load state
    var t = typeof(System.Configuration.ConfigurationManager);
    var f = t.GetField("s_initState", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    f.SetValue(null, 0);
}

Save the file somewhere you'll remember and return to the project you want to load in visual studio. Right click the project and select "Reset C# Interactive from Project". Once the C# interactive window is finished loading call:

#load "C:\path\to\LoadConfig.csx"

NOTE: You must call this immediately after loading the project. If any configuration lookups occur before you load this script then this script will fail to work.

Now the C# Interactive window should have access to your project settings.

Community
  • 1
  • 1
justin
  • 453
  • 1
  • 4
  • 13
  • 2
    Just curious if this is working for anyone else? Doesn't seem to work for me. This is a tragedy, as it renders all assemblies that get, say, connection string info from a config file as untestable from the C# interactive window. This could be such a useful tool, and as of now, it's rendered nearly useless to me. – esmoore68 Aug 24 '16 at 13:27
  • Doesn't work for me too. I'm using Visual Studio 2017 – Jalal Sep 05 '17 at 06:41
1

Already accepted answer didn't work for me; based on the comments to the accepted answer, it didn't work for 2 others as well. Below solution worked for me.

  1. Update the config file of c# interactive (REPL) which is located at

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

  2. Re-initialize REPL.