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

View File

@ -17,17 +17,17 @@ public class Program
{ {
public async Async.Task Invoke(FunctionContext context, FunctionExecutionDelegate next) 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>(); var log = (ILogTracerInternal?)context.InstanceServices.GetService<ILogTracer>();
if (log is not null) if (log is not null)
{ {
log.AddTags(new[] { //TODO
("InvocationId", context.InvocationId.ToString()) //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) => .ConfigureServices((context, services) =>
services services
.AddScoped<ILogTracer>(_ => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.NewGuid(), severityLevel: EnvironmentVariables.LogSeverityLevel())) .AddScoped<ILogTracer>(s => new LogTracerFactory(GetLoggers()).CreateLogTracer(Guid.Empty, severityLevel: EnvironmentVariables.LogSeverityLevel()))
.AddSingleton<INodeOperations, NodeOperations>() .AddScoped<INodeOperations, NodeOperations>()
.AddSingleton<IEvents, Events>() .AddScoped<IEvents, Events>()
.AddSingleton<IWebhookOperations, WebhookOperations>() .AddScoped<IWebhookOperations, WebhookOperations>()
.AddSingleton<IWebhookMessageLogOperations, WebhookMessageLogOperations>() .AddScoped<IWebhookMessageLogOperations, WebhookMessageLogOperations>()
.AddSingleton<ITaskOperations, TaskOperations>() .AddScoped<ITaskOperations, TaskOperations>()
.AddSingleton<IQueue, Queue>() .AddScoped<IQueue, Queue>()
.AddSingleton<ICreds, Creds>() .AddScoped<ICreds, Creds>()
.AddSingleton<IStorage, Storage>() .AddScoped<IStorage, Storage>()
.AddSingleton<IProxyOperations, ProxyOperations>() .AddScoped<IProxyOperations, ProxyOperations>()
.AddSingleton<IConfigOperations, ConfigOperations>() .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(); .Build();

View File

@ -3,6 +3,7 @@ using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http; using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
namespace Microsoft.OneFuzz.Service; namespace Microsoft.OneFuzz.Service;
@ -57,8 +58,10 @@ public class TestHooks
{ {
await _events.SendEvent(new EventInstanceConfigUpdated(config)); await _events.SendEvent(new EventInstanceConfigUpdated(config));
var str = (new EntityConverter()).ToJsonString(config);
var resp = req.CreateResponse(HttpStatusCode.OK); var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteAsJsonAsync(config); await resp.WriteStringAsync(str);
return resp; 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 public TableEntity ToTableEntity<T>(T typedEntity) where T : EntityBase
{ {
if (typedEntity == null) if (typedEntity == null)