aspnet / Tooling

Issue tracker and info on Visual Studio tooling for ASP.NET
Other
256 stars 123 forks source link

Cannot build project after adding reference to a asp.net class library(non core) from a .NET Core Web Application #955

Open TheRealPiotrP opened 7 years ago

TheRealPiotrP commented 7 years ago

Moving https://github.com/dotnet/cli/issues/5248 on behalf of @squid16


Steps to reproduce

Using Visual Studio 2015, update 3, on Windows 10: 1.Create a .NET Core Web Application (WebApplication1)

  1. Modify WebApplication1's package.json to also support .NET 4.6.1. I copied the content of my package.json file after step #7 below.

  2. Use nuget(version 3.5) to create a local nuget package for a ASP.NET class library (target frame work: .NET Framework 4.6.1), named XXX

  3. In Visual Studio 2015, select Tools--> Nuget Package Manager-->Package Manager Settings. Select Package Sources. Then browse to the local directory where the XXX library was pushed in step 3.

  4. In Visual Studio 2015, WebApplication1, Right click on References, select "Manage Nuget Packages". Under Package Sources, select the package source created in step 4, then select the XXX library. Click Install

  5. Now in Visual Studio 2015, WebApplication1, expanding References, I could see my newly added library listed under "NET Framework 4.6.1"(please see screenshot attached), in addition to the original ".NETCoreApp,Version=v1.0" reference (please see the attached image) corereferencenoncore1

  6. Going back to the code for WebApplication1, I was able to see XXX library with all the intelligences for all of my classes and methods. I was able to create an instance of my library and call its methods. Please note that as I moved my mouse over "Using XXX" in my class, I got a warning(please see the attached image) saying WebApplication1..NET Framework 4.6.1 - Available WebApplication1..NETCoreApp,Version=v1.0-Not Available corereferencenoncore2 It looks like I successfully added the reference to the non-core XXX library from my core Web Application, but when building the core Web Application, the added XXX library could not be seen. I’m not sure what is missing. Please help me out! Thanks for your time!

Below is my package.json:

{ "dependencies": { "Microsoft.ApplicationInsights.AspNetCore": "1.0.0", "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview2-final", "type": "build" }, "Microsoft.AspNetCore.Routing": "1.0.1", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Logging": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0" },

"tools": { "BundlerMinifier.Core": "2.0.238", "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final", "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" },

"frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ], "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0", "type": "platform" } } }, "net461": { "dependencies": { "TCShipping": "1.0.0" } } },

"buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true },

"runtimeOptions": { "configProperties": { "System.GC.Server": true } },

"publishOptions": { "include": [ "wwwroot", "*/.cshtml", "appsettings.json", "web.config" ] },

"scripts": { "prepublish": [ "bower install", "dotnet bundle" ], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }

Expected behavior

To be able to build the Web Application project with the newly added library

Actual behavior

As I build WebApplication1, I see errors on all the lines that i used references to my XXX library:

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?) WebApplication1..NETCoreApp,Version=v1.0 C:\qs\proj\test\VS2015\WebApplication1\src\WebApplication1\Program.cs 6 Active where XXX is the name of my ASP.NET library.

Environment data

dotnet --info output: Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\squid>dotnet --info .NET Command Line Tools (1.0.0-preview4-004233) Product Information: Version: 1.0.0-preview4-004233 Commit SHA-1 hash: 8cec61c6f7

Runtime Environment: OS Name: Windows OS Version: 10.0.14393 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\1.0.0-preview4-004233

C:\Users\squid>

mlorbetske commented 7 years ago

@squid16 It seems to be that since the package is marked as .NET Framework 4.6.1 that it's only referenced in the net461 build. Since the code isn't conditional (there's not a #if NET461 around the places where XXX library is being used), the netcoreapp1.0 compilation is failing because it's encountering a namespace it can't resolve.

In the upper left corner of the C# window where you're using the library, there should be a dropdown that indicates which framework to get intellisense for, if you choose the netcoreapp1.0 option, the places where this package is being used should display the same errors you're seeing at build time - I believe the .NET Framework 4.6.1 option is currently selected, as you're getting intellisense for things in your package.

To work around this, if the library is also being built for netcoreapp1.0, you should be able to do dotnet pack on the library at the command line to produce both the .NET 4.6.1 and netcoreapp1.0 flavors and put them in a single NuGet package. You can then reference this package in the non-framework specific dependencies section of the project.json file, allowing you to use the library without conditional compilation. If it truly is a .NET 4.6.1 only library, you'll need to use conditional compilation (as mentioned earlier) to make those sections of your code where you're using it specific to the .NET 4.6.1 build.