InvalidOperationException: Cannot find reference assembly 'System.AppContext.dll' file for package System.AppContext.Reference - asp.net-core

I've created a new project using ASP.Net Core 2 project targeting .Net framework 4.7.1 and I see the following error on startup.
"InvalidOperationException: Cannot find reference assembly 'System.AppContext.dll' file for package System.AppContext.Reference".
How to resolve this?

Add reference to following Nuget Package to resolve this error.
Package:
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation

If you read the documentation from Microsoft, the fix for this problem is adding the following package reference to your web project:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />

Related

Package Restore Failed Microsoft.WindowsAzure.Storage

I switched my 4.8 project to SDK format and am trying to restore the Nuget packages I need.
I can see that it is available in Nuget
However when I try to install it I get
NU1101 Unable to find package Microsoft.WindowsAzure.Storage
If I have the following in my project
<PackageReference Include="Microsoft.WindowsAzure.Storage" Version="9.3.3.0" />
Then when I build I get an error.
No packages exist with this id in source(s) nuget.org
[Update]
Now I see that the package version is deprecated
However if I try installing Azure.Storage.Blobs 12.11.0 I get
Error NU1605 Detected package downgrade: Azure.Core from 1.22.0 to
1.6.0. Reference the package directly from the project to select a different version.
The following works.
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3.0" />
When I right clicked the error in my source code VS2022 gave me the option of downloading from Nuget.

An assembly specified in the application dependencies manifest (...) was not found

I upgraded Microsoft.AspNetCore from 2.0.3 to 2.0.5 and my WebAPI project, although running successfully locally, fails to start in production (IIS). Everything was fine in production until this upgrade. The error message produced in the log directory is as follows:
Error:
An assembly specified in the application dependencies manifest (MyProject.WebAPI.deps.json) was not found:
package: 'Microsoft.AspNetCore.Mvc.Abstractions', version: '2.0.2'
path: 'lib/netstandard2.0/Microsoft.AspNetCore.Mvc.Abstractions.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.5.xml
Could someone explain to me the details of exactly what this means? I assume it's a version mismatch of sorts, but why is this occurring? I thought the latest stable releases of NuGet packages weren't supposed to have such issues.
I was able to resolve the issue by downgrading Microsoft.AspNetCore.All from 2.0.5 to 2.0.3, but would like to find a better solution to the issue so I can use the most up-to-date version of this package.
Development machines usually have the SDK installed but on production the runtime only.
Add the following to your .csproj file and publish again.
<PropertyGroup>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
Sometimes this is related to the Startup Project, For example if the migration is a class library in Azure Functions project. You have to make sure when you run Add-Migration while the EF Library project is selected as Startup Project.
For me, the marked answer didn't solve the issue. My issue was when trying to Add-Migration
Add-Migration -Name initial-migration -Context Mysln.Data.MyDbContext -StartupProject Mysln -Project Mysln.Core
And the error was like this:
I solved it by downgrading all my Entityframework packages to 2.0.0 instead of the latest 2.2.0-preview one.
If you have more than one project in your solution like me:
and if you want to scaffold dbcontext in your "non startup" project (InstantOrder.Functions.Data in my case)
then you should add the -StartupProject parameter of the Scaffold-DbContext command like this -
Scaffold-DbContext "Server=..." -Project InstantOrder.Functions.Data -StartupProject InstantOrder.Functions.Data
I know this may be old, but just in case it can help some one else, this one worked for me:
Adding:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
To the PropertyGroup in the .csproj file.
To solve the first half of the error message, An assembly specified in the application dependencies manifest (…) was not found be sure to always use the publish output when deploying to a target sever.
For a self-contained application it can be found in
bin\Release\netcoreapp2.0\win81-x64\publish
or for framework-dependent deployments in
bin\Release\netcoreapp2.0\publish
The output in the directories above are meant to be used in development only, since they are specific to machine and user configuration built with.
Taken from a related answer.
2 cents: If you just take from the build folder, the dlls for the dependency aren't provided. If you publish the folder, they are. This was the fix for me.
I had this error however my solution was somewhat different from what was posted above. My problem was that I was deploying via a zip file and while building the zip file I wasn't including sub directories therefore required files were not being included.
So if you are publishing via a zip file make sure to include all sub folders while building the zip.
I got this error while running Scaffold-DbContext command on the Library project.
Solution:
Remove the Azure Function project from the solution, and then run this command.
After that, use add an existing project feature to add the Azure Function project again in the solution.
The correct .NET Core runtime was not installed on my PC. I had NETCore.App 2.1 and 2.2, but the project was targeted to 2.0.
dotnet --list-runtimes
I installed the correct runtime from the dot.net site and it resolved the issue.
In most case you get that error because there's misalignment of versions.
I changed the Microsoft.VisualStudio.Web.CodeGeneration.Design version, an it worked.
Before
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
After
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.4" />
This happened to me when I published my Lambda to AWS after renaming the project. I deleted both the obj and bin folders, rebuilt, republished and that fixed it.
I changed filters in Yaml.
Had projects called TestHelper etc...
Testrunner tried to run projects without tests, and the build was flagged as failed.
Added:
!**\*Helper*.*
To:
- task: DotNetCoreCLI#2
inputs:
command: 'test'
projects: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
!**\*TestPlatform*.dll
!**\*Testing*.*
!**\*TestHost*.*
!**\*Helper*.*

