New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get connection string from app.config through .fsx file. #185

Closed
isaacabraham opened this Issue Jan 16, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@isaacabraham
Copy link

isaacabraham commented Jan 16, 2016

Have a .fs file which contains my data access. I've set the connection string based on config file connection string setting e.g. "name=MyDatabase". If I try to load this file from a .fsx file, initially this failed to even compile because my script is not in the same location as the .fs file. I fixed this by specifying the path in interactive mode as a compiler directive e.g.

[<Literal>] 
#if INTERACTIVE
let private Path = __SOURCE_DIRECTORY__ + @"..\app.config"
#else
let private Path = ""
#endif

which I then pass to the ConfigFile setting on the query type.

This now compiles through an FSX file if I e.g. execute the auto-generated "load-project-debug.fsx". But as soon as I try to actually execute the query code, it goes pop: -

System.Collections.Generic.KeyNotFoundException: Cannot find name MyDatabase in <connectionStrings> section of config file.

I assume that this is looking at the config of fsi.exe but can't know for sure from the exception message.

Is there a "standard" way of getting the provider working through both .fs files and scripts when using a connection string from config at both design and runtime? Or specifying at runtime (e.g. in a FSX file) which config file to use?

@dmitry-a-morozov

This comment has been minimized.

Copy link
Member

dmitry-a-morozov commented Jan 26, 2016

A logic to lookup for config file is different at design time and run-time.
I know it's confusing but benefits outweigh the trouble.

At design time the type provider reads config file relative to resolution folder. The resolution folder for VS project is project folder. For *.fsx script it's file folder.

static member ReadFromConfig(name, resolutionFolder, fileName) =

At run-time standard BCL api is used to read configuration.

let section = ConfigurationManager.ConnectionStrings.[name]

So your application deployment should follow some rules to make configuration info avalable. For example [MyApp].exe.config side by side with executable, web.config for web apps and Azure webs sites have their own magic, etc.
A location of config file can be acquired via System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.
The problem that in FSI it returns 'C:\Program Files (x86)\Microsoft SDKs\F#\4.0\Framework\v4.0\fsianycpu.exe.Config' which is not where type provider is looking for connection string.

I've been asked about this a lot. It just occurred to me that I can leverage "IsHostedExecution" property https://msdn.microsoft.com/en-us/library/hh323940.aspx

@dmitry-a-morozov

This comment has been minimized.

Copy link
Member

dmitry-a-morozov commented Jan 27, 2016

Can you try v1.8.1-alpha?
https://www.nuget.org/packages/FSharp.Data.SqlClient/1.8.1-alpha
I fixed the issue in that version.
If it's ok I will promote it to prod release.

@isaacabraham

This comment has been minimized.

Copy link
Author

isaacabraham commented Jan 27, 2016

Sure, give me a day or so - will let you know :-)

Thanks!

@isaacabraham

This comment has been minimized.

Copy link
Author

isaacabraham commented Feb 29, 2016

@dmitry-a-morozov Hey. I'm a bit late but finally trying this out. What's the best way to validate this? Currently what I've done is simply replace the [<Literal>] let connectionString = "" with an if interactive to set a hard coded connection string for the script. Ideally I would like to just have the connectionString as e.g. name=MyDb and it work within script and application.

@dmitry-a-morozov

This comment has been minimized.

Copy link
Member

dmitry-a-morozov commented Mar 4, 2016

@isaacabraham The fix deployed with ver 1.8.1.
https://www.nuget.org/packages/FSharp.Data.SqlClient/1.8.1.
In v 1.8.1 generated commands do not go thru standard .NET BCL api to fetch value of connection string but rather reuse same value as at design time. I thought this is the best solution because in interactive FSI connection string value doesn't change between design time and compile time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment