Search code examples
c#iisoledb.net-7.0dbf

System.Data.OleDb is not supported on this platform on windows server 2022 IIS


I'm developing an ASP.NET Core Web API with .NET 7.0 Framework whose sole functionality is to read DBF files and return data in JSON format. We have a very old business application developed with Visual FoxPro, and this API will provide a bridge to other tools.

To read these DBF files, I use the NuGet package System.Data.OleDb, which allows me to query my files with SQL queries. From what I understand of the package page on NuGet.org (here), the net7.0 version is compatible.

Here's the code :

private void ReadFile()
{
    var connection_string = "Provider=VFPOLEDB.1;Data Source=[path_to_file]";

    try
    {
        using OleDbConnection connection = new(connection_string);
        connection.Open();

        var query = "SELECT * FROM FILE.DBF WHERE [condition]";

        OleDbCommand cmd = new(query, connection);
        OleDbDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader[0]);
        }

        connection.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

This code works as long as I specify x86 in the PlatformTarget property of the project properties, otherwise, I get the error "The 'VFPOLEDB.1' provider is not registered on the local machine".

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PlatformTarget>x86</PlatformTarget>
    <Platforms>AnyCPU;x86</Platforms>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
    <PackageReference Include="System.Data.OleDb" Version="7.0.0" />
  </ItemGroup>

</Project>

But, when I publish the project to IIS on a Windows 2022 Server x64, I get the error "System.Data.OleDb is not supported on this platform", whereas locally I have no problems at all.

On the relevant application pool, I've set "Enable 32-bit applications" to true and .NET CLR version to 4.0.

Here is the information returned by the dotnet --info command :

SDK .NET :
 Version:   7.0.403
 Commit:    142776d834

Environnement d'exécution :
 OS Name:     Windows
 OS Version:  10.0.20348
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.403\

Host:
  Version:      7.0.13
  Architecture: x64
  Commit:       3f73a2f186

.NET SDKs installed:
  6.0.416 [C:\Program Files\dotnet\sdk]
  7.0.403 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

I've tried many solutions posted on forums (including SO) but nothing works. If you have any ideas or even hints, I'd love to hear from you.


Solution

  • On @jmcilhinney advice I tried to use Odbc but without success, either locally or on the server. I had an External table not in expected format error, which made me realize that there's a driver difference between a classic DBF file and a DBF file from Visual FoxPro. When I tried to change driver, I got the error Data source name not found and no default driver specified. Honestly, I think I could get Odbc to work, but after several unsuccessful searches I wanted to try OleDb again. I didn't change anything in the code, but I did make several modifications to both the server and the publishing profile. I don't know which of these changes made the application work, but in case anyone else has the same problem as me, here are the changes I made :

    • I installed a license of Microsoft Visual FoxPro 9.0 on the Windows Server 2022 machine, which in my opinion allowed me to install the necessary dependencies. I don't think this is the solution, but I'd rather report it anyway.
    • In the application pool on IIS, I went to Advanced settings and changed the pipeline mode from Integrated to Classic. I can't find the source where I saw this proposed solution, but it may be the reason for resolution.
    • From Visual Studio, I went to: Right-click on the project -> Add -> COM Reference and checked Microsoft OLE DB Provider for Visual FoxPro 7.0 Type Library. I haven't had time to research these COM references and have no idea what they correspond to, but just know that I've checked this reference.
    • In the publishing profile, I changed the "Delete existing files" option from false to true. This was the last change I made, and it was after this deployment that everything worked. Did it delete any residual files (DLLs?) or other files that were designed to run in 64-bit mode, given that my first deployments were not targeted at win-x86?

    One of these modifications (or the accumulation of several perhaps) made my deployment work. Even if this was already the case for me, as @Alexander Powolozki and @YurongDai pointed out, be careful to select "win-x86" in the target runtime and to set "enable the 32-bit version" to true in IIS manager.

    Hopefully this message will help someone else. Thanks for your help.