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.
- 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
With this line.
- Add the following group with appropriate framework
-
- Remove all the property groups that reference configuration
- 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.
- Remove all the item groups that import targets
- With this, the project should be ready in the new format.
- Open the project and delete the assemblyinfo.cs file.
- Now you can view and edit the project at the same time
- The Unit Test and REST Api projects require conversion of packages to PackageReference
- There are several NuGet Package Reference upgrader extensions available. Install the one that you prefer.
- 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
- Follow the same steps as in Step 1.
- In the ProjectReference entry, you can remove the elements for the project guid and name. The relative path would be sufficient.
- More for ASP.NET project
- Remove ProjectExtenstions element from the .csproj file
- Keep all the content elements
- Go to Properties->Debug tab and edit the following settings
- Executable as iisexpress
- Application arguments as
/path:”$(SolutionDir)$(ProjectName)” /port:12345
- This will add lauchSettings..json to properties folder
Here is the GitHub Repo:
Pre Upgrade – https://github.com/sameerkapps/Upgrade-.csproj/commit/aa934e11f0f0faa0ee8258b19c35b907961ed8bc
Post Upgrade – https://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.
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?
LikeLike
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.
LikeLike