Dotnet Run is not working but the application is running in Visual Studio

40 Views Asked by At

I'm facing an issue with an ASP.NET 8 Web API.

While running the project in the console with dotnet run, it is not running completely or showing any error either. The console screen is as follows:

Console Log

But the application is running in debug mode from the Visual Studio 2022 without any issue.

Here's my 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();

Here's the .csproj file for project:

<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>

Please let me know if any more details required in this regards.

0

There are 0 best solutions below