Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException : The ConnectionString property has not been initialized. .net 8 isolated worker #3384

Open
Akhil1501 opened this issue Nov 21, 2024 · 2 comments

Comments

@Akhil1501
Copy link

Akhil1501 commented Nov 21, 2024

I am facing this issue only in runtime in azure, in local it works fine for me. even after i hardcoded the connectionstring in the program.cs

Program.cs

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices((context,services )=>
    {
       string connectionString = "Server=";

        if (string.IsNullOrEmpty(connectionString))
        {
            // Log an error or throw an exception if the connection string is not available
            throw new InvalidOperationException("Connection string is not set. ConnectionString: " + connectionString);
        }
        services.AddDbContext<mydbContext>((options) =>
        {
            options.UseNpgsql(connectionString, builder =>
            {
                builder.EnableRetryOnFailure(3, TimeSpan.FromSeconds(10), null);
            });

        }, ServiceLifetime.Scoped);
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
}

even the throw new invalid operation is not getting logged .

Function App:

public class ProjectChangeListener
{
    private readonly ILogger<ProjectChangeListener> _logger;
    private readonly MyDBContext _context;
    private readonly IProjectService _projectService;

    public ProjectChangeListener(ILogger<ProjectChangeListener> logger, MyDBContext context, IProjectService projectService)
    {
        _logger = logger;
        _context = context;
        this._projectService = projectService;
    }

    [Function("ProjectChangeListener")]
    public async Task RunProjectChangeListener(
        [ServiceBusTrigger("lot", "build", Connection = "XXXXXX_SERVICEBUS_CONNECTION_STRING")]
        ServiceBusReceivedMessage message,
        ServiceBusMessageActions messageActions)
    {
        _logger.LogInformation("ProjectChangeListener Triggered");
        try
        {
            _logger.LogInformation("ProjectChangeListener Triggered Try Block");
            _logger.LogInformation("Message ID: {id}", message.MessageId);
            var recievedMessage = message.Body.ToString();
            var lotDTO = JsonConvert.DeserializeObject<LotDTO>(recievedMessage);
            var dbProject = await _context.Projects.FindAsync(lotDTO.LotNo);

            if (dbProject == null)
            {
                _logger.LogInformation("ProjectId Not found to Sync between HB1 and BetterBuild: {id}", lotDTO.LotNo);
                return;
            }

            var response = await _projectService.UpdateProjectAsync(lotDTO);

            if (!response.IsSuccessStatusCode)
            {
                var error = response.Content.ReadAsStringAsync();
                _logger.LogError($"Update project failed for projectId {lotDTO.LotNo} - {error}");                    
            }               
        }
        catch (Exception ex)
        {
            _logger.LogError("ProjectChangeListener Failed" + ex.Message + "Stack Trace" + ex.StackTrace);
            throw ex;
        }
    }

above is the function app works locally , totally fine, but when deployed and when the topic is picked up it fails.

@roji
Copy link
Member

roji commented Nov 21, 2024

@Akhil1501 whether you're running on Azure or locally shouldn't have any bearing on how Npgsql of EF behaves - there's likely some sort of difference in how you configure or run your application on Azure. Double-check your Azure configuration/deployment to make sure everything is correct; otherwise, I'm going to need some sort of minimal, runnable repro in order to help further.

@Akhil1501
Copy link
Author

Akhil1501 commented Nov 22, 2024

@Akhil1501 whether you're running on Azure or locally shouldn't have any bearing on how Npgsql of EF behaves - there's likely some sort of difference in how you configure or run your application on Azure. Double-check your Azure configuration/deployment to make sure everything is correct; otherwise, I'm going to need some sort of minimal, runnable repro in order to help further.

But if it was an issue with configuration that shouldnt been a problem, when i deleted the connection string created by script and added it myself, still same issue, in the code above, i have hardcoded the connection string. that shouldnt have been a problem right, I even tried removing all database operations, still it fails with the same reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants