.NET CORE web api未检测到更改

问题描述 投票:0回答:1

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

我没有检测到我的Web API更改,我不知道为什么例如,我有一些控制器,每当我更改它们,然后使用web api时,都没有进行更改。我甚至可以删除整个文件,使用此文件的web api仍然可以正常工作!我得到的另一个问题是,当我创建新的控制器文件时,它也没有被检测到,我被我的旧控制器所困扰,无法更新它。检测到其他文件更新(至少如果我更改了vueJS前端)我还可以更改提供程序,删除用于web 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

    }
}

我可以删除这些文件,我仍然可以访问我的操作web 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" });
            });
        }
    }
}

提前致谢,

vue.js visual-studio-code asp.net-core-2.2
1个回答
1
投票

好吧,我已经找到了为什么我遇到这个问题,我觉得有点愚蠢但是,现在它正在运作。

当我从.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

© www.soinside.com 2019 - 2024. All rights reserved.