.NET CORE web api 未检测到更改

.NET CORE web api Changes not detected

我目前正在开发一个使用 .NET Core 2.2 作为后端的 VueJS 应用程序。 我为此工作了几个月,但当我从 2.0 更新到 2.2 时,它突然停止工作了。

我的网站 API 更改未检测到,我不知道为什么。 例如,我有几个控制器,每当我更改它们,然后使用网络 API 时,都不会进行更改。我什至可以删除整个文件,使用这个文件的网络 API 仍然可以工作!

我遇到的另一个问题是,当我创建新的控制器文件时,它没有被检测到;我受困于无法更新的旧控制器。 检测到其他文件更新(至少如果我更改 VueJS 前端) 我还可以更改提供程序,删除用于网络的任何文件 API,未检测到更改。可能是配置问题?

有什么我可以尝试让事情再次更新的吗?

更新:我可以在后端更改我想要的任何内容,但它什么都不做。编译错误是我唯一需要关心的问题,就像应用程序不再使用代码一样。

这是我可以提供的示例:

我有一个控制器 InterventionController 可以检索有关操作的数据(我在法语环境中是法语,所以变量名称等将使用法语):

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Vue2Spa.Models;
using Vue2Spa.Providers;

namespace Vue2Spa.Controllers
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    public class InterventionController : Controller
    {
        private readonly IInterventionProvider interventionProvider;


        public InterventionController(IInterventionProvider interventionProvider)
        {
            this.interventionProvider = interventionProvider;
        }

        [HttpGet("[action]")]
        public IActionResult Interventions([FromQuery(Name = "from")] int from = 0, [FromQuery(Name = "to")] int to = 5000)
        {

            var quantity = to - from;

            if (quantity <= 0)
            {
                return BadRequest("La quantité doit être positive !");
            }
            else if (from < 0)
            {
                return BadRequest("Vous devez spécifier un indice de départ non nul !");
            }

            var allInterventions = interventionProvider.GetInterventions();

            var result = new
            {
                TotalInterventions = allInterventions.Count,
                Interventions = allInterventions.Skip(from).Take(quantity).ToArray()       
            };

            return Ok(result);
        }
    }

    // Others methods not useful for my example
}

它调用具有以下代码的提供程序:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Vue2Spa.Models;

namespace Vue2Spa.Providers
{
    public class DBInterventionProvider : IInterventionProvider
    {

        private List<Intervention> interventions { get; set; }
        DbContextOptionsBuilder<DepouillementTestContext> optionsBuilder = new DbContextOptionsBuilder<DepouillementTestContext>();

        public DBInterventionProvider()
        {
            optionsBuilder.UseSqlServer(credentials); // Credentials are correct but not including it there for obvious reasons
            using (var context = new LECESDepouillementTestContext(optionsBuilder.Options))
            {
                interventions = context.Intervention.ToList();
            }
        }

        public List<Intervention> GetInterventions()
        {
            using (var context = new LECESDepouillementTestContext(optionsBuilder.Options))
            {
                interventions = context.Intervention.ToList();
            }
            return interventions;
        }

    // Others methods not useful for this example

    }
}

我可以删除这些文件,而且我仍然可以访问我的操作网站 API

如果需要,这是我的 startup.cs 文件:

using System;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Vue2Spa.Models;

namespace Vue2Spa
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();

            // Additional code for SQL connection

            services.AddDbContext<DepouillementTestContext>(options =>
            {
                options.UseSqlServer(Configuration["ConnectionString"],
                sqlServerOptionsAction: sqlOptions =>
                {
                    sqlOptions.
                        MigrationsAssembly(
                            typeof(Startup).
                            GetTypeInfo().
                                Assembly.
                                GetName().Name);

                    //Configuring Connection Resiliency:
                    sqlOptions.
                        EnableRetryOnFailure(maxRetryCount: 5,
                        maxRetryDelay: TimeSpan.FromSeconds(30),
                        errorNumbersToAdd: null);

                });

                // Changing default behavior when client evaluation occurs to throw.
                // Default in EFCore would be to log warning when client evaluation is done.
                options.ConfigureWarnings(warnings => warnings.Throw(
                    RelationalEventId.QueryClientEvaluationWarning));
            });

            // Provider pour les interventions
            services.AddSingleton<Providers.IInterventionProvider, Providers.DBInterventionProvider>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                // Webpack initialization with hot-reload.
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true,
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }
    }
}

提前致谢,

好吧,我找到了为什么会遇到这个问题,我对此感到有点愚蠢,但好吧,它现在可以正常工作了。

当我从 .NETCORE 2.0 升级到 2.2 时,我没有更改 launch.json,我所要做的就是更改

"program": "${workspaceFolder}/content/bin/Debug/netcoreapp2.0/Vue2Spa.dll",

来自

"program": "${workspaceFolder}/content/bin/Debug/netcoreapp2.2/Vue2Spa.dll",

有关详细信息,请参阅:https://docs.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-2.2&tabs=visual-studio