Tuesday, January 3, 2017

.NET Core : In Project.json Having Dependencies Section Global Vs. Nested Under Frameworks Section

If you are on Visual Studio 2015 and if you are working on .NET Core related projects, I am sure you are familiar with the project.json file.

In this post let’s see the difference between having dependencies section global Vs. nested  under frameworks section in the project.json. And that’s basically the difference between following.
"frameworks": {
  "netcoreapp1.1": {
  }
},
 
"dependencies": {
  "Microsoft.NETCore.App": {
    "version": "1.1.0"
  }
}
Vs.
"frameworks": {
  "netcoreapp1.1": {
    "dependencies": {
      "Microsoft.NETCore.App": {
        "version": "1.1.0"
      }
    }
  }
}
Here in the second,  dependencies section is nested under frameworks section. In both of these scenarios packages are getting restored without any errors. So are these the same, if not, what is the difference?

There is a significant difference between these two approaches. To give you all a better explanation, let’s change the first example, adding another framework which the application will support, say .NET Framework 4.5.2.
"frameworks": {
  "netcoreapp1.1": {
  },
  "net452": {
  }
},
 
"dependencies": {
  "Microsoft.NETCore.App": {
    "version": "1.1.0"
  }
}
With this change, packages are not going to get restored any more, it will throw you an error “The dependency Microsoft.NETCore.App 1.1.0 does not support framework .NETFramework,Version=v4.5.2”.

So what is happening here is when you defined dependencies as global, that means all those listed should able to be used with all the different frameworks your application supports. In this case, Microsoft.NETCore.App does not support .NET Full Framework as it contains APIs that targets .NET Core.

So the best practice is, if your application supports multiple frameworks, list the common dependencies in global dependencies section and if you have framework specific dependencies, list them inside dependencies section under frameworks section. For instance that would be something like below,
"frameworks": {
  "netcoreapp1.1": {
    "dependencies": {
      "Microsoft.NETCore.App": {
        "version": "1.1.0"
      }
    }
  },
  "net452": {
  }
},
 
"dependencies": {
  "Newtonsoft.Json": "9.0.1"
}
If your application supports only one framework, you can either list them under global dependencies section or list them inside dependencies section under frameworks section as both are fine.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment