Upgrading .csproj File

Historically format of .csproj file has been complex and the developers dread to edit it. The new format is much simpler to edit.

When you want to edit a .csproj file that has the old format, you need to unload the project, edit the project and reload it. As a result, the developer loses the context of his project files and work. Also, the imported packages are stored in two places 1) package.config and 2) .csproj file. So, if you are editing one, you want to make sure that the other file stays in sync.

The new file format provides solutions to these problems. It provides the following advantages:

1. Easier to edit (fat-free) format
The new format is much less cluttered than the classic one. So it is easier to edit and maintain.

2. Simultaneously edit and view
As I mentioned earlier, one can simply right click on the project file and click edit. You need not unload, edit and reload the project. It saves a lot of time and pain.

3. Single package reference

Traditionally, the imported packages are stored in the packages.config file and references to the platform specific library is stored in the project file. Should you need to manually edit somethings, having references in two places is not easy to maintain. The new format has PackageReference in one place.

4. AssemblyInfo in the project
AssemblyInfo is another file that can become redundant. With the new format, contents of the file can be stored in the project file itself.

If you think that converting the good old clunky format to the new format would be a daunting task, it is not. A large project that contained several package files and other references could be converted in a few minutes.
Here are the steps with a sample solution that contains a library, a RESTApi service referencing  NuGet packages, and a unit test project.
  1. For the Library project that does not have any NuGet package reference
  • Right Click and Unload the project
  • Right click and Edit the project file.
  • Replace the top two lines

OldTopTwo

With this line.

NewTop

  • Add the following group with appropriate framework

AddTargetFrameworkGroup

    • Remove all the property groups that reference configuration

RemoveConfigGroups

  • Retain all the groups that refer to reference
  • Remove all item groups that refer to code files. Do not worry, they will be picked up from the folder.

RemoveCodeGroups

  • Remove all the item groups that import targets

RemoveImports

  • With this, the project should be ready in the new format.
  • Open the project and delete the assemblyinfo.cs file.

RemoveAssemblyInfo

  • Now you can view and edit the project at the same time

EditWhileOpen

  1. The Unit Test and REST Api projects require conversion of packages to PackageReference
    1. There are several NuGet Package Reference upgrader extensions available. Install the one that you prefer.
    2. Right click on the packages.config file in the project and choose upgrade/Migrate to PackageReference. This should delete the packages.config file and add appropriate references in the .csproj file
    3. Follow the same steps as in Step 1.
    4. In the ProjectReference entry, you can remove the elements for the project guid and name. The relative path would be sufficient.
  2. More for ASP.NET project
    1. Remove ProjectExtenstions element from the .csproj file
    2. Keep all the content elements
    3. Go to Properties->Debug tab and edit the following settings
      1. Executable as iisexpress
      2. Application arguments as

/path:”$(SolutionDir)$(ProjectName)” /port:12345

Debug

  1. This will add lauchSettings..json to properties folder

Here is the GitHub Repo:

Pre Upgradehttps://github.com/sameerkapps/Upgrade-.csproj/commit/aa934e11f0f0faa0ee8258b19c35b907961ed8bc

Post Upgradehttps://github.com/sameerkapps/Upgrade-.csproj/commit/9a9583fb974a792940b68d411160cb509b60ed5d

Let me know, if you have any questions. Please share it with fellow developers to make their life easier.

Published by: Sameer Khandekar

I am a passionate software engineer who loves to work on Azure microservices, REST API, SDKs, and .NET apps using WPF, Xamarin, and MAUI. The work includes highly scalable geo-distributed services and an authentication library with nearly 500 million downloads. I also had fun integrating with hardware using Bluetooth (BLE). More here: https://www.sameer.blog/about/

Categories csharp, Uncategorized, Visual StudioTags, , 2 Comments

2 thoughts on “Upgrading .csproj File”

  1. I am facing a typical issue, we have recently migrated our selenium project from VSTS to VSCode and hence migrated to .netcore5.0. But since then what i have observed that when we add any file in the project the .csproj file doesn’t add its entry automatically and gets build without any issue and when we push our code to different branches on those branches also its get built smoothly. But earlier it was not like that and used to give errors.
    q1. How we can make sure that csproj entries gets updated after addition of new file?
    q2. Is it going to impact on pipelines on remote machines?

    Like

    1. The new format does not store file names in th .csproj file. The files are picked up from the folder and the subfolders. With that
      >> q1. How we can make sure that csproj entries gets updated after addition of new file?
      You do not need to worry about it. They need not get added. Compiler will pick them up.
      >> q2. Is it going to impact on pipelines on remote machines?
      It should not.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s