fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
572 stars 146 forks source link

The type initializer f threw an exception..InnerMessage: Could not load type 'System.Web.Hosting.HostingEnvironment' from assembly #605

Closed tforkmann closed 5 years ago

tforkmann commented 5 years ago

Description

I'm trying to use an app.config file to get the DataContext on runtime.

But getting following error message:

Can't get DataContext. Message: The type initializer for '<StartupCode$..>.$Config' threw an exception.. InnerMessage: Could not load type 'System.Web.Hosting.HostingEnvironment' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Repro steps

Please provide the steps required to reproduce the problem

  1. Adding CompileTime ConnectionString let [<Literal>] CompileTimeConnectionString = "Data Source=...."

  2. Get SqlType: type Sql = SqlDataProvider<Common.DatabaseProviderTypes.MSSQLSERVER, ConnectionString=CompileTimeConnectionString,UseOptionTypes=true, ContextSchemaPath = ContextSchemaPath>

  3. Add RunTime ConnectionString to App.Config file:

    <connectionStrings>
    <add name="Db"  providerName = "System.Data.ProviderName" connectionString="Server=..."/>
    </connectionStrings>
  4. Use FSharp.Configuration to receive ConnectionString from the App.Config

    type Settings = AppSettings<"app.config">
    open System.Configuration
    let connectionString = Settings.ConnectionStrings.Db
  5. Try to get DataContext with RunTimeConnection String let getDb () = SqlDme.GetDataContext connectionString

Expected behavior

Get GetDataContext without issues

Actual behavior

Assembly File Missing Error

Known workarounds

None

Related information

Thorium commented 5 years ago

I don't think this is an issue with SQLProvider.

If you do Sql.GetDataContext connectionString where the connectionString is a string it should work?

So you are just missing an assembly reference to e.g. System.Web or System.Web.Hosting when using something in there in your project. SQLProvider doesn't use System.Web at all, but maybe the configuration typeprovider does.

tforkmann commented 5 years ago

Yes, if the connection String is a string. The typeprovider works like a charm!

Ok, I guess this is related to the configuration typeprovider.

tforkmann commented 5 years ago

Ok, I got rid of the configuration typeprovider and used the standard System.Configuration module instead and it just works.