如何将dotnet core 2.2 webapi迁移到dotnet core 3.0

时间:2019-09-26 10:07:07

标签: c# asp.net-core .net-core asp.net-core-mvc asp.net-core-3.0

我在这里https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#mvc-service-registration

遵循本指南

但是失败了,所以我用.net 2.2创建了一个新项目(只有一个控制器的空而新鲜的项目),然后尝试迁移到3.0。而且我又失败了,尤其是在Startup.cs中,DI无法检测到方法services.AddMvc();

似乎我的代码仍然引用.net 2.2而不是.net 3.0

如果我运行.net watch run

我收到此错误:

  

应用程序启动异常System.InvalidOperationException:   端点路由不支持“ IApplicationBuilder.UseMvc(...)”。   要使用“ IApplicationBuilder.UseMvc”设置   里面的'MvcOptions.EnableEndpointRouting = false'   'ConfigureServices(...)。在   Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder   应用程序,操作1 configureRoutes) at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app) at netmig.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in E:\testapp\netmig\Startup.cs:line 46 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) Unhandled exception. System.InvalidOperationException: Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...). at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action
1 configureRoutes)位于   Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder   app)在netmig.Startup.Configure(IApplicationBuilder app,   IHostingEnvironment env),位于E:\ testapp \ netmig \ Startup.cs:第46行   System.RuntimeMethodHandle.InvokeMethod(对象目标,对象[]   参数,签名sig,布尔构造函数,布尔wrapExceptions)   在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags   invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化)
  在Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object   例如IApplicationBuilder构建器)   Microsoft.AspNetCore.Hosting.ConfigureBuilder。<> c__DisplayClass4_0.b__0(IApplicationBuilder   建筑商)   Microsoft.AspNetCore.Hosting.GenericWebHostBuilder。<> c__DisplayClass13_0.b__2(IApplicationBuilder   应用)   Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter。<> c__DisplayClass0_0.g__MiddlewareFilterBuilder | 0(IApplicationBuilder   建筑商)   Microsoft.AspNetCore.HostFilteringStartupFilter。<> c__DisplayClass0_0.b__0(IApplicationBuilder   应用)   Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken   cancelToken)   Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken   cancelToken)   Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost   主机,CancellationToken令牌)位于   Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost   主机,CancellationToken令牌)位于   Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost   主机)在netmig.Program.Main(String [] args)中   E:\ testapp \ netmig \ Program.cs:line 18 watch:退出并显示错误代码   -532462766

我的代码在https://github.com/callmeblessed/netmig

我的.net信息

  

dotnet --info .NET Core SDK(反映任何global.json):版本:
  3.0.100提交:04339c3a26

     

运行时环境:操作系统名称:Windows操作系统版本:   10.0.18362操作系统平台:Windows RID:win10-x64基本路径:C:\ Program Files \ dotnet \ sdk \ 3.0.100 \

     

主机(可用于支持):版本:3.0.0提交:7d57652f33

     

.NET Core SDK已安装:     2.2.401 [C:\ Program Files \ dotnet \ sdk]     3.0.100 [C:\ Program Files \ dotnet \ sdk]

     

.NET Core运行时已安装:Microsoft.AspNetCore.All 2.2.6   [C:\ Program Files \ dotnet \ shared \ Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.6 [C:\ Program   Files \ dotnet \ shared \ Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [C:\ Program   Files \ dotnet \ shared \ Microsoft.AspNetCore.App] Microsoft.NETCore.App   2.2.6 [C:\ Program Files \ dotnet \ shared \ Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0 [C:\ Program   Files \ dotnet \ shared \ Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\ Program   Files \ dotnet \ shared \ Microsoft.WindowsDesktop.App]

     

要安装其他.NET Core运行时或SDK,请执行以下操作:
  https://aka.ms/dotnet-download

1 个答案:

答案 0 :(得分:3)

对于v2.2

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }

对于v3

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

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

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

还要确保您的csproj是目标.net core 3.0

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>