Referencing class library in ASP.NET 5 - dependency issues - asp.net

My custom class library (targeting .NET 4.5.1) references Newtonsoft.Json 8.0.2 (the latest version to date). I've added this class library as a reference to a new ASP.NET 5 Web API project.
From the Error List output:
Error NU1002 The dependency Newtonsoft.Json 1.0.0 in project MyProject does not support framework DNXCore,Version=v5.0.
That part seems somewhat self-explanatory, however, I couldn't understand why it was changing my referenced version to 1.0.0.
From the References --> DNX 4.5.1 --> MyClassLibrary Node:
NU1007 Dependency specified was Newtonsoft.Json >= 8.0.2 but ended up with Newtonsoft.Json 1.0.0.
I've tried removing/re-adding the references several times, all to no avail. I did notice that several of the ASP.NET libraries use Newtonsoft.Json 6.0.6 -- I'm not sure if this has anything to do with it.
If I navigate to the wrap folder --> Newtonsoft.Json --> json file, and replace "Newtonsoft.Json 1.0.0-*" with "Newtonsoft.Json 8.0.2-*", it fixes the references for the DNX 4.5.1 node.
However, errors still show afterwards saying that Newtonsoft.Json 8.0.2 referenced in my class library does not support DNXCore 50.
I'm at a loss as to what to do. Any help would be greatly appreciated.

Answer to my issue:
Thanks to Oleg's response I was able to make it work -- however there were two hurdles. One was the "wrap" folder that was created for Newtonsoft.Json. In the root of my solution was a "wrap" folder that contained a folder called Newtonsoft.Json. Apparently(?) that folder shouldn't have been created (how it was created I have no idea). Once removing that, I was able to add a reference to my custom class library without any errors referencing Newtonsoft.Json.
However when I began trying to use my custom assembly as in code, I would get an error about DNX50 not being supported -- which sort've makes sense because my custom referenced assembly was a .NET 4.5.1 class library.
This GitHub link has many people experiencing a similar issue
However, I managed to get everything to build and errors to go away by removing targeting for DNXCore50. This worked for me because I was just wanting to share some POCO data models between a Web API and WPF project. I'm not interested in targeting DNXCore at the moment.
I'm looking forward to the future of ASP.NET, but the newness is a bit challenging, at least for me.

Related

UWP - Cannot resolve Assembly or Windows Metadata file

Everything was rolling along smoothly until a few days ago when UWP all of a sudden stopped building after pulling a new version from VSTS (git) with the errors:
Cannot resolve Assembly or Windows Metadata file 'Type universe cannot resolve assembly: X.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.'
Could not copy the file "obj\x86\Debug\MainPage.xbf" because it was not found.
Could not copy the file "obj\x86\Debug\App.xbf" because it was not found.
Could not copy the file "obj\x86\Debug\X.Mobile.UWP.xr.xml" because it was not found.
I have a solution structure of the following:
X.Core (.NET Standard class library)
X.Mobile (.NET Standard PCL)
X.Mobile.UWP (UWP specific project)
UWP references Mobile, and Mobile references Core (Core is also referenced by a web API project).
The commit that I pulled from source control did not have any changes to the X.Mobile.UWP .csproj file.
Things I have tried:
The obligatory clean and rebuild.
Delete all obj and bin folders for the entire solution.
Remove and re-add all references in the .UWP project.
Upgrade Xamarin.Forms to the latest stable (3.1.0.637273).
Remove and re-add X.Core reference in the X.Mobile project.
Delete C:\Users\%username%.nuget folder.
Update Microsoft.NETCore.UniversalWindowsPlatform to the latest stable (6.1.5).
Change the target version to all available versions - we've been running on build 16299 for several months.
And I've been beating my head against this problem on and off for days now. Android and iOS projects build just fine, which is ironic considering UWP has been our most stable platform. Anyone have any insight?
EDIT:
After adding a reference to X.Core directly to the X.Mobile.UWP project, I can compile. This shouldn't be the answer though since UWP never directly references Core.
I found the solution.
I had the very same problem because I had added a new (.Net Standard 2.0 Class Library) project into my Xamarin.Forms solution.
In short, initially my solution included the following projects:
BackgroundTaskTest (which have all my Views and ViewModels)
BackgroundTaskTest.Android
BackgroundTaskTest.iOS
BackgroundTaskTest.UWP
Suddenly I decided to add a class library named "BackgroundTaskTest.Common" in the same solution folder:
BackgroundTaskTest.Common (new one)
BackgroundTaskTest (which have all my Views and ViewModels)
BackgroundTaskTest.Android
BackgroundTaskTest.iOS
BackgroundTaskTest.UWP
The Android was working fine with it but the UWP project didn't like that new neighbor (which in your case is named "X.Core"). So I moved my classes from that new project to the "BackgroundTaskTest" again and deleted "BackgroundTaskTest.Common" from my solution with all the references and it has started working.
To finalize and make it short, please compare your X.Mobile.csproj file with X.Mobile.Core.csproj file. you will find the issue in the differences. Plus try to check all your dependencies to the X.Core project to make sure they are the same.
I installed NuGet package "NETStandard.Library" and added a reference to all my .Net Standard libraries to my UWP project. This solved the problem for me!

