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.

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s