20

I was playing around with VS2015 and ASP.NET vNext, and got stuck on trying to add a reference from vNext class library (kproj) to a regular class library (csproj) in the same solution. Visual Studio 2015 shows the following error message:

"The following projects are not supported as references".

Is it possible at all to add references to csproj from vNext class libraries?

4 Answers 4

24

Note: The kpm command has been replaced by dnu.

Visual Studio 2015 Preview (as of writing this) comes with the ASP.NET 5 stable release beta1. In this version there is no way to reference a csproj project from an ASP.NET 5 project.

However, on the development feed of ASP.NET 5 the command kpm wrap was introduced to support referencing csproj-projects from ASP.NET 5 projects. See the github issue #827 in the aspnet/KRuntime repository and pull request #875 which closes the issue.

Here is an example how you would use kpm wrap:

Make sure the newest version of the KRuntime is installed (check this with the kvm list command) (I tested this with version 1.0.0-beta2-10709).

Create an ASP.NET 5 class library project, I used the name ClassLibrary1.

Create a "normal" csproj class library, I named this ClassLibrary2 (make sure you put this in the src folder).

From the commandline, from the solutiondirectory run the command

kpm wrap .\src\ClassLibrary2

This gives the output:

Wrapping project 'ClassLibrary2' for '.NETFramework,Version=v4.5' Source C:\Users\andersns\Source\ClassLibrary1\src\ClassLibrary2\ClassLibrary2.csproj Target C:\Users\andersns\Source\ClassLibrary1\wrap\ClassLibrary2\project.json Adding bin paths for '.NETFramework,Version=v4.5' Assembly: ../../src/ClassLibrary2/obj/debug/ClassLibrary2.dll Pdb: ../../src/ClassLibrary2/obj/debug/ClassLibrary2.pdb 

Now in the project.json of ClassLibrary1 (which is ASP.NET 5) you can add a reference to ClassLibrary2 with this:

... "dependencies": { "ClassLibrary2": "" }, ... 

Note: kpm wrap did not run properly for me with cmd, I needed to launch powershell to make it run.

Sign up to request clarification or add additional context in comments.

11 Comments

Man @AndersNS, you are bleeding edge :). We're still fixing issues with it and also working on integrating this into visual studio Add Reference dialog.
PS > kpm wrap System.Exception: TODO: Error: unrecognized argument 'wrap' Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.HandleUnexpectedArg(CommandLineApplication co mmand, String[] args, Int32 index, String argTypeName) Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args) Microsoft.Framework.PackageManager.Program.Main(String[] args)
How upgrade to 1.0.0-beta2-10709 ? PS > kvm upgrade Determining latest version KRE-CLR-x86.1.0.0-beta1 already installed. Adding C:\Users\user\.kre\packages\KRE-CLR-x86.1.0.0-beta1\bin to process PATH Adding C:\Users\user\.kre\packages\KRE-CLR-x86.1.0.0-beta1\bin to user PATH Updating alias 'default' to 'KRE-CLR-x86.1.0.0-beta1'
Will the tooling eventually support the reverse scenario to let older class libraries reference ASP.NET 5 libraries if, say, they also target net45 in addition to the aspnet50x frameworks? I know this could be done somewhat more manually, but it would be nice to be able to have everything play together in mixed solutions when possible.
@Adam not sure how that would work given how the net45 target framework works vs how aspnet5 works. Maybe in the future? Start another question with that maybe, interesting question. :)
|
11

Starting with (Visual Studio 2015 RC) the kpm command has been replaced by dnu

The dnu command stands for (.NET Development Utility)

dnu wrap .\src\ClassLibrary2\ClassLibrary2.csproj 

New ASP.NET Features and Fixes in Visual Studio 2015 RC http://blogs.msdn.com/b/webdev/archive/2015/04/29/new-asp-net-features-and-fixes-in-visual-studio-2015-rc.aspx

Comments

3

I have found it easiest to simply create a corresponding .kproj for the .csproj I want to reference. The .kproj does not require listing every included file, so this is rather straightforward.

You can create YourProject.kproj as a text file with the contents below, and only replace the [REPLACE_WITH_UNIQUE_GUID] and [ROOT_NAMESPACE].

<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> </PropertyGroup> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" /> <PropertyGroup Label="Globals"> <ProjectGuid>[REPLACE_WITH_UNIQUE_GUID]</ProjectGuid> <RootNamespace>[ROOT_NAMESPACE]</RootNamespace> <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> <OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> </PropertyGroup> <PropertyGroup> <SchemaVersion>2.0</SchemaVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <ProduceOutputsOnBuild>True</ProduceOutputsOnBuild> </PropertyGroup> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> </Project> 

You can then add this project to your solution and reference it from your MVC 6 project.

Comments

0

In addition to this answer I found out that you need to use the if directive (#if) to make the call without errors:

Something like:

#if ASPNET50 using class2 #endif 

When you use it in a call you need to do the same.

#if ASPNET50 ViewBag.Message = class2.Class1.Greetings() #endif 

1 Comment

Update: Please check stackoverflow.com/questions/27537496/…. If you remove in the project.json the framework ASPNET50CORE you do not need to use that ugly directive :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.