References in .NET Core (2.0)

I've tried to add a new reference to my .NET Core project.The strange thing is that I can access also the projects that are involved in my reference. For this example, i should be able to see the Repository project from service, but should not be able to access Entity Project.However , I can still access the entities object from Service.
How comes ?
References in SDK-based projects are fully transitive so - similar to many other package managers like npm or maven - you all the transitive references are available in the project to make sure the app compiles and runs cleanly, e.g. there are no unresolved references when the dependency is referenced and all assemblies are part of the build output and ready to run. (there may even be conflict resolution applied to conflicting version of assemblies resulting in the generation of binding redirects.)
In previous versions, you would need to install NuGet packages or add additional project references to other projects as well to not get build errors or type load exceptions.
Currently there is no perfect workaround if you want your project to do all the things needed to be able to run and resolve conflicts correctly but not pass transitive references to the compiler.
If you only need a dependency to build a project, but not to run it, you can mark a package or project reference as PrivateAssets="All" (add as attribute to the reference in the .csproj file).
If you want to enforce API usage - e.g. for layered APIs, consider writing a roslyn analyzer that will emit warnings if you reference APIs from places you don't want to. this may be suitable for large projects where tooling is needed to maintain the desired architecture.

ASP.NET 5 - Referencing files in projects (missing csproj-like references)

