0
votes

I am trying to build some applications programmatically, using Microsoft.Build.Execution, however I have encountered the following error:

The "TransformTemplates" task was not loaded from assembly C:\Program Files (x86)\Microsoft Visual Studio \2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.Build.Tasks.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version = 15.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available and that the task contains a public class that implements Microsoft.Build.Framework.ITask

My code implemented:

Dictionary<string, string> globalProperties = new Dictionary<string, string>() {
                        { "RebuildT4Templates" , "true" },
                        { "VSToolsPath", @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0" },
                        { "LangVersion", "6" },
                        { "ToolsVersion", "15.0" },
                        { "VisualStudioVersion", "15.0" }
                    };
var manager = BuildManager.DefaultBuildManager;
var buildLoggger = new InMemoryBuildLogger();
var buildParameters = new BuildParameters() { Loggers = new[] { buildLoggger } };
var buildRequestData = new BuildRequestData(csProj, globalProperties, null, new[] { "Rebuild" }, null);
var buildResult = manager.Build(buildParameters, buildRequestData);

.Csproj file that I'm trying to build.

<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <!-- Run the Transform task at the start of every build -->
    <TransformOnBuild>true</TransformOnBuild>
    <!-- -->
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <!-- Transform every template every time -->
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  <!-- add AFTER import for $(MSBuildToolsPath)\Microsoft.CSharp.targets -->
  <Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  <ItemGroup>
    <T4ParameterValues Include="BuildConfiguration">
      <Value>$(Configuration)</Value>
      <Visible>False</Visible>
    </T4ParameterValues>
  </ItemGroup>
  <Target Name="CreateT4ItemListsForMSBuildCustomTool" BeforeTargets="CreateT4ItemLists" AfterTargets="SelectItemsForTransform">
    <ItemGroup>
      <T4Transform Include="@(CreateT4ItemListsInputs)" Condition="'%(CreateT4ItemListsInputs.Generator)' == 'MSBuild:TransformAll'" />
    </ItemGroup>
  </Target>

Any ideas on how to fix this? it seems that MSBuild does not have the necessary dependencies to build T4Template

1
How did you reference Microsoft.Build.Utilities.Core.dll in your project? What is the assembly dll version of it? Did you try to use C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Utilities.Core.dll?Mr Qian

1 Answers

0
votes

As a suggestion, I think you should reference the dll by right-click on the References-->Add Reference-->Assemblies-->Extensions-->find Microsoft.Build.Utilities.Core.dll and then reference it.

==============================

Update 1

Maybe you should update your VS2017 is it is not the latest version and the new version in my side uses the 15.1.0.0 of that dll.

So I am not sure whether your old VS2017 uses the 15.0.0.0 version and it uses 15.0.0.0 under the msbuild.exe.config file. If not, you should update your VS2017.

1) Or add bindingredirect on app.config or web.config file:

 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Utilities.Core" publicKeyToken="xxxxx"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0"/>
      </dependentAssembly>
 </assemblyBinding>
 </runtime>

2) check C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe.config and make sure that there is the same binding redirect like the above for Microsoft.Build.Utilities.Core.

3) Or if not, you should try to install Microsoft.Build 15.1.xnuget package and Microsoft.Build.Runtime 15.1.x nuget package. Then, add binding redirect for them.