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.
Related
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>
...
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.
I'm working on a asp.net core MVC project. I need to add a view for a method of a controller. To do that, I right click on the body of the considered method and then choose Add View but the system shows me an error:
there was an error running the selected code generator: Package restore failed rolling back package changes MyProject.
I appreciate if anyone tells me how I can fix the issue.
I've been banging my head for a while with this. I resolved it by following these steps.
Make sure all Packages are exactly the same version! Even Patch versions seem to fail. So if your SQLite is 3.1.1 and you SQLServer is 3.1.2 it will fail
Open your csproj file and manually edit the versions of the packages to the exact same version. Currently its 5.0.3 for everything but I had Microsoft.VisualStudio.Web.CodeGeneration.Design at 5.0.2 and it was failing
Optional Step : delete these package from all projects
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="5.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
After you manually edit the csproj file, save it/them (make changes in all your csproj files btw)
I also did this step - Nuget Package Manager Settings -> Package Sources Unselect the local folder and only make sure the remote one is selected
Close Visual Studio 2019.
Delete .vs folder in the Solution folder.
Start Visual Studio again, set startup project etc.
Now try creating a scaffolder controller, it will install the packages again and should work!
I had been trying scaffolding identity with latest version of as provided below
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
</ItemGroup>
and I had been getting similar error for latest 2 days, I tried most of the solution but it didn't work out for me.
Later I created a new project and tried scaffolding and it worked, I checked .csproj of new project and configured my old one same as that and it worked for me.
Here is the version details if you wish to scaffold using latest versions.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
</ItemGroup>
may be it can help someone trying identity scaffolding with latest version(5.0.7).
Package restore failed rolling back package changes for 'MyProject'.
It seems trying to install Package(s) that is not compatible with netcore version of your app, which cause the issue.
To troubleshoot the issue, please try to check the detailed information in Output window, like below.
I don't know if this is specific to my case, but I had a problem when it didn't want to generate a api controller. Project ran, No errors, no issues. But it just didn't want to generate.
So what I did was I just rebuilt the solution and then the next time I ran the generator it showed a different error. forgot to write [keyless] on top of my class. I fixed that and then ran the generator again, and it worked like a charm.
I don't have access to NuGet currently, and I need to add a Roslyn analyzer (StyleCop) to a new .NET Standard class library project. What manual file edits (project.csproj, package.config) do I need to make? I already have the StyleCop nuget package downloaded from installing it in a .NET Framework class library earlier.
If you already have the StyleCop package in your NuGet cache, what you posted in your answer will work and it will work if you clear your cache when have access to NuGet.org.
But if you have an analyzer library that is not on a NuGet package that you want to use, you can explicitly add analyzers:
<ItemGroup>
<Analyzer Include="<path to analyzer dll" />
</ItemGroup>
Looked at an existing .NET Standard class library project with StyleCop installed. Here's the relevant portions of the project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
...
</PropertyGroup>
...
<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
...
</Project>
I have a .net core test app that's referencing a .net framework 4.7.1 project. I've updated the project file of the .net core project so that it looks like this...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PushNotifications\PushNotifications.csproj" />
</ItemGroup>
</Project>
When I debug a test, I get..
Catastrophic failure: System.TypeInitializationException: The type
initializer for 'Xunit.DiaSession' threw an exception. --->
System.IO.FileNotFoundException: Could not load file or assembly
'System.Reflection.TypeExtensions, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
system cannot find the file specified.
Adding System.Reflection.TypeExtensions to either project has no effect.
What else do I need to do?