1
votes

I've written an MSBuild Task like this one, but mine has a reference to System.Data.SqlClient.

So I use dotnet publish to target the specific framework netcoreapp2.0 to get the dependencies in the bin dir.

The problem with this is when I run the task through an msbuild script I get: error : System.Data.SqlClient is not supported on this platform.

The build engine: Microsoft (R) Build Engine version 15.7.177.53362 for .NET Core.

Here is the complete (task & test) project

The csproj:

<Project DefaultTargets="Build">
  <UsingTask TaskName="TestTasks.TestSqlClient" AssemblyFile=".\Task\bin\Debug\netcoreapp2.0\publish\TestTask.dll" />
  <Target Name="Build">
    <TestSqlClient />
  </Target>
</Project>

list of files and dirs under bin\netcoreapp2.0\publish:

|   Microsoft.Build.Framework.dll
|   Microsoft.Build.Utilities.Core.dll
|   System.Data.SqlClient.dll
|   System.Text.Encoding.CodePages.dll
|   TestTask.deps.json
|   TestTask.dll
|   TestTask.pdb
|
\---runtimes
    +---unix
    |   \---lib
    |       \---netstandard2.0
    |               System.Data.SqlClient.dll
    |
    +---win
    |   \---lib
    |       +---netcoreapp2.0
    |       |       System.Text.Encoding.CodePages.dll
    |       |
    |       \---netstandard2.0
    |               System.Data.SqlClient.dll
    |
    +---win-arm64
    |   \---native
    |           sni.dll
    |
    +---win-x64
    |   \---native
    |           sni.dll
    |
    \---win-x86
        \---native
                sni.dll
1
Can you share the csproj of the project producing the MSBuild task assembly? I have a guess. Also, can you share what is in the publish folder? Which files and folders? - natemcmaster
@natemcmaster, I've just added the details - Ashkan Nourzadeh

1 Answers

2
votes

It appears you have run into https://github.com/Microsoft/msbuild/issues/1887. One of the current limitations of using dependencies from MSBuild tasks is that MSBuild will not load native dependencies. As you can see in your output, there is a “runtimes” folder which contains sni.dll for x86, x64, and more. The only way to workaround this on .NET Core is to manually resolve native dependencies youself using assembly load context. This is very difficult to do right and I don’t recommend it. See https://natemcmaster.com/blog/2017/11/11/msbuild-task-with-dependencies/ for more details.

A simpler workaround is to implement your tool as a console tool. Then use a target and the Exec task, or a customized implementation of ToolTask, to run your console tool during build. This is how the C# compiler itself works, and I have described this approach in more detail here: https://natemcmaster.com/blog/2017/11/11/build-tools-in-nuget/. Also, see https://github.com/natemcmaster/Yarn.MSBuild for a real-world example.