Skip to main content

Some cool Project.json features with ASP.NET Core

As of today, Project.json is the way to define your dependencies, managing runtime frameworks, compilation settings, adding scripts to execute at different events (Prebuild, Postbuild etc.) for ASP.NET Core projects. Though, it will be no longer available in future releases of ASP.NET Core. But since it is available till now, and I used couple of features which I found useful. So sharing those cool Project.json features with ASP.NET Core that you may also find helpful.

Cool Project.json features with ASP.NET Core

  • warningsAsErrors
  • Most of the time, we concentrate only on fixing build error, and ignore build warnings. It’s not a good practice as to ignore them.

    Varible Declared but not used Warning

    The best way to fix those warnings is to convert them into error, so you had to fix them to proceed. So in project.json, there is an option warningsAsErrors under buildOptions. Set value to “true” for this option.

    "buildOptions": {
      "warningsAsErrors": true
    },
    

    And now when you build the application, warning is now an error.
    WarningAsErrors

  • nowarn
  • As warningsAsErrors when set to “true”, will convert warnings into errors, but sometimes it is not required. When you are sure about any particular warning that will not create any problem in the future like, the warning The variable ‘var’ is assigned but its value is never used. So you want to ignore some of the warnings, while continue to convert all other warnings as errors.

    Open Project.json and add nowarn option under buildOptions. You can define a comma separated list of error codes.

     "buildOptions": {
       "warningsAsErrors": true,
       "nowarn": ["CS0168"]
      },
    

  • xmlDoc
  • It’s a best practice to put comments in your code but lots of developers don’t follow this. Though you can’t force developers to put comments in code, but you can definitely force for putting xml documentation comments (using a triple slash). If there are no XML comments present, Visual studio will display green squiggly lines in the code and also display warning Missing XML comment build warning.

    To force XML comments then, open Project.json and add xmlDoc option under buildOptions. Set the value to true to make XML comments compulsory.

    "buildOptions": {
      "warningsAsErrors": true,
      "xmlDoc": true
    },
    

    XMLDoc Error

  • outputName
  • By default, .NET core output file (.dll, .pdb,.json) name is based on the project name. If you wish to change it to any other name of your choice, then use outputName option in Project.json to control the name of output file.

     "buildOptions": {
        "warningsAsErrors": true,
        "xmlDoc": false,
        "outputName": "MyApp"
      },
    

    Project json outputName

  • entryPoint
  • As an ASP.NET Core application is a true console application, therefore it has Main() method as entry point of application. If you wish to change the entry point of the application, then open Project.json and add entryPoint option in the beginning. Here you need to define name of the method which you want to make as starting point.

    {
       "entryPoint": "ProjectJsonSettings.Program.MyMainMethod",
    }
    

    However, this option is not working. When application is built after this change, an error comes “Program does not contain a static ‘Main’ method suitable for an entry point”. So the changes made to project.json are not reflected. Let me know in comments section, if you are able to make it work.

    Hope you find them useful. You can find list of all options in Project.json here.

    Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

    PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

    Leave a Reply

    Your email address will not be published. Required fields are marked *