The “TransformAppSettings” task failed unexpectedl

2020-07-11 04:38发布

问题:

I have a .NET Core 3 Blazor (server side) application which I recently upgraded to .NET Core 3.0.1 preview 6 version from the preview 5 version. When I build and run it locally, it works fine; but when trying to publish it to a file system folder (in Framework-Dependent mode), it throws this error:

C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(192,5): Error MSB4018: The "TransformAppSettings" task failed unexpectedly. System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. File name: 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

at Microsoft.NET.Sdk.Publish.Tasks.AppSettingsTransform.UpdateDestinationConnectionStringEntries(String destinationAppSettingsFilePath, ITaskItem[] destinationConnectionStrings) at Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.TransformAppSettingsInternal() at Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()

It's perhaps worth mentioning that this error didn't occur in the preview 5 version. Also, I use Visual Studio Enterprise 2019 (Windows).

Things I've tried so far: (with no luck)

  • Clean/Rebuild solution
  • Reinstall .NET Core 3 preview 6 SDK
  • Add Newtonsoft.Json package via Nuget
  • Search for related issues raised by the community on github

.csproj file

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>7.3</LangVersion>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.1.0" />
    <PackageReference Include="MatBlazor" Version="1.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="SqlTableDependency" Version="8.5.3" />
    <PackageReference Include="System.DirectoryServices" Version="4.5.0" />
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0" />
    <PackageReference Include="Telerik.UI.for.Blazor" Version="1.1.1" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="wwwroot\images\" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\{path to project file}" />
  </ItemGroup>

</Project>

回答1:

In my case, I edit the publish configuration and turned off the Database -> Use this connection at runtime. Then I could publish without this error, however, I had to manually edit the web.config and make the following changes.

modules="AspNetCorModuleV2"  had to become modules="AspNetCodeModule"

hostingModel="InProcess" had to become hostingModel="OutOfProcess" 

<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> had to become value="Production"

These changes were already in my own directories web.config, but they didn't seem to get propagated in the publish anymore.



回答2:

I had this same error and was able to fix it by editing the .pubxml.user file.

When the error occurred, my publish settings didn't have the "Use this connection at runtime" selected, nor were any database migrations checked. But when I looked in the pubxml.user file and it turns out there was a reference to a non-existent database that wasn't showing up in the Publish Settings UI. I removed that and it solved the problem.



回答3:

I have the same issue

Same Question on Stack Overflow

Though i have managed to publish my project via powershell commands

dotnet publish --configuration Release --framework netcoreapp2.2

Try it, I hope it will help...



回答4:

I know this has been resolved for you. But in my own case.

IDE: Visual Studio 2017 Enterprise Publish Profile: Folder

Follow this step:

  1. Change the build option to the environment you want to publish for (Test, Staging, Release or Production) and clean the solution.
  2. Open your project folder and delete the bin and obj folders.
  3. Restart Visual Studio
  4. After restarting VS, delete the publish profile and re-create the profile

This should fix it as it did for me. After which I was able to publish comfortably.

I hope this helps.



回答5:

All I did was uncheck the "Use this connection string at runtime" database option, since my azure server will that set that anyway it shouldn't be an issue.



回答6:

My issue was resolved when I

  1. checked "Use this connection string at runtime"

  2. unchecked the "Default connection string"