Local.Settings.json wird nicht in der Azure -Funktion gelesenC#

Ein Treffpunkt für C#-Programmierer
Guest
 Local.Settings.json wird nicht in der Azure -Funktion gelesen

Post by Guest »

Ich habe eine Azure -Funktion erstellt, den DBContext in der Programmdatei konfiguriert und die Verbindung hinzugefügt Zeichenfolgen als Werte in der Datei Local.Settings.json. < /p>
Was ich erwarte, ist, dass der Wert der Wert in der lokalen Datei ist. < /p>

Code: Select all

var builder = Host.CreateDefaultBuilder(args)
.ConfigureFunctionsWebApplication((host, webApp) =>
{
StartupHelper.FunctionsWorkerDefaults(webApp
, host
, "Sentry DSN");

webApp.UseMiddleware();
});

//builder.ConfigureFunctionsWebApplication();

// Application Insights isn't enabled by default. See https://aka.ms/AAt8mw4.
// builder.Services
//     .AddApplicationInsightsTelemetryWorkerService()
//     .ConfigureFunctionsApplicationInsights();

builder.ConfigureServices(services =>
{
// 1. Setup EF Core with Audit.Net
var tdbcs = Environment.GetEnvironmentVariable("TaskDbConnection");
services.AddDbContext(options => {
options.UseSqlServer(tdbcs);

#if DEBUG
options.EnableSensitiveDataLogging();
#endif
});

var idbcs = Environment.GetEnvironmentVariable("InsightsConnection");
services.AddDbContextFactory(options =>
{
options.UseSqlServer(idbcs);

#if DEBUG
options.EnableSensitiveDataLogging();
#endif
});

// 2. Add MediatR (scan the application layer for commands, queries, and event handlers)
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(CreateTaskCommandHandler).Assembly));  // Point to the assembly where MediatR handlers are located
services.AddScoped();
services.AddScoped();

services.AddScoped();
services.AddScoped();

// 3. Configure Audit.Net
Audit.EntityFramework.Configuration.Setup()
.ForContext(config => config
.IncludeEntityObjects()
.AuditEventType("{context}:{database}"))
.UseOptOut()
.IgnoreAny(t => t.Name.EndsWith("Audit"));

Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.AuditTypeExplicitMapper(m =>
{
// Example: Add types that should be audited
m.Map();  // Audit user entity
m.Map();  // Audit user entity
m.Map();  // Audit user entity
m.AuditEntityAction((evt, entry, auditEntity) =>
{
// Customize the audit event with additional information
auditEntity.AuditAction = entry.Action;
auditEntity.AuditDate = DateTimeOffset.Now;
auditEntity.AuditEventDocument = evt.ToJson();
//auditEntity.CorrelationId = evt.CustomFields["CorrelationId"].ToString().ToGuid();// as Guid;
auditEntity.AuditorUserId = evt.CustomFields["AuditorUserId"]?.ToString();
auditEntity.CorrelationId = evt.CustomFields["CorrelationId"]?.ToString();
});
}));

// Configure Audit.Net
Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, scope =>
{
// Set the custom fields in the audit event
scope.SetCustomField("AuditorUserId", AuditContext.UserId ?? "Anonymous");
scope.SetCustomField("CorrelationId", AuditContext.CorrelationId ?? "No Correlation Id");

if (scope.Event.Environment.Exception != null)
{
scope.SetCustomField("ExceptionMessage", scope.Event.Environment.Exception);
}

scope.Comment("Saved at " + DateTimeOffset.Now);
});

// 4.  Register any additional services here
// Example: Logging
services.AddLogging(loggingBuilder =>
{
var cs = Environment.GetEnvironmentVariable("InsightsConnection");
loggingBuilder.AddConsole();  // Logs to the console
StartupHelper.SetupSerilog(loggingBuilder
, "task_insights"
,connectionString:cs );
});
});

builder.Build().Run();

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post