ASP.NET 8 Web API:dotnet run无法运行但Visual Studio可正常调试
ASP.NET 8 Web API启动异常问题:
dotnet run无响应但VS调试正常 在控制台执行dotnet run命令运行ASP.NET 8 Web API项目时,程序无法完全启动,且无任何错误提示;但在Visual Studio 2022调试模式下运行完全正常。
Program.cs 代码
using Institutor.Data; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using System.Text; System.Environment.SetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "0"); // Create a WebApplication builder with the arguments passed to the program var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Add the DbContext and configure it to use SQLite with the connection string from the configuration builder.Services.AddDbContext<InstitutorDataContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); // Add Identity services to the DI container and configure it to use the DbContext for storage builder.Services.AddIdentity<IdentityUser, IdentityRole>() // Add IdentityRole here .AddEntityFrameworkStores<InstitutorDataContext>() .AddDefaultTokenProviders(); // Add this to add the default token providers // Add JWT authentication services to the DI container and configure it builder.Services.AddAuthentication(x=> { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration.GetSection("AppSettings:Token").Value)), ValidateIssuer = false, ValidateAudience = false }; options.IncludeErrorDetails = true ; }); // Add controllers to the DI container builder.Services.AddControllers(); // Add API explorer services to the DI container builder.Services.AddEndpointsApiExplorer(); //Adding Azure Services logging builder.Services.AddLogging(builder => { builder.AddAzureWebAppDiagnostics(); }); // Add Swagger services to the DI container and configure it builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new() { Title = "Institutor API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme. Just paste the token (No need to add Bearer)", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.Http, Scheme = "bearer" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] {} } }); }); // Build the application var app = builder.Build(); // Configure the HTTP request pipeline. // Use Swagger in development environment if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } // Use HTTPS redirection middleware app.UseHttpsRedirection(); // Use authentication middleware app.UseAuthentication(); // Use authorization middleware app.UseAuthorization(); // Map controller routes app.MapControllers(); // Run the application app.Run();
.csproj 文件内容
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <InvariantGlobalization>false</InvariantGlobalization> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" /> <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="8.0.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.4.1" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Data\Data.csproj" /> <ProjectReference Include="..\Model\Model.csproj" /> <ProjectReference Include="..\Services\Services.csproj" /> </ItemGroup> </Project>
如需更多细节,请告知。
内容的提问来源于stack exchange,提问作者ROY
相关产品推荐
相关产品推荐

