10

I'm currently using ASP.NET Core MVC RC2 which is suppose to support referencing a full framework library Project from a Core MVC Web Application Project. But I can't seem to make it work.

I have an existing full framework library project that I'd like to use with a new Core MVC Web application project. But when I try to add a reference to the library's project I get an error that says the target framework for it is incompatible with the target framework for the web application.

To try to simplify the problem I created a new Core MVC web application and a new Windows Code Library and tried to add a reference from the web application project to the library's project and got the same error message. I've searched on stack overflow and I did find this question that seemed highly relevent:How to make ASP.NET Core RC2 app build on net46 framework with older third party dependencies but I was unable to use the information there to solve my issue.

Here are the steps to reproduce the issue:
I created a solution with a ".NET Core" "ASP.NET Core Web Application" project based on the "Web Application" template in Visual Studio 2015.

[pic 1

[pic 2

I then added a new "Windows" "Class Library" project to the solution.

enter image description here

So at this point the solution looks like this:

pic 4

Now, when I try to use visual studio to add reference from the Core MVC Web Application to the Full Framework library I get an error that says that "ExampleLib has a target frameworks that are incompatible with the targets in current project ExampleWebApplicaiton.":

enter image description here

This error seems fairly self explanatory and seems to want me to change the framework that the web application is targeting so that it's the same as the library, which makes perfect sense. The problem is that I can't find documentation on how to do that and intellisense isn't being particularly helpful.

I suspect I need to change one of the following pieces of the project.json file for the web application:

"dependencies": {
    "Microsoft.NETCore.App": {
     "version": "1.0.0-rc2-3002702",
     "type": "platform"
   },

or

 "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

How do I specify .NETFramework Version 4.6 for the Core MVC Web Application instead of .NETCoreApp?

2 Answers 2

5

There is a template that allows you to target the full .NET framework - sorry, can't post an image yet, but you can go to: Templates -> Visual C# -> Web -> ASP.NET Core Web Application (.NET Framework)

Note that you can specify the version of the .NET Framework from the dropdown above (as per usual).

This will change your project.json to:

"frameworks": {
"net461": { }
}

(depending on the framework version that you've selected). You will now be able to add a reference to your class library.

"frameworks": {
    "net461": {
    "dependencies": {
    "ClassLibrary1": {
        "target": "project"
        }
     }
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've been fleshing out an approach with someone at Microsoft named abpiskunov in this thread github.com/aspnet/Tooling/issues/45 but I like your approach better if I can get it to work. When I select the .NET Framework 4.5.1 in the drop down list for the New Project dialog and select ASP.NET Core Web App the project.json seems to ignore it and contains "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net45+win8" ] } },
But if I manually edit the project.json frameworks section to include "net461" AND I remove `"Microsoft.NETCore.App" from the dependencies section at the top of the file, then manually restore packages, then use Visual Studio to add my project reference, THEN manually restore packages, I can get it to work. Theoretically the last manually restoring of packages is not required but in my setup it is for some reason that is still unclear.
One more note, for me, using this approach to reference a Windows Class Library Project only works if the project was created with my Visual Studio 2015 Update 2 using RC2 tooling, it didn't work for a Windows Class Library Project created years ago. The workaround? Create a new Windows Class Library Project, add your existing code files into it and now VS will allow a reference to it using the steps in the answer above and my comments. Cheers!
I did manage to create a test application that targets .NET 4.5.1 successfully. Are you sure that you selected the correct template when you created the app - it should be "ASP.NET Core Web Application (.NET Framework)" and NOT "ASP.NET Core Web Application (.NET Core)". As far as adding class library references go: Adding a class library with a simple name seems to work correctly (e.g. ClassLibrary1). I did experience some problems with class libraries where the csproj and assembly name is a little more complex and doesn't match, e.g. "A.B.csproj" and "Company.Product.A.B.dll".
That's a GREAT POINT. I created my Web Application with the "ASP.NET Core Web Application (.NET Core)" template and didn't realize the other template existed since I found my template under ".NET Core" templates. I now see that the "ASP.NET Core Web Application (.NET Framework)" template under "Web" templates in Visual Studio would have been a much better choice and made the manual editing of the project.json unnecessary. This is the way to go for new web applications that need to link to Windows Class Libraries. Sweet!
1

Following Ron C's steps here's the complete project.json for a web api project I just tested and it worked.

{
  "dependencies": {
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "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"
  },

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

  "frameworks": {
    "net461": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ],
      "frameworkAssemblies": {
        "System.Data": "4.0.0.0"
      }
    }
  },

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

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

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

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.