One or more compilation references may be missing - c#

I just added Microsoft.AspNet.MVC to my project and used Html.RenderAction to render a partial view however on compile im getting this message : One or more compilation references may be missing.
Here us how the .csproj looks:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.18" />
</ItemGroup>
</Project>
and the offending line #Html.RenderAction("MenuItems") MenuItems exists and has a MapRoute
I am using VSCode, so I may just not understand how to do a particular thing since it doesn't have the inbuilt project management that VStudio has.

I added an old version of MVC to my project to get MVC.Html helpers. I didn't need to do this.
RenderAction is deprecated and so is Action.

Related

Azure function v4 migration. Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=7.0.0.0, Culture=neutral

I'm currently migrating an Azure Function from v2 to Azure Functions v4. I already migrated my project to .NET6 and fixed all the package reference errors. I have in the solution 3 projects which I adjusted to .NET6 and Azure Function v4. For that I edited the .csproj files of each of those projects and upgraded/substituted packages which are not working with .NET6. The project file of each project looks like:
Azure Function Project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.11.1" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.7.5" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.18.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="5.1.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.8.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.48.1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PCM.1.z\x.1.z.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="2.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="1.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="3.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Second Project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.11.1" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.7.5" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.38.2" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
<PackageReference Include="SendGrid" Version="9.28.1" />
<PackageReference Include="StackExchange.Redis" Version="2.6.80" />
<PackageReference Include="Twilio" Version="6.0.1" />
<PackageReference Include="UnitsNet" Version="4.149.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\x.1.w\x.1.w.csproj" />
</ItemGroup>
</Project>
Third Project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
</ItemGroup>
</Project>
When I build the project I got no errors. But when I debug the project I got an exception which looks like:
Exception:
Exception thrown: 'System.IO.FileNotFoundException' in Microsoft.Azure.WebJobs.Host.dll.
An exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Azure.WebJobs.Host.dll but was not handled in user code
Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
I have tried the last two weeks different packages since when I googled this error, the suggestion was to see which packages are not supported by package:
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />. I analyzed all my packages. After changing them back and force I got still the same error.
Can anyone help me and say what exactly I have to change?
As #Codebrane said, Microsoft.Extensions.Configuration.Abstractions, Version=7.0.0.0 has the issues in working with Azure Functions v4 Version and it has been shown practically in this thread.
Try by downgrading the Microsoft.Extensions.Configuration.Abstractions to Version 6.0.0 and check.
Microsoft.NET.Sdk.Functions depends on the NuGet Packages related to the Code but these can be suggested by VS IntelliSense and installed automatically, or it shows these packages required.
Also, NuGet Packages varies between In-Process and Out-of-Process worker type in Azure Functions. Refer to one of my threads for more information.
I am building an Azure function as v4 in net6. Since I also got the System.IO.FileNotFoundException regarding library Microsoft.Extensions.Configuration.Abstraction 7.0.0 when starting up the function,
I downgraded to Microsoft.Extensions.Configuration.Abstractions 6.0.0 and also had to downgrade other dependencies such as Microsoft.EntityFrameworkCore to 6.0.12 and, since I am using PostgreSQL also Npgsql.EntityFrameworkCore.PostgreSQL to 6.0.8. Now everything works.
I fixed it by including the package Microsoft.Extensions.Configuration.Abstractions 6.0.0 and building it. To include this package doesn't solve the problem directly it just shows which packages have a conflict. By downgrading these conflicting packages the project could be debugged. To check which packages are conflicting you have to open up the packages on the right side in visual studio and look where is a exclamation mark. Then you have to analyze these packages to know on what version you have to downgrade them. It changes from project to project.

Is it possible to use the same version for multiple dependencies in a csproj file?