I'm developing my ASP.NET 5 app using Visual Studio 2015. There is no problem at all since RC1 with working with multiple projects in one solution, and so on..
But for a few days I have to work with VS Code and I'm not sure how to solve missing classes problem. Let's say I have solution with project A and project B. They have it's own classes inside. In previous versions of ASP.NET all files have been referenced in .csproj file, but now, my .xproj files are nearly empty. There are no direct paths to files. And in my project A I've created new classes, they have all correct namespaces, project B has a reference to A (even my controller in project B uses classes from same A's namespace!), but there is a compiltion error which says that my classes cannot be found in project A :/
I thought that with VS Code I have to add some kind of references in project, to notify compiler about that new files,but I cannot find a correct place to do it. Any ideas?
You do this in the project.json file.
{
"dependencies": {
"ProjectA": "1.0.0-*"
}
}
You declare the dependency. If the source is available in the solution folder, it will find it and use it. If the source isn't available, it can pull down a NuGet package that matches the version number. The documentation I linked to above provides more information about this process.
Once you have a dependency registered, you can refer to the public classes in your project. You'll need to fully qualify the dependencies (use the full namespace) or add a using <namespace> directive at the top of your file.

VS2015 add reference for Class Library

I have created a project in VS2015, structure as below:
Solution1
BookStore.ClassLibrary1 => Class Library (Package)
BookStore.ClassLibrary2 => Class Library
BookStore.Web => MVC5
In BookStore.Web, I can reference BookStore.ClassLibrary2, but fail to reference BookStore.ClassLibrary1.
It shows an error "A reference to 'ClassLibrary1' could not be added."
My question is how to reference a Class Library (Package) in VS2015? Thank you so much!
Looks like your ClassLibrary1 project is a Class Library Package, not a class library project. Class Library Package is used to create Nuget packages that can target any platform.
There are a number of benefits of ASP.NET 5 Class Library projects (.kproj) over Class Library projects (.csproj):
ASP.NET 5 class libraries easily support cross-compiling projects to multiple targets, such as aspnet50, aspnetcore50, net45, and various other portable class library variations. This includes rich Visual Studio support for Intellisense to notify you which APIs are available for which targets.
NuGet packages are automatically created, which is an extremely common thing to do with class libraries.
Better productivity when it comes to things like automatically refreshing Solution Explorer when the file system changes. Fewer conflicts in source control when trying to merge conflicting changes in the *.csproj file.
Can be compiled cross-platform (in part because it doesn't depend on MSBuild)
You can reference a *.csproj project from a *.kproj project (this was just made a lot easier with the new preview of Visual Studio 2015), but it was always possible with some manual steps.
Why does the name have "ASP.NET" in it?
As far as the names goes, it's a relic of history that will soon be addressed. The new project type is useful far beyond ASP.NET 5 applications. Expect to see new names in a future preview of Visual Studio:
.NET Console Application (Cross-platform)
.NET Class Library (Cross-platform)
With the release of Visual Studio 2015 RC you can see the updated project template names:
Class Library (Package)
Console Application (Package)
These use the project.json file and the .NET Execution Environment (DNX) to build, run, and package (into a NuGet package) the project.
These project templates continue to show up in the New Project dialog under the "Web" node, but now also show up in the main "Visual C#" node as well.
Here is a good link as you need to referance a dll that the new clas library does not build. https://evolpin.wordpress.com/2015/01/25/vnext-and-class-libraries/
Either use a plain old class library or use a Nuget class library, publish it to a local or public Nuget repo and add it to the web project from there.
I doubt about dependency of ClassLibrary1 and I can even see that in your screenshot,
It seems ClassLibrary1 is looking for some dependent dlls, so you might need to add those dll first then you can go ahead and add it.
Something similar happens here too
More details about Depencies can gather from this MSDN link you can directly jump to Dependencies node for Bower and NPM dependencies

Can not find system.web.http

I have to add a system.web.http assembly reference because I added the HttpConfiguration class to my unit test class library project.
When I browse the Add reference dialog I can not find the system.web.http assembly.
The class library project has the .Net 4.5.1 framework targeted.
in order to get the system.web.http you will need to install a nuget package
in the nuget command line:
Install-Package Microsoft.AspNet.WebApi.Core
https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Core/
I've had multiple errors regarding missing references such as "system.web.http" or "system.web.razor" in my project. Even though I do have all the references installed through nuget.
What worked for me was to install a standalone version of AspNetMVC4, even though Visual Studio 2014 installed that as well. Apparently it was missing some references in Windows. I've installed this from the Microsoft website, and all went well afterwards.
Hope this helps someone. It drove me crazy for like 2 days, nothing regarding nuget worked for me, I've deleted and reinstalled the references a couple times, did everything I've found in multiple posts on stackoverflow, nothing worked.
Url for mvc4 standalone setup from microsoft: https://www.microsoft.com/en-us/download/confirmation.aspx?id=30683
For every one haveing this problem. Remember that you might have a downgraded version of based on your framework. Try look on your packagde folder what version you have. Then upade that version. Dont use Update-Package it will update all version with no respect to the framework your running at all.
It was solved by commenting out the "bindingRedirect" part.
bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/

Resources