Quickstart

Filip W edited this page Nov 15, 2016 · 23 revisions
  • Create a .NET 4.5 console application in Visual Studio (other project types are also supported)
  • Install ConfigR from NuGet
  • Add a new file named the same as your project output file with a csx extension, e.g. ConsoleApplication1.exe.csx and in the file properties set Copy to Output Directory accordingly:
    • web apps: Do not copy
    • all other project types: Copy always
  • Add some configuration to the csx file e.g.

ConfigR 1.0

// regular C#, no restrictions!
Config.Link = new Uri("https://github.com/config-r/config-r");
Config.Count = 123;
  • Add some code to your project which uses the configuration, e.g.:
class MyConfig
{
    public int Count {get; set;}    
    public Uri Link {get; set;} 
}

// option 1
void Main(string[] args)
{ 
    // set up ConfigR to use Roslyn CSX scripting and load the config into a POCO
    var config = new Config().UseRoslynCSharpLoader().Load<MyConfig>().GetAwaiter().GetResult();

    var count = config.Count;  // it's a System.Int32!
    var link = config.Link;    // it's a System.Uri!
}

// option 2
void Main(string[] args)
{ 
    // set up ConfigR to use Roslyn CSX scripting and load the config into dynamic object
    var config = new Config().UseRoslynCSharpLoader().LoadDynamic().GetAwaiter().GetResult();

    var count = config.Count;  // it's a dynamic with System.Int32 under the hood
    var link = config.Link;    // it's a dynamic with System.Uri under the hood
}

// option 3
void Main(string[] args)
{ 
    // set up ConfigR to use Roslyn CSX scripting and load the config into a dictionary
    var config = new Config().UseRoslynCSharpLoader().LoadDictionary().GetAwaiter().GetResult();

    var count = config["Count"];  // it's an object with System.Int32 under the hood
    var link = config["Link"];    // it's an object with System.Uri under the hood
}

ConfigR prior to 1.0.

// regular C#, no restrictions!
Add("Count", 123);
Add("Uri", new Uri("https://github.com/config-r/config-r"));
  • Add some code to your project which uses the configuration, e.g.:
void Main(string[] args)
{ 
    var count = Config.Global.Get<int>("Count");     // it's a System.Int32!
    var uri = Config.Global.Get<Uri>("Uri");         // it's a System.Uri!
    Console.WriteLine("Count: {0}", count);
    Console.WriteLine("Uri: {0}", uri);
}
  • Run your application, e.g. press F5

Congratulations! You've freed yourself from the shackles of XML and strings!

A Note on Web Apps

For a web app, the Web.csx file should not be set to copy to the output folder. It should live in the root, alongside Web.config, and it will be executed from there. If Web.csx is set to copy to the output folder then it also appears there, but is not used. If someone sees this and assumes it is being executed then they this may cause confusion. When referencing assemblies in Web.csx, use a path to the assembly relative to the Web.csx, for example:

#r "bin\Custom.Library.dll"
Add("my-key", new Custom.Library.MyCustomType("foo", "bar"));