MSBuild 15: The "Error" task could not be instantiated - msbuild

I'm attempting to programmatically build a project which uses C#7, and therefore MSBuild 15, but this task is failing seemingly because of mismatched assembly references.
Here is my code:
string projectFilePath = Path.Combine(args.Any() ? args.First() :#"C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln");
ProjectCollection pc = new ProjectCollection();
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("Configuration", "Debug");
globalProperty.Add("Platform", "x86");
BuildParameters bp = new BuildParameters(pc);
bp.Loggers = new ILogger[] { new Logger(), new ConsoleLogger(), };
BuildRequestData BuidlRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0", new string[] { "Build" }, null);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuidlRequest);
Here is the error message:
C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln.metaproj : error MSB4127: The "Error" task could not be instantiated from the assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.Error' to type 'Microsoft.Build.Framework.ITask'.
C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln.metaproj : error MSB4060: The "Error" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
Here is a link to a project you can use to recreate the issue:
https://drive.google.com/a/xibis.com/file/d/0B-mqMIMqm_XHcVRJQmtxQkd1b3c/view?usp=sharing
You will have to change the path in the code to a project on your own machine, but it doesn't seem to matter if this is a VS 2017 project or earlier.
One other thing that may or may not be relevant, I noticed that the Microsoft.WebApplication.Build.Tasks.Dll in this folder:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications
Still seems to be referencing Microsoft.Build.Framework.dll version 14, not 15 as I would have expected.

It turns out there are two issues in my test project. The first is due to the naming of the project.
However there is a second issue which is due to the references not being correct. To use MSBuild 15 programmtically you must install the following packages:
Install-Package Microsoft.Build -Version 15.1.1012
Install-Package Microsoft.Build.Framework -Version 15.1.1012
Install-Package Microsoft.Build.Tasks.Core -Version 15.1.1012
Install-Package Microsoft.Build.Utilities.Core -Version 15.1.1012
Install-Package Microsoft.Net.Compilers -Version 2.2.0
There is one more step which is nuts and completely undiscoverable. You must now add a reference to this DLL which should be relative to your solution folder:
packages\Microsoft.Net.Compilers.2.2.0\tools\Microsoft.Build.Tasks.CodeAnalysis.dll

I opened a support ticket with Microsoft and they have confirmed that this is a bug in Visual Studio 2017.
They are aiming to get a fix for this into update 3 for Visual Studio, but this may slip.
This issue tracks the bug:
https://github.com/Microsoft/msbuild/issues/2194
There is no workaround at present for this API, but you can invoke the MSBuild exe using Process as an alternative.

If you have MSBuild installed on your PC, either directly or through Visual Studio, the proper fix is to use the package Microsoft.Build.Locator which will find that installed version and execute the build using it.
Install these packages
Microsoft.Build
Microsoft.Build.Framework
Microsoft.Build.Locator
The first 2 are needed so that the code can compile but should be excluded from the project output.
In your application startup add the following line of code, this only has to run once.
Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults();
Remove any additional binding redirects in your app.config that are specific to Microsoft.Build or other build libraries. The call to the Microsoft.Build.Locator assembly will ensure that these redirects take place automatically.
References
Update an existing application for MSBuild 15
Github MSBuildLocator.

Related

Failure writing debug information: Unable to load DLL 'Microsoft.DiaSymReader.Native.amd64.dll'

I installed a VS2017, tried to develop a first app in VS2017.
File > New Project > Visual Basic > Windows Desktop > Windows Forms App (.NET Framework)
simply tried to run an app with only one label on form, but build error comes up, I clicked on "Yes" then dialogue box appears (image).
Error Code- BC37225
Error Message- Failure writing debug information: Unable to load DLL <br>'Microsoft.DiaSymReader.Native.amd64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Then searched SO, read and tried solutions, but not worked.
What I tried from SO is-
Error while building C# Console Application: Error CS0041 Cannot find a module
(1) installed Microsoft.NETCore.Platforms (nuget package)
(2) installed Microsoft.DiaSymReader.Native.amd64.dll (nuget package)
(3) Unable to load DLL 'Microsoft.DiaSymReader.Native.amd64.dll
in this, solution is available (accepted answer), but I don't know from where to delete these lines of code.
<PropertyGroup>
<Deterministic> true </Deterministic>
</PropertyGroup>
what could be the solution?
When the build errors dialog comes up it asks if you want to run a previous build because the current build will not compile (won't make a new .exe file) Since this is the first run of your program there was no previous build, no .exe. What you need to resolve are the build errors.

ASP.NET Core configurations issue with rc1 vs rc2 and more

I am using the DotNetCore.1.0.0-VS2015Tools.Preview2.exe with Visual Studio Update 3.
I am unable to migrate code to read settings in the appsettings.json file from an MVC6 app.
The code uses the #inject Microsoft.Extensions.OptionsModel.IOptions Settings syntax in a razor file to read the configuration.
The only version of Microsoft.Extensions.OptionsModel.IOptions available is rc1, whereas the Microsoft.Extensions.Options.ConfigurationExtensions version is rc2.
Therefore I am getting the 'ambiguous call' error referred to in a related .net github issue:
Error CS0121 The call is ambiguous between the following methods or properties:
Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure and Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure. This error is about using ANY rc1 version concurrently with any rc2 version of any library/component listed in project.json.
leaving out the Microsoft.Extensions.OptionsModel.Options part I get this further error:
Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Of course, I realize this is part of the worst (or most visible) release mess in ms-land history. And such is the cost of progress. But I still would like to know the answer, please.

Clearscript files cannot be found on host

Like a lot of others I'm receiving the following error when deploying my ASP.Net MVC application:
Cannot load V8 interface assembly; verify that the following files are installed with your application: ClearScriptV8-32.dll, ClearScriptV8-64.dll, v8-ia32.dll, v8-x64.dll
Clearscript was installed as part of an effort to transform less files on the fly for page requests.
I have tested my application locally in ISS Express and ISS without a hitch.
As suggested here http://clearscript3.rssing.com/chan-14849437/all_p12.html I've also included the missing code libraries as resources to my project.
ClearScriptV8-32.dll, ClearScriptV8-64.dll, v8-ia32.dll, v8-x64.dll are all included in a folder ClearScript.V8 in the bin folder. Removing this folder does not resolve the issue.
At my wits end. Any help is appreciated.
the cause is that asp.net load instantly all libraries in /bin directory.
I added the following rule to ignore Clearscript assemblies, and it worked
<configuration>
<system.diagnostics>
<trace autoflush="true" />
</system.diagnostics>
<system.web>
<compilation>
<assemblies>
<remove assembly="ClearScriptV8-64" />
<remove assembly="ClearScriptV8-32" />
....
</assemblies>
</compilation>
...
To be clear this exception is not always caused by a missing ClearScriptV8-32.dll, ClearScriptV8-64.dll, v8-ia32.dll or v8-x64.dll. Oftentimes the issue is that a dll referenced by one of the aforementioned dlls cannot be found. In cases where the appropriate dlls are on the server installing the appropriate Visual C++ Redistributable will usually solve this transitive dependency issue.
According to the project's discussion forum ClearScript no longer supports Visual Studio 2012. Meaning the version of the Visual C++ Redistributable that needs to be installed on your server is dependent on the version of ClearScript your project is utilizing:
ClearScript Versions 5.0 - 5.3: Visual C++ Redistributable for Visual Studio 2012
ClearScript Versions 5.4 - .....: Visual C++ Redistributable for Visual Studio 2013
Might be a bit late but this may help others coming to this post.
This is a common error when you don't have the Visual C++ Redistributable for Visual Studio 2012 or above installed on the hosting server
http://www.microsoft.com/en-gb/download/details.aspx?id=30679
If you're deploying on Windows Server 2012 with IIS role, there are two things you should do to get ClearScriptV8 running:
As #no1sprite pointed out:
you have to install on the hosting server the Visual C++ Redistributable for Visual Studio 2012 or above:
http://www.microsoft.com/en-gb/download/details.aspx?id=30679
Make sure you place ClearScript.dll in website's bin\ folder, and ClearScriptV8-64.dll and v8-x64.dll into bin\ClearScript.V8.
Optional, for 32-bit applications/platforms:
If you use 32-bit platform, place ClearScriptV8-32.dll and v8-ia32.dll in website's bin\ClearScript.V8\ folder.
Also, In IIS Manager, right-click on site's Application pool and select "Advanced settings...". Set property "Enable 32-bit applications" to true.
This was seconds time starting some project with clearscript v8, and good I remembered what was the issue first time. Loading Native Lib v8.
You would think somewhere in GETTING STARTED or similar topic, devs from ClearScript should have mentioned that you need to have V8 native lib located in subfolders 'ia32' or 'x64' (Platform x86 or Platform x64 respectfully).
Create above subfolders. and place native v8 libs there (32bit into 'ia32', 64bit in 'x64').
I guess they forgot to write down that thought.
just as reminder...
source code taken from loader that helped me last time track the issue...
private static IntPtr LoadNativeLibrary()
{
var suffix = Environment.Is64BitProcess ? "x64" : "ia32";
var fileName = "v8-" + suffix + ".dll";
var messageBuilder = new StringBuilder();
var paths = GetDirPaths().Select(dirPath => Path.Combine(dirPath, deploymentDirName, fileName)).Distinct();
foreach (var path in paths)
{
var hLibrary = NativeMethods.LoadLibraryW(path);
if (hLibrary != IntPtr.Zero)
{
return hLibrary;
}
var exception = new Win32Exception();
messageBuilder.AppendInvariant("\n{0}: {1}", path, MiscHelpers.EnsureNonBlank(exception.Message, "Unknown error"));
}
var message = MiscHelpers.FormatInvariant("Cannot load V8 interface assembly. Load failure information for {0}:{1}", fileName, messageBuilder);
throw new TypeLoadException(message);
}
Oddly enough, this loader should have thrown more meaningful message in debug environment, but it didn't. Instead we have : FileNotFoundException with message "Could not load file or assembly 'ClearScriptV8' or one of its dependencies. The system cannot find the file specified.". Guess there in code elsewhere is another similar loader that actually doesn't use LoadLibrary but falls back to .Net default loader, giving meaningless Exception.
hope this helps others solve similar issues.
None of the answers worked for me. It is a Windows Service application.
Based on accepted answer;
I removed v8-ia32.dll & ClearScriptV8-32.dll (since my application is targeting x64)
It solved the issue.
Posted answers here did not work for me, but this did:
Visual Studio -> Tools -> Options -> Project and Solutions -> Web Projects -> check "Use 64 bit version of IIS Express for web sites and projects"

Visual Studio 2012 Hot towel template not working correctly

I am following this tutorial for getting started with Hot towel, I have installed the ASP.NET Web Tools 2012.2 update and have also downloaded and installed the VSIX for the Hot towel template, but when I try create a new project with this template it is not working correctly. If I open a new project in Visual Studio 2012 and select ASP.NET MVC 4 Web Application I see the HotTowel Single Page Application template listed there, but I select the project gets created with a lot of files missing.
What I would expect to see in my Scripts folder is the following:
But instead my project looks like this:
The project created also does not compile, it throws the following exception:
Error 1 The name 'BundleConfig' does not exist in the current context
This exception is being thrown here:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Does anyone know why the Hot towel template is not working correctly in Visual Studio 2012 and what I could do to get it working?
UPDATE:
I tried installing Hot Towel with Nuget instead of the template, but that also did not work correctly. Install-Package got as far as installing Q and then gave an exception. Here is the Nuget output:
PM> Install-Package HotTowel
Attempting to resolve dependency 'Breeze.WebApi (≥ 1.4.1)'.
Attempting to resolve dependency 'Breeze.Client (≥ 1.4.2)'.
Attempting to resolve dependency 'Q'.
Install-Package : External packages cannot depend on packages that target projects.
At line:1 char:1
+ Install-Package HotTowel
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
I solved this problem by updating NuGet Package Manager. I updated it from here:
http://docs.nuget.org/docs/start-here/installing-nuget

Does mono 2.8 support 'dynamic' keyword?

I tested IronPython on mono 2.8 with the code in the book Professional IronPython p.315 listing 15-3.
using System;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
namespace Method2
{
class Program
{
static void Main(string[] args)
{
// Obtain the runtime.
var IPY = Python.CreateRuntime();
// Create a dynamic object containing the script.
dynamic TestPy = IPY.UseFile("TestClass.py");
// Execute the __test__() method.
TestPy.__test__();
}
}
}
I see it's compiled OK, and run without a problem on Windows 7, whereas the mono 2.8 gives me the following error message.
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
`Microsoft.Scripting.Hosting.ScriptScope' does not contain a definition for `__test__'
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object)
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1
(System.Runtime.CompilerServices.CallSite,object)
at Method2.Program.Main (string[])
I thought Mono 2.8 supports C# 4.0 which has the dynamic keyword, but I see that the 'dynamic' keyword is not fully supported with mono.
Is this a bug of Mono 2.8?
ADDED
This is the python script.
# The class you want to access externally.
class DoCalculations():
# A method within the class that adds two numbers.
def DoAdd(self, First, Second):
# Provide a result.
return First + Second
# A test suite in IronPython.
def __test__():
# Create the object.
MyCalc = DoCalculations()
# Perform the test.
print MyCalc.DoAdd(5, 10)
# Pause after the test session.
raw_input('\nPress any key to continue...')
This is the command that I used
dmcs Program.cs /r:System.Core /r:IronPython.dll /r:IronPython.Modules.dll /r:Microsoft.Dynamic.dll /r:Microsoft.Scripting.dll /r:Microsoft.CSharp.dll
It compiles well, but it still breaks when I run the execution binary. Do I need to have all the dlls in the same directory where the execution binary locates?
The fact that you are getting a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException means that the dynamic keyword worked. The problem you are having is that IPY.UseFile("TestClass.py"); is returning a ScriptScope that doesn't see your test method. So the problem lies with your python source or how you are including IronPython with mono.
The dynamic keyword is certainly supported by Mono 2.8 when using the C# 4 profile.
I guess my question is how you are building this sample?
Just for kicks, I just pasted your sample into MonoDevelop. I had to tell MonoDevelop to use C# 4 instead of C# 3.5 first of all.
The dynamic keyword was introduced in C# 4 obviously.
Also, I did have to include a few assembly references: System.Core, IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, Microsoft.Scripting.dll, and Microsoft.CSharp.dll. I am not sure if I needed them all.
If you are building from the command-line you need to use 'dmcs' as the compiler (to indicate the .NET 4 profile) and you need to include the assembly references.
It builds without problems with this configuration (for me at least).
I am responding to this question with respect to Mono 2.10.
I could successfully build and execute the code from command line on Windows 7 without using MonoDevelop. Here are the steps:
Install Mono 2.10.8 on Windows 7.
Install IronPython 2.7.2.1.
"C:\Program Files\Mono\bin" or equivalent and "C:\Program Files\IronPython 2.7" or equivalent should be part of system path.
Both TestClass.py and Program.cs should be in the same folder.
From the dos prompt, corresponding to the folder where TestClass.py and Program.cs exists, execute setmonopath batch file.
From the same dos prompt execute the following command:
dmcs Program.cs /r:System /r:"C:\Program Files\IronPython 2.7\IronPython.dll" /r:"C:\Program Files\IronPython 2.7\IronPython.Modules.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Dynamic.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Scripting.dll" /r:"C:\program Files\IronPython 2.7\Microsoft.Scripting.MetaData.dll" /r:Microsoft.Csharp
Program.exe should be successfully generated and the same can be executed without any error message.