Switch dependency injection to Scoped (#1801)

Co-authored-by: stas <statis@microsoft.com>
This commit is contained in:
Stas
2022-04-15 09:33:59 -07:00
committed by GitHub
parent 22faa1b5db
commit 98875f0531
4 changed files with 37 additions and 24 deletions

View File

@ -229,7 +229,7 @@ public class LogTracer : ILogTracerInternal
public void Verbose(string message)
{
if (_logSeverityLevel >= SeverityLevel.Verbose)
if (_logSeverityLevel <= SeverityLevel.Verbose)
{
var caller = GetCaller();
foreach (var logger in _loggers)
@ -241,7 +241,7 @@ public class LogTracer : ILogTracerInternal
public void Info(string message)
{
if (_logSeverityLevel >= SeverityLevel.Information)
if (_logSeverityLevel <= SeverityLevel.Information)
{
var caller = GetCaller();
foreach (var logger in _loggers)
@ -253,7 +253,7 @@ public class LogTracer : ILogTracerInternal
public void Warning(string message)
{
if (_logSeverityLevel >= SeverityLevel.Warning)
if (_logSeverityLevel <= SeverityLevel.Warning)
{
var caller = GetCaller();
foreach (var logger in _loggers)
@ -265,7 +265,7 @@ public class LogTracer : ILogTracerInternal
public void Error(string message)
{
if (_logSeverityLevel >= SeverityLevel.Error)
if (_logSeverityLevel <= SeverityLevel.Error)
{
var caller = GetCaller();
foreach (var logger in _loggers)
@ -277,7 +277,7 @@ public class LogTracer : ILogTracerInternal
public void Critical(string message)
{
if (_logSeverityLevel >= SeverityLevel.Critical)
if (_logSeverityLevel <= SeverityLevel.Critical)
{
var caller = GetCaller();
foreach (var logger in _loggers)

View File

@ -17,17 +17,17 @@ public class Program
{
public async Async.Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
//TODO
//if correlation ID is available in HTTP request
//if correlation ID is available in Queue message
//log.ReplaceCorrelationId
var log = (ILogTracerInternal?)context.InstanceServices.GetService<ILogTracer>();
if (log is not null)
{
log.AddTags(new[] {
("InvocationId", context.InvocationId.ToString())
//TODO
//if correlation ID is available in HTTP request
//if correlation ID is available in Queue message
//log.ReplaceCorrelationId(Guid from request)
log.ReplaceCorrelationId(Guid.NewGuid());
log.AddTags(new[] {
("InvocationId", context.InvocationId.ToString())
});
}
@ -65,17 +65,21 @@ public class Program
)
.ConfigureServices((context, services) =>
services
.AddScoped<ILogTracer>(_ => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.NewGuid(), severityLevel: EnvironmentVariables.LogSeverityLevel()))
.AddSingleton<INodeOperations, NodeOperations>()
.AddSingleton<IEvents, Events>()
.AddSingleton<IWebhookOperations, WebhookOperations>()
.AddSingleton<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
.AddSingleton<ITaskOperations, TaskOperations>()
.AddSingleton<IQueue, Queue>()
.AddSingleton<ICreds, Creds>()
.AddSingleton<IStorage, Storage>()
.AddSingleton<IProxyOperations, ProxyOperations>()
.AddSingleton<IConfigOperations, ConfigOperations>()
.AddScoped<ILogTracer>(s => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.Empty, severityLevel: EnvironmentVariables.LogSeverityLevel()))
.AddScoped<INodeOperations, NodeOperations>()
.AddScoped<IEvents, Events>()
.AddScoped<IWebhookOperations, WebhookOperations>()
.AddScoped<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
.AddScoped<ITaskOperations, TaskOperations>()
.AddScoped<IQueue, Queue>()
.AddScoped<ICreds, Creds>()
.AddScoped<IStorage, Storage>()
.AddScoped<IProxyOperations, ProxyOperations>()
.AddScoped<IConfigOperations, ConfigOperations>()
//TODO: move out expensive resources into separate class, and add those as Singleton
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.
)
.Build();

View File

@ -3,6 +3,7 @@ using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
namespace Microsoft.OneFuzz.Service;
@ -57,8 +58,10 @@ public class TestHooks
{
await _events.SendEvent(new EventInstanceConfigUpdated(config));
var str = (new EntityConverter()).ToJsonString(config);
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(config);
await resp.WriteStringAsync(str);
return resp;
}
}

View File

@ -119,6 +119,12 @@ public class EntityConverter
});
}
public string ToJsonString<T>(T typedEntity) where T : EntityBase
{
var serialized = JsonSerializer.Serialize(typedEntity, _options);
return serialized;
}
public TableEntity ToTableEntity<T>(T typedEntity) where T : EntityBase
{
if (typedEntity == null)