The new csproj is lightweight, you don’t have to unload the project to edit the csproj file and you can add package references directly to the csproj file, save it and they will be restored in your assembly automatically. Also, alleviates a lot of pain that’s inherent with Nuget package hell in the old csproj and packages.config style of doing things.
Overall, no reason not to migrate. The way to do it is as follows (for WebJobs and class library type projects):
- Right click the packages.config and click the option to migrate to the new PackageReferences style. This will move all the package references in your csproj file and get rid of the packages.config file.
- Right click on the project -> Unload Project -> Edit {project-name}.csproj. Change the XML to look like the structure below i.e. remove all but the XML sections shown in the gist below. You’ll need to keep all your package references:
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net462</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Azure.WebJobs"> | |
<Version>1.1.2</Version> | |
</PackageReference> | |
<!-- List all your package references here --> | |
</ItemGroup> | |
<!--List all your project reference--> | |
<ItemGroup> | |
<ProjectReference Include="path\to\proj.csproj"> | |
<Project>{7EDAEA1C-53B8-41E4-8253-DDC5EFD83999}</Project> | |
<Name><!--referenced project name--></Name> | |
</ProjectReference> | |
</ItemGroup> | |
</Project> |
- Save, reload and build the project. You will get a few build errors to do with AssemblyInfo.cs file just open that file and remove these lines:
[assembly: AssemblyTitle("{project name}")] | |
[assembly: AssemblyConfiguration("")] | |
[assembly: AssemblyCompany("")] | |
[assembly: AssemblyProduct("{project name}")] | |
// | |
[assembly: AssemblyVersion("1.0.0.0")] | |
[assembly: AssemblyFileVersion("1.0.0.0")] |
Rebuild! You’ve successfully upgraded the old csproj to the new csproj format with PackageReferences!
Just to confirm, right click the project and you should see “Edit {projectname}.csproj” in the menu which you couldn’t with the old format unless you unloaded the project first.
NB: I would recommend doing this in a source controlled environment so that you can revert changes if something does go wrong with this migration. I haven’t tried this migration with all the project types so you might need to do additional research for your needs and then please do let me know.
NB: You might find that in CI environments the project might fail to build because it fails to restore packages using “nuget restore”, to solve this you’re going to have to use “dotnet restore” instead in your build scripts.
One Reply to “Upgrading Old csproj to New csproj in Visual Studio 2017”