Skip to content
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

Could not load file or assembly System.Data.SqlClient if building using dotnet build #373

Open
daniellittledev opened this issue Feb 26, 2020 · 13 comments

Comments

@daniellittledev
Copy link

Description

When building a netcoreapp3.1 or netstandard2.1 library, the build will succeed when using Visual Studio 2019 but will fail when built via dotnet build.

Error details:

error FS3033: The type provider 'FSharp.Data.SqlCommandProvider' reported an error: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (0x80131058)

Repro steps

Create a dotnet core library which uses the type provider.

<PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <WarningsAsErrors></WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
    <PackageReference Include="FSharp.Data.SqlClient" Version="2.0.6" />
</ItemGroup>

Expected behavior

I'd expect the non-reference version of System.Data.SqlClient to be loaded and the build to succeed.

Actual behavior

The type provider is unable to load the System.Data.SqlClient assembly as it is a reference assembly.

Known workarounds

Forcing the project to use the full framework compiler seems to work, but you need to set some properties for this to work in .netcore3.0

  <PropertyGroup>
    <DisableAutoSetFscCompilerPath>true</DisableAutoSetFscCompilerPath>
    <DotNetFscToolPath></DotNetFscToolPath>
    <DotnetFscCompilerPath></DotnetFscCompilerPath>
  </PropertyGroup>

  <Import Project="fsc.props" />

Related information

I found a number of what appear to be related but older issues. As far as i can tell .NET Standard 2.0 type providers can now be used when the F# compiler is running on .NET Core. So I believe this is an issue with loading the correct System.Data.SqlClient assembly?

Environment

  • Windows
  • SDK 3.1.100
  • F# 4.7
@aggieben
Copy link

aggieben commented Mar 2, 2020

I recently had this problem with SQLProvider (fsprojects/SQLProvider#645), and relying on MSBuild and the net FSAC backend in ionide was enough to muddle through. However, according to Phillip Carter (dotnet/fsharp#3303 (comment)), things should just work in .NET Core 3.x. That hasn't been my experience for the same reasons described here.

@viktorvan
Copy link

I am in the same situation as aggieben, I can get the typeprovider to work using the fix mentioned but it is not ideal. So it sadly does not just work “out of the box” I am afraid.

@MarneeDear
Copy link

MarneeDear commented Mar 20, 2020

For anyone wondering how to make this work, I put together an example project here:

https://github.com/MarneeDear/FSharp.Data.SqlClient-dotnet

This includes an fsc.props file that should work if you only have Visual Studio 2019 installed. The older version only pointed to Visual Studio 2017.

@brettrowberry
Copy link

For anyone wondering how to make this work, I put together an example project here:

https://github.com/MarneeDear/FSharp.Data.SqlClient-dotnet

This includes an fsc.props file that should work if you only have Visual Studio 2019 installed. The older version only pointed to Visual Studio 2017.

I have to try this!

@melanore
Copy link

melanore commented Apr 3, 2020

https://github.com/demetrixbio/FSharp.Data.Npgsql is very similar to FSharp.Data.SqlClient. We recently migrated to netstandard2.0 without any net461 dependencies. It works smoothly on Windows/Unix using 3.1.201 SDK. If you are having issues, I would suggest to try new SDK.

@smoothdeveloper
Copy link
Collaborator

Related: #380 and dotnet/fsharp#10323

Thanks @MarneeDear for the workaround and putting up the repository.

I'm not clear yet how to address this in the type provider or if this is an upstream problem with dotnet sdk version of F# compiler.

@jackfoxy
Copy link

I cannot get this to build with dotnet build at all now with latest version, 2.0.7, but it does build in VS 16.9.2.

According to paket.lock:

FSharp.Data.SqlClient (2.0.7)
     ...
      System.Data.SqlClient (>= 4.5.1)

Resulting in this error

Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0

Which is obviously very messed up.

It's time to fix this properly with the new library Microsoft.Data.SqlClient

#374

I worked my way backwards to v2.0.2, which has a different problem

Assembly attribute 'TypeProviderAssemblyAttribute' refers to a designer assembly 'FSharp.Data.SqlClient.DesignTime.dll' which cannot be loaded from path...

Same issue with v2.0.1

@omcnoe
Copy link

omcnoe commented Jul 21, 2021

I was also experiencing this issue.

I was able to build the project using Mono, rather than the native MacOS dotnet sdk. I configured this in Rider.

Configured to build using the MSBuild.dll 15.0 from Mono succeeded:

/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/15.0/bin/MSBuild.dll

But with MSBuild.dll 16.0 from the dotnet sdk the build fails:

/usr/local/share/dotnet/sdk/5.0.301/MSBuild.dll

As far as I can tell building with the Mono MSBuild.dll doesn't affect the <TargetFramework> your resulting binary actually targets.

@kevcrooks
Copy link

We'd really like this to work too, and we'd rather not have to add fsc.props or use msbuild if possible.

Is there an update on if this looks fixable for dotnet build @smoothdeveloper ?

@costa100
Copy link

costa100 commented Jan 4, 2022

Hello, where is this at? I tried to use SqlClient in rider.net with F# 6 and it gave me the same error reported in the OP.

I tried the workaround suggested by someone else (to use fsc.props) but it didn't work.

I also tried the same project in VS 2019 and it doesn't work. In the next few days I will try VS 2022, and I will be back to this thread.

Thanks

@keppelerj
Copy link

any updates on this?

@cr3wdayt5p
Copy link

cr3wdayt5p commented Aug 24, 2022

I am also interested in updates on this.

I managed to get it working on macOS 12.4 (Apple Silicon) with .NET SDK 6.0.400. But I would rather not have to provide any workarounds.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <OutputType>Exe</OutputType>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="FSharp.Data.SqlClient" Version="2.1.2" />
        <PackageReference Include="System.Data.SqlClient" Version="4.8.3">
            <ExcludeAssets>compile</ExcludeAssets>
        </PackageReference>
    </ItemGroup>
    <PropertyGroup>
        <OtherFlags>$(OtherFlags) -r:/Users/mbk/.nuget/packages/system.data.sqlclient/4.8.3/runtimes/unix/lib/netstandard2.0/System.Data.SqlClient.dll</OtherFlags>
    </PropertyGroup>
    <ItemGroup>
        <Compile Include="Program.fs" />
    </ItemGroup>
</Project>

This workaround prevents the 'ref' assembly from being provided to the compiler, and then manually adds the 'runtimes/unix/lib' assembly.

@smoothdeveloper
Copy link
Collaborator

@jackfoxy, @omcnoe, @costa100, @keppelerj, @cr3wdayt5p I am sorry I can't really help with the issue.

Please see the issue I opened dotnet/fsharp#10323 related to discrepancy between VS shipped compiler and the one in dotnet SDK, maybe report other reproduce cases that would help the compiler team to iron out a solution to this issue.

Thanks to everyone for providing more technical insights about workarounds for the different platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests