nuget restore: Exception has been thrown by the target of an invocation

This error is particularly frustrating on large solutions with many projects, as there is no hint from NuGet at what point in the file parsing failed.

To analyze the problem, try msbuild MyProject.sln; the parser of msbuild is slightly more verbose. At least it will give you a line number, so you will know where to look for the error. Open MyProject.sln in a text editor to inspect that line. In my case, it was simply a blank line that accidentally got introduced while manually resolving a TFS merge conflict.

(It may seem quite obvious to call msbuild, but in our case, that call was part of a larger build script where nuget restore would come first, aborting the build process before msbuild was reached.)

A future release of NuGet should return a more detailed error message; see issue #1150.


I found the solution after examining our source control. There was an incorrect merge (in git) which caused our solution to have 2 nested projects

Project(...) = ...
Project(...) = ...
EndProject
Global
.......

and the last EndProject was missing. What's interesting is that Visual Studio didn't fail even though our solution file was effectively corrupt.

Adding an EndProject between the 2 Projects fixed the error.


I Had same issue. The problem was the sln file has same blank lines. I removed the lines. It resolved

Ex Problem

//Blank Line
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1

    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.Build.0 = Debug|Any CPU

        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.ActiveCfg = Prod|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.Build.0 = Prod|Any CPU
    EndGlobalSection

Fixed Version

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1

    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.Build.0 = Debug|Any 
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.ActiveCfg = Prod|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.Build.0 = Prod|Any CPU
    EndGlobalSection

Tags:

Nuget

Sln File