---
# System prepended metadata

title: '`.NET Core` 2.2 升級 3.1'
tags: [.NET Core, 工作紀錄]

---

`.NET Core` 2.2 升級 3.1  
===

```
嚴重性	程式碼	說明	專案	檔案	行	隱藏項目狀態
警告	CS0618	'CompatibilityVersion.Version_2_2' 已經過時: 'This CompatibilityVersion value is obsolete. The recommended alternatives are Version_3_0 or later.'	TLDE.Frontend.Host	C:\Azure\TLDE.Service\TLDE.Frontend.Host\Startup.cs	43	作用中
```
`services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);` 
改成 
```
services.AddControllers();
```  

```
嚴重性	程式碼	說明	專案	檔案	行	隱藏項目狀態
警告	CS0618	'IHostingEnvironment' 已經過時: 'This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment.'	TLDE.Frontend.Host	C:\Azure\TLDE.Service\TLDE.Frontend.Host\Startup.cs	46	作用中
```

`IHostingEnvironment` 改成 `IWebHostEnvironment`  

[參考 - 從 ASP.NET Core 2.2 遷移至 3.0](https://docs.microsoft.com/zh-tw/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio)
![](https://i.imgur.com/1yjTYH2.png)

移除 `Microsoft.AspNetCore.App` 與 `Microsoft.AspNetCore.Razor.Design`  

```
warning MVC1005: Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'.
```

`app.UseMvc();` 
改成   
```
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});
```
[](https://stackoverflow.com/questions/53675850/how-to-fix-the-cors-protocol-does-not-allow-specifying-a-wildcard-any-origin)
```
The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the CORS policy by listing individual origins if credentials needs to be supported.
```
刪除程式碼  
```
app.UseCors(
                builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
```
![](https://i.imgur.com/67jZL0X.png)
```
System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamReader.ReadBuffer()
   at System.IO.StreamReader.ReadToEnd()
   at CArk.Host.DotNetCore.CArkHostDotNetCore.ProcessRequest(HttpContext context, Func`1 next)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
```
新增程式碼  
```
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader());
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseDeveloperExceptionPage();

    app.UseCors("CorsPolicy");

    app.UseCArkSDK(new CArkBaseServicesHost("TLDE", ConfMgr.Instance._config,
        new Assembly[] {
            typeof(Service_Echo).Assembly, // Health Check
            typeof(ContractRequest_Echo).Assembly, // Health Check
            typeof(Service_Room_RoomCategoryListALL).Assembly,
            typeof(CReq_RoomCategory_ListALL).Assembly
        }));
}

```
[參考 - .Net Core 3 preview: Synchronous operations are disallowed
](https://stackoverflow.com/questions/55052319/net-core-3-preview-synchronous-operations-are-disallowed)
```
public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
            });
        }
```
改成  
```
public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
            });
            services.Configure<KestrelServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });
            services.Configure<IISServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });
        }
```

`FolderProfileDebug.pubxml` 檔案裡的  
```
<TargetFramework>netcoreapp3.1</TargetFramework>
```
記得改成 3.1 發布才能正常編譯  
還有所有原本是 `netstandard2.0`  
```
<TargetFramework>netstandard2.0</TargetFramework>
```
要改成 `netstandard2.1`  
```
<TargetFramework>netstandard2.1</TargetFramework>
```
記得是因為 3.0 之後就只支援 netstandard2.1  
不然發布時也會編譯不過  





###### tags: `.NET Core` `工作紀錄` 