.net core msbuild nuget with additional assemblies

This sample shows a .NET Core project which can be packaged into a nuget package just using dotnet pack, and when restored in another project, it integrates in the msbuild pipeline. One of the great things about this sample is it creates a nuget package that integrates with msbuild on linux, mac and Windows. However, the custom build code doesn't have dependencies on any other assemblies.
How can I adapt this sample to use code that uses a dependency?
Here are my failed attempts:
Attempt 1
I added a package reference to Newtonsoft.Json and changed the code to do some JSON serialisation. However, in the project that uses the build nuget, when I do a dotnet publish, I get the following error:
error MSB4018: The "Zip" task failed unexpectedly. [C:\git\MSBuild-Features-With-Nate-McMaster\Video-2\1-NuGet\Web.csproj]
error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The system cannot find the file specified. [C:\git\MSBuild-Features-With-Nate-McMaster\Video-2\1-NuGet\Web.csproj]
Additionally, if my project didn't already have a dependency on JSON.NET, adding the build nuget would unnecessarily add it.
Attempt 2
I used nuget.exe spec to create a .nuspec file. At the end of the file, I added:
<files>
<file src="bin\Release\**" target="build" />
<file src="build\**" target="build" />
</files>
However, both "dotnet pack" and "msbuild /t:pack" ignore the file, and nuget.exe pack fails with the error Unable to find 'bin\Release\0-WriteATask\bin\Release\'. Make sure the project has been built..
If I try nuget.exe pack Zipper.nuspec or msbuild /t:pack /p:NuspecFiles=Zipper.nuspec, they both fail with the message Value cannot be null or an empty string..
Attempt 3
I edited the nuspec to remove all of the placeholders that are normally calculated from the project (any string starting and ending with a $). Then, doing a nuget.exe pack Zipper.nuspec created a nupkg file, and the net46 folder contains Newtsonsoft.Json.dll, but the netstandard1.3 folder does not.
The way MSBuild loads a task assembly can make it tricky to load additional assemblies that you may depend on.
Typically, the easiest way to solve this is to ship a copy of your dependencies inside your NuGet package. But your dependencies alongside your task assembly file in the package. There may be some additional complications that require you to use AssemblyLoadContext or the AppDomain.AssemblyResolve event.
You can do this without a nuspec file by forcing MSBuild to copy your assemblies into the local build output, and then copying them into your package. Set CopyLocalLockFileAssemblies=true, and add the items to _PackageFiles
Here's an example of how to do that: https://github.com/madskristensen/BundlerMinifier/blob/3333b5c38289a247391966443370ee6f4a29bf26/src/BundlerMinifier/BundlerMinifier.csproj#L35-L47
Hopefully, this will be addressed in the future, https://github.com/Microsoft/msbuild/issues/1312, and the task assembly resolution will use the NuGet cache.
Try it with the 9.0.1 version of Newtonsoft.Json, it worked for me, all these dll load problems went away, and it still targets .NET Standard. Although I did copy all the dependencies next to the task dll, but with the 10.x version even that didn't help.

