AspNetCore.Docs/aspnetcore/migration/22-to-30.md

90 lines
4.7 KiB
Markdown
Raw Normal View History

---
title: Migrate from ASP.NET Core 2.2 to 3.0 Preview
author: tdykstra
description: Learn how to migrate an ASP.NET Core 2.2 project to ASP.NET Core 3.0.
ms.author: tdykstra
ms.custom: mvc
ms.date: 01/04/2019
uid: migration/22-to-30
---
# Migrate from ASP.NET Core 2.2 to 3.0 Preview 2
By [Scott Addie](https://github.com/scottaddie) and [Rick Anderson](https://twitter.com/RickAndMSFT)
This article explains how to update an existing ASP.NET Core 2.2 project to ASP.NET Core 3.0 preview 2.
[!INCLUDE[](~/includes/net-core-prereqs-all-3.0.md)]
## Update the project file
* Set the [Target Framework Moniker (TFM)](/dotnet/standard/frameworks#referring-to-frameworks) to `netcoreapp3.0`:
```xml
<TargetFramework>netcoreapp3.0</TargetFramework>
```
* Remove any `<PackageReference>` to the [Microsoft.AspNetCore.All](xref:fundamentals/metapackage) or [Microsoft.AspNetCore.App](xref:fundamentals/metapackage-app) metapackage.
There's a known issue in Preview 1: projects that don't start with `<Project Sdk="Microsoft.NET.Sdk.Web">` get compiler or runtime errors due to missing `Microsoft.AspNetCore.*` assemblies. This is most often the case for test projects and class libraries. The workaround is to add the following to the *.csproj* file.
```xml
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
```
For more information, see [NuGet/Home issue #7342](https://github.com/NuGet/Home/issues/7342).
* Update the `Version` on remaining `<PackageReference>` elements for `Microsoft.AspNetCore.*` packages to the current preview (for example, 3.0.0-preview-18579-0053).
If there is no 3.0 version of a package, the package might have been deprecated in 3.0. Many of these are part of `Microsoft.AspNetCore.App` and should not be referenced individually anymore. For a preliminary list of packages no longer produced in 3.0, see [aspnet/AspNetCore #3756](https://github.com/aspnet/AspNetCore/issues/3756).
* Some assemblies were removed from `Microsoft.AspNetCore.App` between 2.x and 3.0. You may need to add `<PackageReference>` items if you're using APIs from packages listed in [aspnet/AspNetCore #3755](https://github.com/aspnet/AspNetCore/issues/3755)
For example, `Microsoft.EntityFrameworkCore` and `System.Data.SqlClient` are no longer part of `Microsoft.AspNetCore.App`. The list of assemblies shipping in `Microsoft.AspNetCore.App` hasn't been finalized yet and will change before 3.0 RTM.
* Add [Json.NET support](#json)
<a name="json"></a>
## Json.NET support
As part of the work to [improve the ASP.NET Core shared framework](https://blogs.msdn.microsoft.com/webdev/2018/10/29/a-first-look-at-changes-coming-in-asp-net-core-3-0/), [Json.NET](https://www.newtonsoft.com/json/help/html/Introduction.htm) has been removed from the ASP.NET Core shared framework.
To use Json.NET in an ASP.NET Core 3.0 project:
- Add a package reference to [Microsoft.AspNetCore.Mvc.NewtonsoftJson](https://nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson)
- Update `ConfigureServices` to call `AddNewtonsoftJson()`.
```csharp
services.AddMvc()
.AddNewtonsoftJson();
```
Newtonsoft settings can be set with `AddNewtonsoftJson`:
```csharp
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
```
## HostBuilder replaces WebHostBuilder
The ASP.NET Core 3.0 templates use [Generic Host](xref:fundamentals/host/generic-host). Previous versions used [Web Host](xref:fundamentals/host/web-host). The following code shows the ASP.NET Core 3.0 template generated `Program` class:
[!code-csharp[](22-to-30/samples/Program.cs?name=snippet)]
The following code shows the ASP.NET Core 2.2 template-generated `Program` class:
[!code-csharp[](22-to-30/samples/Program2.2.cs?name=snippet)]
<xref:Microsoft.AspNetCore.Hosting.IWebHostBuilder> remains in 3.0 and is the type of the `webBuilder` seen in the preceding code sample. <xref:Microsoft.AspNetCore.Hosting.WebHostBuilder> will be deprecated in a future release and replaced by `HostBuilder`.
## Moving from WebHostBuilder to HostBuilder
The most significant change from `WebHostBuilder` to `HostBuilder` is in [dependency injection (DI)](xref:fundamentals/dependency-injection). When using `HostBuilder`, you can only inject <xref:Microsoft.Extensions.Configuration.IConfiguration>, <xref:Microsoft.Extensions.Hosting.IHostingEnvironment>, and <xref:Microsoft.AspNetCore.Hosting.IHostingEnvironment> into Startup's constructor. The `HostBuilder` DI constraints:
* Enable the DI container to be built only one time.
* Avoids the resulting object lifetime issues like resolving multiple instances of singletons.