Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding .net 4.5 reference to Asp.net vNext project

I'm trying to add .net 45 reference to a asp.net 5 starter web project..and i'm getting this errors.enter image description here

like image 279
PICTD.RUFY Avatar asked Sep 29 '22 17:09

PICTD.RUFY


1 Answers

While it is true that you can target multiple frameworks in a way similar to how you've done in your example, I think you will probably not be able to get the StarterWeb project to run under .NET 4.5 without some significant changes.

When dealing with multiple frameworks, Visual Studio 2015 will actually provide a lot of help if you hover the mouse over the code with errors:

Visual Studio 2015 CTP 5 showing net45 has a problem with the shared code

One obvious difficulty in getting the StarterWeb project working is that between MVC 5 and 6, a number of pieces have been moved into new namespaces and broken up into different assemblies. Most notably, the MVC 5 package depends on System.Web, whereas the 6.0 beta that is used by the StarterWeb project does not. With the MVC libraries moved, the Authorize attribute is now in Microsoft.Aspnet.Mvc, whereas in earlier versions it belonged to System.Web.Mvc.

While in theory you could probably target different versions of MVC across the frameworks, in practice that would probably not be worth the trouble. Even if you find Authorize, there's no guarantee that it will be the "same" one, even if it compiles.

If that is a route you want to go (or just when targeting multiple frameworks in general), you have control over which packages are used by which framework in the project.json file.

If packages are compatible with all of the targeted frameworks, you can leave them in the parent dependencies area of the project.json file, but when they are specific to a given framework, you need to add a child dependencies section to that particular framework's configuration. In the case of .NET 4.5, you can also add familiar framework assemblies (as opposed to NuGet packages) by adding a frameworkAssemblies section.

You will end up with something like this:

"dependencies": {
    "SomeCommonPackage":"1.0.0"
},
"frameworks": {
 "aspnet50":{
    "dependencies":{
       "Aspnet50SpecificPackage":"1.0.0"
    },
  "net45":{
    "dependencies":{
       "NET45SpecificPackage":"1.0.0"
    },
    "frameworkAssemblies":{
       "System.Web":"4.0.0.0"
    }

When you target these different frameworks, you will often find that at least some code will have to framework-specific. To handle this, you can add compiler directives for the various using statements and the actual code that depends on packages only available in one framework or another.

For example, you may end up with areas that look something like this:

SomeType result;

#if ASPNET50
   result = SomeMethodNotAvailableIn45( );
#endif

#if NET45
   result = EquivalentMethodIn45( );
#endif

Obviously that is overly simplistic, but it gives you the basic formula:

  • Put shared dependencies in the root dependencies section
  • Put dependencies specific to a given framework in its own dependencies section
  • Use compiler directives for framework-specific code that can't be shared

I also would recommend you take a look at Rick Strahl's excellent blog post to see a great walk-through of targeting multiple frameworks with more detail and lots of screenshots. One nice feature of the new project system is that it can easily create a NuGet package for all of the frameworks you choose to target, and he goes into more detail about that as well.

like image 72
Adam Avatar answered Oct 06 '22 01:10

Adam