In my previous post, I posted about Some cool Project.json features with ASP.NET Core and also mentioned about announcement made by Microsoft in May 2016 that Project.json will be going away so as .xproj and .csproj will make a comeback for .NET Core. This change was supposed to come out after tooling preview 2 release and in one of recent nightly build release of .NET core, this change is introduced. So bye-bye Project.json and .xproj and welcome back .csproj.
Bye-Bye Project.json and .xproj and welcome back .csproj
I personally liked Project.json idea to manage dependencies, framework and managing pre, post build and publish events as JSON is far easy to handle than XML. Sadly, it is going to become history now. To see this change in action, you have to download .NET Core nightly build from here. At the time of writing this post, this change was available in the nightly build only. So download the .NET Core installer based on your platform and install it. If you are new to .NET Core, then read How to Install ASP.NET Core And Create Your First Application
Once the installation is over, go to command prompt and run following command to ensure the latest version of tooling is installed or not.
dotnet --version
And you should see following.
The dotnet tooling version number may be different on your system, as this is a nightly build and likely to be replaced with new build. So in my system, the tooling version is “1.0.0-preview4-004175”. Please keep in mind, this is a nightly build and there would be issues. Let’s create an .NET Core application.
dotnet new dotnet restore dotnet run
So the application is running successfully. And following is the screenshot of the folder.
As you can see, there is no project.json and .xproj. And this app is successfully getting opened in VS 2015 and VS Code. If you don’t know about VS Code, then read What is Visual Studio Code and how is it different from Visual studio 2015?.
Following is the content of .csproj.
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" /> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp1.0</TargetFramework> </PropertyGroup> <ItemGroup> <Compile Include="**\*.cs" /> <EmbeddedResource Include="**\*.resx" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.NETCore.App"> <Version>1.0.1</Version> </PackageReference> <PackageReference Include="Microsoft.NET.Sdk"> <Version>1.0.0-alpha-20161104-2</Version> <PrivateAssets>All</PrivateAssets> </PackageReference> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>
And now let’s create an ASP.NET Core web application.
dotnet new -t web
After executing above command, visit the folder where this ASP.NET Core Web application is created. And you will find there is no Project.json and .xproj.
Now, restore the packages via dotnet restore
. But there is an error “Unable to resolve ‘Microsoft.NET.Sdk.Web (>= 1.0.0-alpha-20161117-1-119)’ for ‘.NETCoreApp,Version=v1.0′”> while restoring the packages.
The error is related with Microsoft.NET.Sdk.Web
package which is not getting restored successfully. To fix this error, open the .csproj file. As you can see, .csproj now contains a list of all the dependencies and framework version like project.json. To fix the error, look for Microsoft.NET.Sdk.Web
. And update package version to 1.0.0-*
from 1.0.0-alpha-20161117-1-119
and save it.
<Project ToolsVersion="15.0"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" /> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp1.0</TargetFramework> <PreserveCompilationContext>true</PreserveCompilationContext> </PropertyGroup> <PropertyGroup> <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback> </PropertyGroup> <ItemGroup> <Compile Include="**\*.cs" /> <EmbeddedResource Include="**\*.resx" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" /> <PackageReference Include="Microsoft.NET.Sdk.Web" Version="1.0.0-alpha-20161117-1-119"> <PrivateAssets>All</PrivateAssets> </PackageReference> <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.1" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Tools" Version="1.0.0-preview2-final" /> <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.0.1" /> <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.1" /> <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.0" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.0" /> <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink.Loader" Version="14.0.0" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>
After saving it, run dotnet restore
again and packages should be restored successfully. Executing dotnet run
should start the application also.
Visit the localhost URL in browser and you will see it’s running successfully. Great!!!!
Let’s open this application in VS 2015 and guess what one more error. 🙁
As per the error message, it is looking for XML namespace in .csproj file. So let’s add that to .csproj and save it.
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Let’s open it again. And there comes one more error “The attribute “Version” in element
Now, let’s try to create a new application from VS 2015 itself. And following error is coming as soon as you hit ok after selecting web application option from the selection dialog box.
So it seems that there are some issues with the nightly build. And let’s not worry about them as these will be addressed when the final release will be made.
What about old projects?
When this change was announced, it was also mentioned that this migration will be done automatically via VS when old projects (with .xproj) will be opened. But for now, it is not happening (since it’s a nightly build). But is there any way to migrate it now?
Well, the answer is Yes. I posted about Entity Framework Core InMemory provider with ASP.NET Core and let’s migrate it to new .csproj. dotnet tooling support migrate
command which migrates a Preview 2 .NET Core project to Preview 3 .NET Core project. You can get more information about dotnet migrate from here.
So let’s do the migration now. Open command prompt and get to the location where the old project resides and execute dotnet migrate
command. And got an error saying “no executable found matching command dotnet-migrate”. I tried executing at parent folder also but got the same error.
To fix above error, I followed these steps.
- Open
global.json
in notepad, and update the SDK version to 1.0.0-preview4-004175 (this is the version that’s installed on my syetem). - Go back to command prompt, and execute
dotnet migrate
command again. And this time, it works.
- Execute
dotnet restore
to restore packages. And once that is done, you will find project.json and .xproj files are now gone and .csproj file is present. All the dependencies are now part of .csproj. - Running
dotnet run
should run the application.
Summary
Though Project.json was a welcome change the experience is better than traditional .csproj. Since the announcement of ASP.NET Core (aka ASP.NET 5), things have changed as the framework is becoming more and more mature and probably based on the community feedback. So bye-bye Project.json and .xproj, let’s celebrate the rebirth of .csproj.
Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.