I have a .Net/C# solution which contains two projects:
Web Project (ASP.Net Web API)
Unit-Test project (C#, etc)
The Unit Test project references the Web Project and using a Test Framework performs some unit tests against it.
In Visual Studio 2017, all of this builds perfectly run and the web api runs perfectly and so do my unit tests.
When I come to build is using MSBuild via the command line (either locally or on own build server), the Unit Test project reports...
error CS0103: The name 'WebApi' does not exist in the current context [c:\Code\UnitTests.csproj]
...it can't find the WebApi project at all!
Both projects are configured as .Net V4.5.2 and 'Any CPU'.
I've tried various:
Versions of MsBuild
Versions of the C# Language (in case C# 6 was causing issues as msbuild doesn't always support C# V5)
I'm building them using
c:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe" C:\Code\MyProject.sln /t:Rebuild /p:Configuration=Release
Additionally, the Unit Test project can reference other .Net Class Libaries and other ASP.NET Web API projects without problem, so it must be something to do with the ASP.NET Web API project rather than the Unit Test project itself.
I've noticed that when msbuild comes to compile the Unit Test project it shows the command line for the csc.exe compile part and it will include all references to other project, but never includes the Web API project, so msbuild must either:
Think the Web Api reference is not required and ignores it, or
Fails to detect it in the csproj file (but VS detects it perfectly fine)
Update: The Web API project was added to the solution 2-3 years ago (likely VS2013/5), but the Unit Test project was added this week using VS 2017, but I also tried VS2015 and same error.
Any other ideas?
Related
I am trying to follow the C# development path on Pluralsight to begin learning C#. I am at the section where we are instructed to create a .Net Core, Unit Test Project of type xUnit. I have followed the instructions on the course to add the new project to my primary solution file. However, when the base code for the xUnit test is populated, Ryder does not seem to detect the xUnit package which I have installed.
I have also made sure to download the latest .NET Framework and .NET core from the Microsoft Website. In addition to completing a full install of Visual Studio, ensuring I have checked each Workload Component needed to develop a .NET Core console application. I will also note that Visual Studio is not detecting the xUnit package either. I did test with and without ReSharper on Visual Studio to test if this was a JetBrains issue. Both times the xUnit package was not detected.
Attached below is the picture of the error I am getting when attempting to download and integrate the package Ryder thinks I am missing. I have also attached a screenshot of my file explorer with the xUnit packages present in the NuGet directory. So unless I am mistaken, I do believe I have the correct xUnit package installed.
This issue may be correlated to an oversight on my part or a simple error that I am not able to locate. However, due to my unfamiliarity with C# development, I am not able to trace the source of the issue. I apologize in advance for the verbose information body for this question. Thank you for your help.
Use Visual Studio to create the test project and then continue with Rider.
I'm trying to run some unit tests on a .NET project (not .NET Core). Visual Studio Code is able to recognize the project, intellisense works and it even offers the links to run and debug unit tests:
The problem is that clicking the unit test does not work. It gives the following error:
Build FAILED.
/usr/local/share/dotnet/sdk/1.0.4/Microsoft.Common.CurrentVersion.targets(1111,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.2" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
My guess is that it's trying to use the .NET Core MSBuild, but this is not a .NET Core project.
If I use mono's msbuild from the command line, the project builds. I'm also able to run the unit tests from the command line using NUnit's runner (from the NuGet package).
Do you know if it is possible to configure Visual Studio Code, on a Mac, so that it can run unit tests on a .NET project?
Info about my setup:
Resharper 2016.3.2
XUnit 2.2.0
VS 2017 RTM
Test Project .net 4.5.2
Asp.net Core Project .net 4.5.2
Repo Steps:
Create an asp.net core site targeting .net 4.5.2
Add a unit test project or class library to the solution and reference the asp.net project
Create a xunit Fact test that targets a class in the asp.net project
Run the unit test using resharper test runner
Error:
System.BadImageFormatException
Could not load file or assembly 'AFAEMS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
at AFAEMS.Tests.Class1.return_errors_if_missing_fields()
Notes:
I've done a lot of searching and cannot find any similar issues to this. I can run a unit test with a simple assert that is not targeting the asp.net core class files. I couldn't get the VS test runner to find my xunit tests (maybe b/c resharper test runner is enabled?).
These were things I found but don't indicate how to fix with the new xproj and app.config using MSBuild for VS 2017.
https://blogs.msdn.microsoft.com/visualstudioalm/2016/05/30/announcing-mstest-framework-support-for-net-core-rc2-asp-net-core-rc2/
Testing Asp.Net Core on full .NET framework
https://github.com/dotnet/sdk/issues/926
In Visual Studio change the target from “X86 ” to “Any CPU”.
Also,you will get this error if you try to host an application with a 32-bit assembly reference ,on IIS running on a 64 bit server/machine. To fix this error set "Enable 32 bit applications" flag to true in Advanced settings of the application pool hosting the application in IIS.
I was able to get this fixed by using the following settings in my Resharper unit testing options.
After upgrading our project to .net 4.6.1, some of the unit test written for MEF Open Generics turned red when they are run using mstest.exe that shipped with visual studio 2015. After further investigation I found that the Open Generics types are not getting registered in the mef catalog. Same test used run fine with vs2015 mstest before upgrading projects to .net 4.6.1.
The problem is only with mstest.exe. The same test runs fine if I run them using visual studio test. runner(vstest.console.exe). Alsohe Open Generics types are properly registered when application is launched.
I tried using .runsettings file with Framework45 value and the tests run green. When TargetFrameworkVersion is not specified I believe the mstest is falling back to .net 40 where mef open generics were not supported.
the .runsettings file was not needed when projects were on 4.5. It seems there is some issue with mstest.exe shipped with vs2015. I tried vs2015 update 3 and the issue still exists. Does anyone know work around other than using .runsettings
I read the following both articticles about the using of portable class library(PCL) in application design:
http://www.dotnetcurry.com/showarticle.aspx?ID=843
and
http://blogs.msdn.com/b/dsplaisted/archive/2012/08/27/how-to-make-portable-class-libraries-work-for-you.aspx
I created an PCL and a unit test project to test the PCL. I build everything with my visual studio 2012 and it works great, i was also able to start my application using this PCL.
I use a TFS for source controll and nightly tests.
If i try to build the the unit test project or my apllication via TFS i retrieve two errors:
CA0055 : * Could not load file: 'C:...\MyPCL.dll'.
CA0052 : No targets were selected.
The PCL use .Net Framework 4.5 and .Net for Windows Store apps as targets and all projects which are no PCL are configured to use .Net Framework 4.5. I does not use any other reference as the default ".Net Protable Subset" reference.
If i check the build server there is a compiled and working version of my PCL.
If i disable the code analysis while building there are no errors and all unit test works fine.
But it is no solution for me to disbale the code analysis. So has anybody an idea why it crashed and how to get it working with code analyse?
I ran into the same problem after renaming the project. Check the AssemblyInfo.cs file, make sure the assemblyTitle is correct. Better to have it same as the project name, and is not conflicting with any other projects
Is your local build working fine when code analysis is enabled in Visual Studio?
If yes then one of the possible reason for this issue can be build output path in TFS Build. In local builds, output files are generated in bin directory of respective projects where as in TFS Build all the project output files are copied in binaries directory.