VSTS - Building ASP.NET Core 2.0, compile error: Could not locate the assembly "Microsoft.AspNetCore.Mvc.ViewFeatures" - asp.net-core

VSTS build of my ASP.NET Core 2.0 MVC app fails with these warnings:
Warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.AspNetCore.Mvc.ViewFeatures".
Warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.Extensions.Logging.Abstractions". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
Warning MSB3245: Could not resolve this reference. Could not locate the assembly "System.Data.SqlClient". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
And then I get compilation error like these:
The type or namespace name 'Extensions' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
Error CS0246: The type or namespace name 'ILogger' could not be found (are you missing a using directive or an assembly reference?)
and so on for ILoggerFactory, SqlDataReader, etc. And everything builds perfectly on my machine locally. What am I missing?

First please make sure the PackageReference in .csproj are not refer the local paths. It should like:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
Then please use ASP.Net Core template to define your build definition:
With the .NET Core tasks to restore, build, test and publish, and you can also add other tasks for your needs.

Related

how to setup EFcore database first in separate class library in ASP.NET Core MVC application?

I have a DB and I want to add a separate class library for entity framework to access my data but when I run scaffold command it makes a DB model in my web application.
You should select your Data project as default project in package manager console.
Be careful, you have ef core references in data.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0-preview3-35497"/>
<PackageReference Include="System.Data.Common" Version="4.2.1" />
</ItemGroup>
</Project>
I realized that there are some errors in ClassLibrary projects. You can take a look at them:
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#targeting-class-library-projects-is-not-supported
https://github.com/dotnet/cli/issues/8735
https://github.com/aspnet/EntityFrameworkCore/issues/10298
#ibrahimozgon's answer is right and helped me. However, I encountered a few errors in the DbContext class on the way which he didn't mention how to solve:
'EntityTypeBuilder' does not contain a definition for 'ToTable' and no accessible extension method 'ToTable' accepting a first argument of type 'EntityTypeBuilder' could be found (are you missing a using directive or an assembly reference?
and
'KeyBuilder' does not contain a definition for 'ForSqlServerIsClustered' and no accessible extension method 'ForSqlServerIsClustered' accepting a first argument of type 'KeyBuilder' could be found (are you missing a using directive or an assembly reference?
To resolve these errors in DbContext class, open up package manager console again and select the default project to be the class library. Enter these commands one by one:
- Install package: Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.4 (or whichever latest version)
- Install package: Install-Package Microsoft.EntityFrameworkCore.Relational -Version 2.2.4 (or whichever latest version)
- If errors persist try: Install-Package or Update-Package Microsoft.EntityFrameworkCore.Tools (or whichever latest version)

EntityFrameworkCore 2.x and PrivateAsset="All"

I'm building an .NetCoreApp1.1 webapi. We have the typical business and data layer assemblies broken out. When we added EntityFrameworkCore 2.x to the data access project, I was able to test functionality that went across projects. But the webapi stopped working! We were no longer able to start the app. program.cs-main failed with the exception:
System.MissingMethodException: "Method not found: 'System.IServiceProvider >MicrosoftExtensions.DependencyInjection.ServiceCollectionContainerBuilderExtens>ions.BuildServiceProvider...'
Articles indicated a package type mismatch but I couldn't find anything out of place. The EntityFrameworkcore in the data layer was suspect since it was version 2.0.0-preview1-final. In VS2017 there is a new feature to mark packages as private to the assembly. This made sense to do this with EF in the data layer so I opened the project and marked them PrivateAsset="All"
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0-preview1-final" **PrivateAssets="All"** />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0-preview1-final" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0-preview1-final" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="2.0.0-preview1-final" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0-preview1-final" PrivateAssets="All" />
<PackageReference Include="WindowsAzure.Storage" Version="8.1.4" />
</ItemGroup>
This solved my problem. I was able to rebuild and run the webapi application. But, now my test failed with:
Message: Test method
ACMEAppTests.AppTests_FileManagerSave.FileManager_SaveFile threw exception: >System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
This makes no sense. The assembly is there. So it seems I can't get both applications working at the same time. The errors make no sense to me. There seems to be little information on the PrivateAsset="All" feature for packages. I found this description:
Tip
A private package reference (PrivateAssets="All") means this dependency is local to the current project. For example, if Project A has a build only dependency and Project B depends on A, dotnet restore will not add A's build-only dependencies into Project B.
reference
.Net Command Line Tools
How to resolve this? Figure out why the webapi stopped working (Method not found) or debug the missing assembly reference that isn't, from what I can tell, missing?
All help appreciated.
You can't mix ASP.NET Core 1.1 with EF Core 2.0. They share some common dependencies (e.g. Logging, DependencyInjection, etc.) which means all your Microsoft.* package versions need to more-or-less align.
See the documentation about PrivateAssets. Adding PrivateAssets="All" prevents your data layer's dependencies from propagating into your app. Hence, you get could not load assembly errors.

Could not find 'UserSecretsIdAttribute' on assembly 'ef'

I am using ASP.NET Core 1.1 and I have the following on startup:
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddUserSecrets();
I am using csproj file instead of JSON where I added:
<PropertyGroup>
<UserSecretsId>8844d677-223b-4527-a648-387a65933d55</UserSecretsId>
</PropertyGroup>
But when I run the command:
dotnet ef migrations add "InitialCommit"
I get the error:
An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Could not find 'UserSecretsIdAttribute' on assembly 'ef, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"
Any idea why?
You have run into this issue: https://github.com/aspnet/Configuration/issues/543
The solution is to change your call .AddUserSecrets() to .AddUserSecrets(Assembly assembly)
See this announcement about how deprecating project.json required a breaking change to user secrets: https://github.com/aspnet/Announcements/issues/209
I had this issue also. My solution was to install the nuget package Microsoft.Extensions.Configuration.UserSecrets.
It finally worked for me when I changed a version of package in .csproj:
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
to:
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />

Build csproj for MONO that requires dlls

I have a line of code that I got from a site called Zetcode, and in this to build the first example, I compile the code like this:
gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll 01-simple-cs-example.cs -out:simple-sample.exe
This builds the exe that draws my window, but I don't think my csproj file is correct.
<Project DefaultTargest="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- This Property will make a variable named MSBuildSample, then you can use it to
assign to whatever in the script.
Properties are variables inside the script.
-->
<SrcPath>src\</SrcPath>
<AssemblyName>01-simple-cs-example</AssemblyName>
<OutputPath>bin\</OutputPath>
</PropertyGroup>
<ItemGroup>
<!-- This Compile statement, gathers all the source files listed in an item called Compiled
ItemGroups interact with the source code.
-->
<Compile Include="$(SrcPath)01-simple-cs-example.cs" />
</ItemGroup>
<!-- In the Build target, the Inputs and Outputs targets which looks to see if the files have been updated,
or if the files are existent. If the files have not been changed, then the Build target is skipped.
-->
<Target Name="Build" Inputs="#(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe" >
<!-- The MakeDir directive will create the directory in the property group above, if it meets the
condition stated in the Condition= statement.
-->
<Message Text="Creating the output path $(OutputPath)." />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
<!-- This Csc is the .NET C# compiler, which then uses the ItemGroup of collected sources called, Compile
-->
<Message Text="Compiling the source code." />
<Csc Sources="#(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
</Project>
This is the result from trying to run xbuild, and msbuild.
C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example>xbuild
XBuild Engine Version 3.3.0.0
Mono, Version 3.3.0.0
Copyright (C) Marek Sieradzki 2005-2008, Novell 2008-2011.
Build started 7/25/2014 2:47:41 PM.
__________________________________________________
Project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj" (default target(s)):
Target Build:
Created directory "bin\"
Tool C:\mono\Mono-3.2.3\bin\gmcs.bat execution started with arguments: /out:bin\01-simple-cs-example.exe src\01-simple-cs-example.cs
src\01-simple-cs-example.cs(1,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
src\01-simple-cs-example.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
src\01-simple-cs-example.cs(4,23): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
Task "Csc" execution -- FAILED
Done building target "Build" in project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj".-- FAILED
Done building project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj".-- FAILED
Build FAILED.
Errors:
C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj (default targets) ->
(Build target) ->
src\01-simple-cs-example.cs(1,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
src\01-simple-cs-example.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
src\01-simple-cs-example.cs(4,23): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
0 Warning(s)
3 Error(s)
Time Elapsed 00:00:01.5341534
This is the result from the MSBuild run. MSBuild completed successfully, but I imagine that's because MSBuild knows where to look without me having to specify it in the csproj file.
C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example>MSBuild
Microsoft (R) Build Engine version 4.0.30319.18408
[Microsoft .NET Framework, version 4.0.30319.18444]
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 7/25/2014 2:48:04 PM.
Project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj" on node 1 (default targets).
Build:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /out:bin\01-simple-cs-example.exe src\01-simple-cs-example.cs
Done Building Project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:04.36
I don't know what to put into the csproj file to make it load those DLL's that I used in the command line to compile using gmcs. Any clues?
-- EDIT 2014-08-20 --
After using the answer from knocte, I was able to see how the References are added in a csproj file. Its as simple as adding an item group, and adding the References you listed with using in the C# source file.
<ItemGroup>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Drawing" />
<Reference Include="System" />
</ItemGroup>
This is the manual way to do it if you're building the csproj file manually.
Just use an IDE (MonoDevelop or Visual Studio) to add a GAC-reference to System.Windows.Forms and System.Drawing libraries.

FxCop in MSBuild and disabling CA0060 exceptions

I am very new to MSBuild and it is taking me a little while to work out how to do things.
So I am trying to integrate FxCop into my project to be automatically run when I build them on the build server.
At the moment the way to go seems to be to add a custom task to the build that you call when you build. So I have so far created the following:
<Target Name="ExecuteFxCop">
<ItemGroup>
<Files Include="bin\XXXX.dll" />
</ItemGroup>
<!-- Call the task using a collection of files and all default rules -->
<MSBuild.ExtensionPack.CodeQuality.FxCop
TaskAction="Analyse"
Files="#(Files)"
SearchGac="True"
OutputFile="FxCopReport.xml">
</MSBuild.ExtensionPack.CodeQuality.FxCop>
</Target>
However when I run this >msbuild XXXX.csproj /t:ExecuteFxCop it fails with error 512 which I have narrowed down to an exception from indirectly-referenced assemblys:
<Exception Keyword="CA0060" Kind="Engine" TreatAsWarning="True">
<Type>Microsoft.FxCop.Sdk.FxCopException</Type>
<ExceptionMessage>The indirectly-referenced Silverlight assembly 'System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' could not be found. This assembly is not required for analysis, however, analysis results could be incomplete. Silverlight reference assemblies should be specified with the '/reference' switch. This assembly was referenced by: XXX\bin\ESRI.ArcGIS.Client.dll.</ExceptionMessage>
</Exception>
But I cannot add this reference. Is there a way to get the build to see this reference or preferably just disable the error altogether?
I did try: http://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/c6780439-bc04-459e-80c3-d1712b2f5456/ but it doesn't work
Try the work-around here: http://geekswithblogs.net/blachniet/archive/2011/07/12/avoiding-fxcop-warning-ca0060.aspx
Edit
For example, using the FxCop MsBuild task, set ContinueOnError and check the ExitCode as follows:
<Target Name="ExecuteFxCop">
<ItemGroup>
<Files Include="bin\XXXX.dll" />
</ItemGroup>
<!-- Call the task using a collection of files and all default rules -->
<MSBuild.ExtensionPack.CodeQuality.FxCop
TaskAction="Analyse"
Files="#(Files)"
SearchGac="True"
OutputFile="FxCopReport.xml"
ContinueOnError="WarnAndContinue">
<Output TaskParameter="ExitCode" PropertyName="ExitCode"/>
</MSBuild.ExtensionPack.CodeQuality.FxCop>
<Error Condition="$(ExitCode) != 512" Text="FxCop failed with exit code: $(ExitCode)"/>
</Target>
P.S. (This is not tested)
Not sure if you're still looking for a solution but what usually works for me is adding the
fxcop cmd-option /d:{dir-of-random-assemblies}
which essentially tells fxcop to look in that additional directory for assemblies.
Adding a reference to a proj that doesn't need it is a bad idea in my opinion.
http://msdn.microsoft.com/en-US/library/bb429449(v=vs.80).aspx