Skip to the content.

将Teams app升级到net6

net6 发布已经有一个多月了,因为我的LuckyDraw app是使用Azure app service,当时我查了一下app service还没有ready,昨天我又查看了一下,发现app service已经默认装了net6。

net6

那就是时候升级我的LuckyDraw了,如果大家的app也是用c#来开发的话,那这篇文章给大家分享一下我的升级方法。

首先,先更新csproj,把之前的LTS 3.1改成 net6.0。同时也把 c# 语言改用版本 9,这样就可以更多的使用一些方便的语法。

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>

改成:

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <LangVersion>9</LangVersion>
  </PropertyGroup>

第二步,把所有对应的library也升级到最新版本

  <ItemGroup>
    <PackageReference Include="AdaptiveCards" Version="1.2.4" />
    <PackageReference Include="Microsoft.Bot.Builder" Version="4.9.3" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
    <PackageReference Include="Microsoft.AspNetCore.AzureAppServicesIntegration" Version="3.1.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.3" />
    <PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.0.2" />
    <PackageReference Include="AspNetCore.HealthChecks.Uris" Version="3.0.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
    <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.2.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.2.1" />
    <PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
  </ItemGroup>

改成

  <ItemGroup>
    <PackageReference Include="AdaptiveCards" Version="2.7.3" />
    <PackageReference Include="Microsoft.Bot.Builder" Version="4.15.0" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.19.0" />
    <PackageReference Include="Microsoft.AspNetCore.AzureAppServicesIntegration" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.21" />
    <PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.1-rc2.4" />
    <PackageReference Include="AspNetCore.HealthChecks.Uris" Version="6.0.1" />
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.2.3" />
    <PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
  </ItemGroup>

编译后,会发现有些warning,虽然不改也可以,但是有强迫症的我,必须把他们改掉。

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Latest)
    .AddJsonOptions(opt =>
    {
        ...
        opt.JsonSerializerOptions.IgnoreNullValues = true;
        ...
    });

3.1中的 SetCompatibilityVersion(CompatibilityVersion.Latest) 已经不再被使用了,直接去掉就可以。

另外,System.Text.Json 中的 IgnoreNullValues 也被标记成了 obsolete,将会在以后版本中移除,我们可以改成 opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;,可以参考这个文档 https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.ignorenullvalues?view=net-6.0#System_Text_Json_JsonSerializerOptions_IgnoreNullValues

改完后是:

services.AddMvc()
    .AddJsonOptions(opt =>
    {
        ...
        opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        ...
    });

最后还有一个warning是 teams bot的,是说:

warning CS0618: 'AppCredentials.TrustServiceUrl(string, DateTime)' is obsolete: 'TrustServiceUrl is not a required part of the security model.'

所以,如果大家在teams app的代码中使用了 TrustServiceUrl(),那当你升级到最新的 teams sdk后,就可以删除这句了。

MicrosoftAppCredentials.TrustServiceUrl(botServiceUrl, dateTimeService.UtcNow.AddDays(7).DateTime);

改完后,checkin,一切正常,赞!说明 .net 6 在向前兼容上做的很不错。

虽然这个只是最最简单的升级方法,代码层面还没有使用 .net6 和 c# 9 带给我的新功能,我会在后面的文章中继续介绍我如何enable nullable 和 implicit using,特别是 nullable,可以极大的提高代码的质量。

Written on December 13, 2021