How to add reference to an external dll file in asp.net core project

Everytime I try to add reference to any dll file from my non asp.net core projects to my new asp.net core project i get this error message:
.NET Core projects only support referencing .NET framework assemblies in this release. To reference
other assemblies, they need to be included in a NuGet package and
reference that package.
What should be happen here? is there a special way to do it?, seams there is something I am missing here which different than all previous asp.net version
As of now, you cannot directly add full .NET framework dll into ASP.NET core project (netcoreapp1.0) directly. You will have to create NuGet package.
If it is project specific dll then create local NuGet package. These are the steps we followed in our project to generate NuGet package-
1.Download Nuget.exe and place it in the folder where .csproj file exists.
2.Open cmd and type nuget spec. File with .nuspec extension will be created.
3.Open the created file and add tag:
<files> <file src="..\..\SomeRoot\**\*.*" target="libs\net461" /> </files>
4.Execute nuget pack A.csproj –IncludeReferencedProjects in cmd. File with .nupkg extension gets created.
5.Go to visual studio. In NuGet package manager settings, Add in “Package Sources” and provide path where your .nupkg and .nuspec file exists.
6.Close Nuget package manager and again open it. Now you can find it in your created package source under browse tab.
Note: Your .nuspec file should be like :
<?xml version="1.0"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ABC.XYZ.FL</id>
<version>1.0.0.0</version>
<title>ABC.XYZ.FL</title>
<authors>ABC</authors>
<owners>ABC</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Framework that will be used to create objects in XYZ world</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>2016</copyright>
<tags>ABC.XYZ.FL</tags>
</metadata>
<files>
<file src="bin\Debug\*.dll" target="lib\net461" />
</files>
</package>
The following links contains more details about creating nuget package and hosting it locally:
https://docs.nuget.org/create/creating-and-publishing-a-package
https://docs.nuget.org/create/hosting-your-own-nuget-feeds
https://docs.nuget.org/consume/nuget-config-file
See if this helps.

System.DllNotFoundException: Unable to load DLL 'libuv' when publishing .NET Core Website on Windows Server 2008 R2

I'm getting an error when publishing my .NET Core Asp.NET Web Application on a Windows Server 2008 R2. It has to do with libuv:
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.DllNotFoundException: Unable to load DLL 'libuv': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.Libuv.NativeMethods.uv_loop_size()
at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.Libuv.loop_size()
at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvLoopHandle.Init(Libuv uv)
at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelThread.ThreadStart(Object parameter)
The strange thing is, when I publish locally on my development machine, I can see the libuv.dll in the publish folder. And the application works on my local machine through IIS.
project.json framework:
"frameworks": {
"net461": {}
},
Using these commands:
dotnet restore
dotnet build
dotnet publish --configuration Release --runtime active
Any idea how I can fix this?
I found the reason for this error, at least what was causing it in my case now, 10 months later.
I had an older web-api project created with .net core 1.1 and found in the *.csproj file this PropertyGroup:
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>
In a newer project that I created a few day ago but with the newer template and .NET Core 2.0 it looked like this:
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
Here the RuntimeIdentifier was missing. I added it to the new project and the error disappeared. Now the server starts as expected.
I had same problem. I solve this removing this strings from csproj
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<Reference Include="System.ServiceModel" />
</ItemGroup>