13

Running VS 2017 15.5.3 on windows 10.

Getting this generic error "The specified task executable "sgen.exe" could not be run. The filename or extension is too long" When building project in Release configuration and X64 platform. But it builds fine in Debug configuration and Any CPU platform.

As suggested in https://developercommunity.visualstudio.com/content/problem/98090/vs-2017-153-error-the-specified-task-executable-sg.html

Was able to build it but after the setting "Generate serialization assembly" to OFF.

Don't think the filename is long. Would anyone shed light on this?

Best Regards,

Damodar

Damodar_K
  • 131
  • 1
  • 1
  • 3

3 Answers3

30

MSB6003 The specified task executable “sgen.exe” could not be run. The filename or extension is too long

As we know, The sgen.exe is used to created an XML serialization assembly for types in a specified assembly in order to improve the startup performance of a XmlSerializer when it serializes or deserializes objects of the specified types.

If you do not need the XML serialization assembly, you should set the property of GenerateSerializationAssemblies to Auto or off. And SGen by default only for proxy types generates for “Any CPU”. That is the reason why you got the error on the Release configuration and X64 platform but not on Debug configuration and Any CPU platform. You can right click on your project->Properties->Build->GenerateSerializationAssemblies, set the value to off. Then unload your project, edit project, in the project file, you can find the following line of code:

<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>

enter image description here

However, this line of code on exists on the condition Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ", So to resolve this issue, you can manually add this line of code to others conditions. Your project files configuration would end up looking something like this:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
   ...
    <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    ...
    <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
  </PropertyGroup>

See Generating an Xml Serialization assembly as part of my build for more detailed info.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    Hi Leo Liu, It will be great to know why this is happening! That is done when we change Generate serialization assembly: to Off from the application property page. As suggested in the link in the question. ... Off We are looking for proper fix. Best Regards, Damodar – Damodar_K Jan 11 '18 at 22:45
  • Hello @Leo Liu-MSFT, As I have mentioned the solution is already there, but we are looking for explanation and clean solution. Best Regards, Damodar – Damodar_K Jan 12 '18 at 02:28
  • It is a life saver. I got the same issue and it works – Miller Oct 10 '19 at 11:02
  • How do I tell if I _need_ the XML Serialization Assembly? That would be helpful information. – Hecatonchires Jun 03 '20 at 02:23
  • 1
    I had the same issue but with VS 2022 and the solution still works. However, I want to clarify that for VS 2022 the location for the GenerateSerializationAssemblies is located in: right click on your project->Properties->Compile->AvancedCompileOptions->GenerateSerializationAssemblies – Joe Salazar Jul 29 '22 at 23:00
7
  1. In Solution Explorer, right-click on the project throwing the error; then click on Properties.
  2. In the window that opens, click on the Build tab by the left.
  3. Locate the Generate serialization assembly option.
  4. Set ti to OFF or AUTO.
Manu
  • 71
  • 1
  • 1
  • 1
    Welcome to StackOverflow! While your answer may very well solve the problem presented in the question, some more explanations on what causes the problem and how your answer addresses it are desired. Consider improving your answer by editing it :D – Patrick Feb 07 '20 at 07:27
3

To find out the reason do the following:

  1. Go to Tools / Options / Projects and Solutions / Build and Run.
  2. Set MSBuild project build output verbosity to at least "Detailed".
  3. Build your project.
  4. Goto Output window and choose "Build" in the "Show output from".
  5. 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.

exaiwitmx
  • 99
  • 8