【问题标题】:Unable to build my app cause ConfigureServices returns error无法构建我的应用程序导致 ConfigureServices 返回错误
【发布时间】:2023-03-27 06:30:01
【问题描述】:

我是 ASP.NET 的新手,并且正在使所有这些事情正常工作。

我想从我的 MSSQL 数据库中获取数据,并将其传递给我的 Angular Web 应用程序,就像简单的前端 后端通信,所以我让数据模型看起来像这样::

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPI_1.Models
{
    public class Users
    {

        public int id { get; set; }
        public string Imie { get; set; }
        public string Nazwisko { get; set; }
        public string Login { get; set; }
        public string Haslo { get; set; }
    }
}

我还有一个如下所示的 DataContext 文件:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPI_1.Models;

namespace WebAPI_1.Data
{
    public class DataContext: DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options) { } //wywołuje konst bazowy i przekazuje opcje

        public DbSet<Users> Users { get ; set; }
    }
}

我想该文件中的所有内容都可以正常工作,但我在 Startup.cs 文件中只有一个 CS0161 错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WebAPI_1.Data;
using Microsoft.EntityFrameworkCore;
using System.Data.Sql;

namespace WebAPI_1
{
    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 object ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(s => s.UseSqlServer("ConnectionString"));

            services.AddControllers();

            services.AddMvc(services => services.EnableEndpointRouting = false);

        }

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

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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

            app.UseMvc();
        }
    }
}

我的 IDE (VS 2019) 中的错误描述:

*严重性代码描述项目文件行抑制状态 错误 CS0161 'Startup.ConfigureServices(IServiceCollection)':并非所有代码路径都返回值 WebAPI_1 C:\Users\Michal\source\repos\WebAPI_1\WebAPI_1\Startup.cs 29 活动 *

我已经知道的:

  • 我不明白为什么会出现此错误
  • 这是因为 services.AddDbContext(s => s.UseSqlServer("ConnectionString")); 没有返回任何内容

最后,我想向您寻求帮助和可能的解决方案。

【问题讨论】:

  • ConfigureServices 通常返回void。为什么你的被声明为返回object

标签: c# sql-server asp.net-core


【解决方案1】:

您的ConfigureServices 方法想要返回object。我猜它不应该返回任何东西 (void)。

【讨论】:

  • 感谢您的回复,您知道如何制作 services.AddDbContext(s => s.UseSqlServer("ConnectionString"));在那种情况下工作?
  • 据我所知,这应该可以正常工作。您收到错误消息吗?
  • 不,现在我看到了问题,你是对的,谢谢\
猜你喜欢
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
相关资源
最近更新 更多