Unable to reference PCL Project in Unit Test Project in VS2019 - Xamarin - visual-studio

How do I fix this issue ? Unable to add PCL reference to my Unit Test Proj.
PCL proj is using framework NetStandardLib v2.1

Testing this, NUnit Test project doesn't let you switch between Framework and Core once you have created it. Perhaps because different PackageReferences are needed.
Safest fix is to create a new NUnit Test project. But this time, when it asks for target Framework, pick a .Net core one. Probably pick the highest number it offers (Core 3.1).
Then copy or move all your test source files into it.
Looking at a Framework Test Project vs a Core one, these are the lines that are different.
Framework:
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
...
<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
</ItemGroup>
...
Core:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
...
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
...

Related

Class not registered exception: BitmapImage

When I try to create a BitmapImage instance (under WinUI 3) I get an exception that a necessary class is not registered. This is taking place within Visual Studio 2022, within an xUnit based test project.
The line throwing the exception is simply:
var retVal = new BitmapImage();
Given how simple the line is I'm wondering if there's something wrong with my xUnit project setup. Here it is:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MapLibraryWinApp\MapLibraryWinApp.csproj" />
</ItemGroup>
</Project>
Here's the project file for the library containing the code I'm trying to test:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
<Nullable>enable</Nullable>
<RootNamespace>J4JSoftware.MapLibrary</RootNamespace>
<AssemblyName>J4JSoftware.MapLibraryWinApp</AssemblyName>
<AssemblyTitle>Map Library (Windows Apps)</AssemblyTitle>
<Version>0.5.0</Version>
<Copyright>Copyright © Mark A. Olbert</Copyright>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.197" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\J4JLogging\J4JLogging\J4JLogging.csproj" />
<ProjectReference Include="..\MapLibrary\MapLibrary.csproj" />
</ItemGroup>
</Project>
I thought perhaps the test project might need to contain the <UseWinUI> tag, but adding it did not solve the problem.
You need to run an actual packaged WinUI app or deploy the Windows App SDK runtime along with a non-MSIX-packaged app to be able to use the WinUI types.
I am afraid it's not as simple as simply referencing the WinUI code from a standalone testrunner app.
Please refer to this blog post for an example of how to set up an actual app that runs the tests. Or avoid creating UI specific stuff in your unit tests.

Blazor WebAssembly Project Cannot Reference NETCore Class Library

I'm trying to reference a class library from a Blazor WebAssembly project but getting an error that Blazor WebAssembly projects cannot reference ASP.NET Core shared framework projects. I've tried changing the class library target to .Net Framework but that breaks it. I'm at a loss here. How do I get this class library referenced?
Class Library project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Piranha" Version="9.1.1" />
<PackageReference Include="Piranha.AspNetCore" Version="9.1.0" />
</ItemGroup>
</Project>
I'm trying to reference a class library from a Blazor WebAssembly project
WebAssembly supports a subset of .net[5/core]. And that subset does not inlcude the SqlServer classes.
The direct error may point at some other cause, it's not clear. But this setup is not going to work. All Database operations have to happen on the Server.
That is why we have the [x] asp.net core hosted option in the New Project wizard.

How to run T4 template in Blazor project on build

I have a Blazor project with a T4 template I wrote for scaffolding some code automatically. It works great from within Visual Studio, but I have to modify & save the template to get it to run again (as documented and expected).
I also want to run the template when building the project, so instead of VS running the template, it has to be MSBuild. I went through a bunch of articles about the topic and it looks like I have to re-import the default targets, as explained here.
I added the following to the top of my .csproj file, and this is when things went south:
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
This is the error I'm getting:
The TargetFramework value 'netstandard2.1' was not recognized. It may
be misspelled. If not, then the TargetFrameworkIdentifier and/or
TargetFrameworkVersion properties must be specified
explicitly. TestProject C:\Program
Files\dotnet\sdk\3.1.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets 93
Full .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
</Project>
What am I missing/doing wrong?
You should note the info from the article:
Fortunately, there’s a workaround: you can import the default targets
file explicitly, and import the text templating targets after that:
Solution
You should import those targets after netstandard 2.1 node.
In my side, I use these:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.Web" />
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets"/>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build"
Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer"
Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0"/>
</ItemGroup>
</Project>
Then,

No executable found matching command "dotnet-ef" (Csproj)

I started a project on a Mac using VS Code and ASP.NET Core MVC, here is my csproj:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
I always get "No executable found matching command "dotnet-ef""
I tried it on Windows 10 and I get the same result.
What am I missing?
Added this to make it work:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version = "1.0.0"/>
</ItemGroup>
From inside the folder containing the csproj file, Add the following to the csproj file:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
Now include the needed dependencies by executing the following commands:
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools.DotNet
Restore the project so that all dependencies are checked upon
dotnet restore
In order to make sure it all went ok, run the following command
dotnet ef
A screen with the basic dotnet ef command usage should appear
Check if the project is still building:
dotnet build
Generate the scaffold from your database with the following command:
dotnet ef dbcontext scaffold "Server=<your_server_address[,port_number]>;Initial Catalog=<your_db>;User Id=<your_user>;Password=<your_password>" Microsoft.EntityFrameworkCore.SqlServer -f -c YourDbContext -o Db --json
The command above can be described as the one responsible for reading out your database and generating your scaffold poco entity classes and the dbcontext file. It requires a basic working connection string, the parameter f forces the overwrite, the c gives the context a name, the o determines the output folder and the namespace for the created classes and the json parameter outputs the command result in json instead of a zero stout.
it is nice to keep this command at hand, it will be used anytime changes from the database must be reflected upon the ORM

How to change “Class Library Project” into “Test Project"?

While working on ASP.NET MVC3 application, by mistake I have added a Class library as a Unit Test project. But unfortunately I don’t see the "Run Tests" from context menu to test the methods which are created for unit testing .
Is there any way to convert the “Class Library Project” into a “Test Project” ?
There is a property type guid in the project file.
Look at this post: How does Visual Studio /mstest identify test projects?
add Microsoft.NET.Test.Sdk from nuget
What worked for me was to do add this <PropertyGroup> tag with this libraries to my project (.csproj or .vbproj) file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
</ItemGroup>
<!--Here goes any other configuration-->
</Project>

Resources