I have a C# project (.NET6) that looks like this:
project.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
...
</PropertyGroup>
...
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
...
</ItemGroup>
...
</Project>
Is it somehow possible to maintain these dependency-versions in one place (they all belong together)?
Similar to what e.g. Maven allows by using a property?
The problem is that when I want to update - e.g. to 6.0.1 - I always have to update all dependency-versions at once. Which is in particularly a problem when using automatic tools like Dependabot as they usually create one pull request for each dependency because they are unable to recognize that these dependencies belong together.
I also checked but I didn't find any other solutions so far, neither on StackOverflow nor in the Microsoft docs.
Yes, that works. Just use a variable for the versions.
Like so:
<PropertyGroup>
<EntityFrameworkVersion>6.0.0</EntityFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="$(EntityFrameworkVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(EntityFrameworkVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="$(EntityFrameworkVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkVersion)" />
...
</ItemGroup>
Now you only have to edit a single line to update the version. I normally even move the variable definition to the top-level Directory.build.props file, so that I only need to edit one single line to update the version across all projects in the solution.
There are two possible problems with this solution:
I'm not sure (and never really tested) whether dependabot is able to resolve this. It might not be.
From experience, when the solution is large (many projects that have this dependency) updating the Directory.build.props file often crashes Visual Studio. When closing it before changing the version, everything is fine, though.
Firstly, you can simplify your references because the other 2 are dependencies and will be automatically included.
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0" />
</ItemGroup>
If you did want them to share the version, you can define a property for the version:
<PropertyGroup>
<EfVersion>6.0.0</EfVersion>
<PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(EfVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="$(EfVersion)" />
</ItemGroup>
However, I wouldn't recommend this - as soon as you use any tooling (like the Nuget Package Manager) to update these references, you will likely run into problems (from memory it will replace the EfVersion with the concrete version, but that will be a tool-specific implementation detail).

DynamicRouteValueTransformer does not exist

I get a missing reference error when I try to inherit from the class DynamicRouteValueTransformer in a class library but when I do it from my MVC app it works fine.
public class ProductRouteTransformer : Microsoft.AspNetCore.Mvc.Routing.DynamicRouteValueTransformer
DynamicRouteValueTransformer is in namespace Microsoft.AspNetCore.Mvc.Routing which is part of Microsoft.AspNetCore.Mvc.Core.dll. I've added the DLL to the class library using Nuget and can see that it is installed but I still get a missing reference error. The class is just missing from the namespace.
I'm using .NET Core 3.1.
The csproj is below:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="NLog" Version="4.6.8" />
</ItemGroup>
<ItemGroup>
<Folder Include="Database\EntityUtils\" />
</ItemGroup>
</Project>
There's a topic in the docs that was introduced recently, which covers this in great detail:
With the release of .NET Core 3.0, many ASP.NET Core assemblies are no longer published to NuGet as packages.
If you take a look at the contents of your .csproj, you'll see that you've added references to 2.2.x versions of a few Microsoft.AspNetCore.* packages. I'm sure you intended to reference 3.1.x versions of these, but, as the docs quote above shows, these packages are no longer produced for 3.0+.
The next few lines from the docs explain what's happened here:
Instead, the assemblies are included in the Microsoft.AspNetCore.App shared framework, which is installed with the .NET Core SDK and runtime installers.
As of .NET Core 3.0, projects using the Microsoft.NET.Sdk.Web MSBuild SDK implicitly reference the shared framework. Projects using the Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference ASP.NET Core to use ASP.NET Core APIs in the shared framework.
As your project is referencing Microsoft.NET.Sdk, you'll need to add a FrameworkReference, as described above, and remove those old PackageReferences:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="NLog" Version="4.6.8" />
</ItemGroup>
<ItemGroup>
<Folder Include="Database\EntityUtils\" />
</ItemGroup>
</Project>
Note that not all of the packages have been folded into the shared framework. For example, Microsoft.EntityFrameworkCore.SqlServer is still provided as a NuGet package.

.NET Core 3.0 Identity issue

I'm trying to update my projects to .NET Core 3.0 and .NET Standard 2.1 (including .NET Core Identity)
I have 2 projects. First my Web API:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0" />
</ItemGroup>
...
And in this project code related with Identity works fine.
As I see here I need to remove package Microsoft.AspNetCore.Identity
https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio
2-nd project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!--<FrameworkReference Include="Microsoft.AspNetCore.App" />-->
<!--<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />-->
</ItemGroup>
...
So, if I remove Microsoft.AspNetCore.Identity from my second project I see error:
The type or namespace name 'SignInManager<>' could not be found (are you missing a using directive or an assembly reference?)
If I add this line:
<FrameworkReference Include="Microsoft.AspNetCore.App" />
I see this error:
Error NETSDK1073 The FrameworkReference 'Microsoft.AspNetCore.App' was not recognized
How can I resolve my issue?
See this Github Issue which explains a lot of the changes. As said in one of the comments.
Correct. We are removing the netstandard2.0 from most Microsoft.AspNetCore.* assemblies. See #3754
So in order to use the <FrameworkReference Include="Microsoft.AspNetCore.App" /> in your .csproj you will need to target netcoreapp3.0 or higher.
I started getting this issue after downgrading visual studio enterprise 2019 to professional 2019.
.Net core framework netcoreapp3.1
I fixed it by updating the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation from 3.1.3 to 3.1.4.

Unable to use Box API after installing nuget package

I started using Box.V2 in my asp dotnet core app in VSCode, but I was getting all kinds of annoying errors.
After finding that there is a specific nuget package for dotnet core, I uninstalled my existing box packages and installed Box.V2.Core - it appears to have installed successfully as I can see it on my installed packages list.
However, When I try using Box.V2.Core I get a
The type or namespace 'Box' could not be found
What am I supposed to do with this? Does the package actually not exist?
My csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="2.6.362"/>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3"/>
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0"/>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0-preview1-25914-04"/>
<PackageReference Include="HtmlAgilityPack.NetCore" Version="1.5.0.1"/>
<PackageReference Include="RestSharp" Version="106.2.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1"/>
<PackageReference Include="Box.V2.Core" Version="3.4.1"/>
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1"/>
</ItemGroup>
</Project>
Something strange is definitely going on with that package. I suggest first installing one release down: <PackageReference Include="Box.V2.Core" Version="3.3.0" /> and then updating it back to 3.4.1.

Categories