Deploying ASP.NET Core Web API created with Clean Architecture to Azure App Service using VS Code is not working

109 Views Asked by At

I am trying to deploy my ASP.NET Core Web API project that I created using Clean Architecture and my Azure Student portal account. The deployment is successful however when I navigate to the API's URL endpoint it doesn't show the JSON object that I was expecting. I also checked the application event logs in Azure but found nothing.

Expected:

enter image description here

Actual:

enter image description here

The job records are stored in Azure's SQL Servers resource and when I ran the dotnet run, it lists all the records within the database. When I ran the command below it added the Release folders for each class libraries. I'm using Azure App Extension in VS Code to deploy the Web API.

Command:

dotnet publish -c Release

enter image description here

Application Event Log:

enter image description here

UPDATED: Added Program.cs

using Infrastructure;
using Application;
using Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Domain.Entities;
using Microsoft.Net.Http.Headers;

var builder = WebApplication.CreateBuilder(args);
var worklogCorsPolicy = "WorklogAllowedCredentialsPolicy";

builder.Services.AddCors(options =>
{
    options.AddPolicy(worklogCorsPolicy,
        policy =>
        {
            policy.WithOrigins("http://localhost:5173") 
                .WithMethods("GET", "POST")
                    .WithHeaders(HeaderNames.ContentType, "x-custom-header",
                    HeaderNames.Authorization, "true")
                        .AllowCredentials();
        });
});

builder.Services.AddControllers();
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddApplication();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();


if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseRouting();

app.UseHsts();

app.UseHttpsRedirection();

app.UseCors(worklogCorsPolicy);

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();


using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;

try
{
    var context = services.GetRequiredService<WorklogDbContext>();
    var userManager = services
            .GetRequiredService<UserManager<AppUser>>();
    await context.Database.MigrateAsync();
    await Seed.SeedData(context, userManager,         
            builder.Configuration);
}
catch (Exception ex)
{
    var logger = services
            .GetRequiredService<ILogger<Program>>();
    logger.LogError(ex, "An error occured during migration.");
}

app.Run();

Other Things I tried:

  1. I tried creating a separate .NET Core Web API called Weather that doesn't use the clean architecture approach, after I published the application using the Release build configuration, I deployed this in Azure.

I compared this with my project that uses clean architecture and I noticed that the worklog has Release build configuration for each layer(Domain, Application, Infrastructure and API) as shown in the screenshot below: I somehow think that this is where I'm having but since there's no error thrown I couldn't verify it.

enter image description here

To deploy the API I'm using Azure App Service Extension as shown below:

enter image description here

  1. Publish Profile - I found that in Visual Studio, it is possible to deploy the API thru importing the publish profile downloaded from Azure, however since I'm using VS Code I won't be able to do this.

  2. I tried searching online for other ways and I found that other options are using Bicep with Azure pipeline or using Bicep with GitHub action however I'm not sure if I'm on the right track. Another is learning curve since I have to learn both options to determine if this is what I need.

Any assistance would be appreciated.

0

There are 0 best solutions below