-1

I have an application that runs fine locally, using ASP.NET Core, Angular and a SQL Server database.

But, I am having problems when trying to deploy it. I have the SQL Server database deployed to Azure. The backend is also deployed to Azure.

I am running the frontend locally to test the backend /database. Any calls that it makes to the database return a http 500 error. I have also tried to use Postman to hit the endpoints, and I also get a http 500 error. It didn't show any errors when I published it and it showed that it should run. When I publish the solution it shows the database is connected:

Azure publish

You can see from the screen print that I have connected application insights also but it hasn't given me much information.

This is in Azure showing where the error was thrown, but I don't know where this is in my code:

erroring part of code

These are the calls that get a 500 error they are both making calls to the database:

Failed calls

Here is the inspect from the locally running front end:

network inspect

I followed this tutorial to deploy but I am not sure that the information it gave to connect the database to my API applied to the Stack I am using.

https://www.youtube.com/watch?v=u_CRppLcC9k&t=463s

Thank you in advance for any direction or help you can give me. I have been stuck on this for about a week. Here is a screenshot of the rules for the firewall that have my api Firewall Rules

here is a screenshot of the log stream I blocked out my ip address and the URL to the API.

logstream This is the app log stream:

app log stream

What does this mean: >IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.

  • IIS was not able to process configuration for the Web site or application.
  • The authenticated user does not have permission to use this DLL.
  • The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.

  • 9
    • Please share logs from the LogStream of Azure Web App. Commented Mar 7 at 6:14
    • Are your IP addresses allowed in your Azure SQL Server firewall rules? Commented Mar 7 at 6:17
    • Hi @AsleshaKantamsetti thank you for your response. I have the IP address of the API and the client (I am spinning up the front end locally right now) I put the development one there in case I needed to use it because I am running the UI locally. Commented Mar 7 at 20:41
    • 1
      @BenCrowe Set the connection string in Azure Web App Environment Variables. Commented Mar 10 at 8:42
    • 1
      Thank you very much @Alesha Kantamsetti, thank you for taking the time to give me the sections where I set that, I haven't verified mine yet but I will let you know if that solved the problem. Commented Mar 15 at 4:30

    2 Answers 2

    1

    The HTTP 500 error in the ASP.NET Core backend after deployment to Azure is likely due to database connectivity issues, insufficient permissions, or misconfiguration.

    To Avoid above error, Add the Connection string to Azure App Service Environment Variable as shown below.

    enter image description here

    Make sure Add current Ip Address and Allow Azure Service in the Networking section of Azure Sql Server.

    enter image description here

    Below is my Program.cs :

    using Microsoft.EntityFrameworkCore;
    using WebApplication3.Data;
    using WebApplication3.Models;
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
    builder.Services.AddControllers();
    builder.Services.AddCors(options =>
    {
        options.AddPolicy("AllowAngularApp",
            builder => builder.WithOrigins("http://localhost:4200")
                              .AllowAnyMethod()
                              .AllowAnyHeader());
    });
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();
    var app = builder.Build();
    if (app.Environment.IsDevelopment())
    {
        app.MapOpenApi();
    }
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    };
    app.UseCors("AllowAngularApp");
    app.UseHttpsRedirection();
    app.MapControllers();
    var summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };
    app.MapGet("/weatherforecast", () =>
    {
        var forecast = Enumerable.Range(1, 5).Select(index =>
            new WeatherForecast
            (           DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                Random.Shared.Next(-20, 55),
                summaries[Random.Shared.Next(summaries.Length)]
            ))
            .ToArray();
        return forecast;
    })
    .WithName("GetWeatherForecast");
    app.MapstudentEndpoints();
    app.Run();
    internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
    {
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    }
    

    Add Azure Backend URL to Angular Project:

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs';
    @Injectable({
      providedIn: 'root',
    })
    export class StudentService {
      private apiUrl = 'https://<AzureWebAppName>.canadacentral-01.azurewebsites.net/api/Students';
      constructor(private http: HttpClient) {}
      getStudents(): Observable<any[]> {
        return this.http.get<any[]>(this.apiUrl);
      }
      addStudent(student: any): Observable<any> {
        return this.http.post<any>(this.apiUrl, student);
      }
      updateStudent(student: any): Observable<void> {
        return this.http.put<void>(`${this.apiUrl}/${student.id}`, student);
      }
      deleteStudent(id: number): Observable<void> {
        return this.http.delete<void>(`${this.apiUrl}/${id}`);
      }
    }
    

    After following above steps, I was successfully call the data from backend to frontend. Output:

    enter image description here

    Sign up to request clarification or add additional context in comments.

    Comments

    0

    I finally was able to get my deployed Back End Services to connect to my deployed Azure SQL DB. I did a few things. I am not sure which exact item fixed my 500.0 error I was receiving.

    1. I read in the Microsoft documentation that if both your service and your database were in Azure you didn't need to put the IPV4 output range for the Web Api as a firewall rule for the DB server. You can enter 0.0.0.0 for the start and end 0.0.0.0
    2. The second thing I changed was I had been using the service connector in addition to putting a connection string in my appsettings.json. I deleted those connections in the service connector and in Visual Studio. VS screen shot of service dependencies
    3. I had forgotten that every time I changed to a different connection string and then published the app again it didn't overwrite the service dependencies, it created additional service dependency files. Service Dependencies So I deleted all of the service dependencies and any additional files created by publishing to Azure. I discarded the changes in my Git version control in VSCode. I made sure I had the first option connection string for ADO.NET (Microsoft Entra passwordless authentication) I made sure I created an admin in the Microsoft Entra Id following these instructions: https://learn.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-sql-database?tabs=windowsclient%2Cefcore%2Cdotnet#grant-permissions-to-managed-identity Then I gave the DB access to it. I then published that and it worked!

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.