To find out the reason do the following:
- Go to Tools / Options / Projects and Solutions / Build and Run.
- Set MSBuild project build output verbosity to at least "Detailed".
- Build your project.
- Goto Output window and choose "Build" in the "Show output from".
- Search for "SGEN.EXE". That way you'll find the command line which causes the error, liker this one:
1> C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\sgen.exe /assembly:C:\MySolutionFolder\Bin\x64\MyAssembly.dll /reference:"C:\Users\MyWindowsUser.nuget\packages\system.runtime.windowsruntime\4.6.0\buildTransitive\net461\..\..\ref\netstandard2.0\System.Runtime.WindowsRuntime.dll,C:\Users\MyWindowsUser.nuget\packages\system.runtime.windowsruntime.ui.xaml\4.6.0\build\net461\..\..\ref\netstandard2.0\System.Runtime.WindowsRuntime.UI.Xaml.dll,C:\Users\MyWindowsUser.nuget\packages\microsoft.toolkit.forms.ui.controls.webview\6.0.0\lib\net462\Microsoft.Toolkit.Forms.UI.Controls.WebView.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll,C:\Users\MyWindowsUser.nuget\packages\newtonsoft.json\6.0.1\lib\net45\Newtonsoft.Json.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\PresentationCore.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.ComponentModel.DataAnnotations.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Core.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Data.DataSetExtensions.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Data.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Data.Linq.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Design.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Device.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Drawing.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Runtime.Remoting.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\Facades\System.ValueTuple.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Windows.Forms.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Xaml.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Xml.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\UIAutomationClient.dll,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.AI.MachineLearning.MachineLearningContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Activation.ActivatedEventsContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Activation.ActivationCameraSettingsContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Activation.ContactActivatedEventsContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Activation.WebUISearchActivatedEventsContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Background.BackgroundAlarmApplicationContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Calls.Background.CallsBackgroundContract.winmd,C:\Users\MyWindowsUser.nuget\packages\microsoft.windows.sdk.contracts\10.0.18362.2005\ref\netstandard2.0\Windows.ApplicationModel.Calls.CallsPhoneContract.winmd," /compiler:/keycontainer:VS_KEY_0011223344556677 /compiler:/platform:AnyCPU
I've shortened it for brevity. As you can see, there are lots of references. As the full path is used for each of them, the total length of the statement is likely to exceed the maximum command line length, which seems to be 32,000:
1>C:\MySolutionFolder\MyProject.csproj(1806,5): warning MSB6002: The command-line for the "SGen" task is too long. Command-lines longer than 32000 characters are likely to fail. Try reducing the length of the command-line by breaking down the call to "SGen" into multiple calls with fewer parameters per call.
1>C:\SBCZ\Br\IS-13869_MWV_P\w1\Source\ITML.SC.Global\ITML.SC.Global.Resources\ITML.SC.Global.Resources\ITML.SC.Global.Resources.csproj(1806,5): error MSB6003: The specified task executable "sgen.exe" could not be run. System.ComponentModel.Win32Exception (0x80004005): The filename or extension is too long
Im not sure how to split this call into multiple calls though.