How to access DI services declared as Scoped within AddResiliencePipeline #2502
Replies: 2 comments 1 reply
-
|
Could you please share with us how the logger is registered? BTW you can create a new scope anytime via the using var scope = context.ServiceProvider.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger>(); |
Beta Was this translation helpful? Give feedback.
-
|
@peter-csala Thanks for your response. Creating a scope via the Here is how the logger is registered. The logger is an public static class ServiceCollectionExtensions
{
public static IServiceCollection AddConnectorFramework(this IServiceCollection services)
{
...etc
services.AddScoped(typeof(ITracerService), typeof(TracerService));
...etc
return services;
}
}Then there is middleware that runs and sets up an instance of Adding of the middleware (Program.cs): IHost host = new HostBuilder()
.ConfigureFunctionsWebApplication(workerApplication =>
{
workerApplication.UseMiddleware<ClientMiddleware>();
workerApplication.UseMiddleware<TracingMiddleware>(); //<-----------------
})
.ConfigureServices(services =>
{
...etcMiddleware itself: namespace ConnectorRequest.Middleware
{
public class TracingMiddleware : IFunctionsWorkerMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
try
{
ExecuteLogic(context);
}
catch { }
await next(context);
}
public static void ExecuteLogic(FunctionContext context)
{
ITracerService tracerService = context.InstanceServices.GetRequiredService<ITracerService>();
// trace id
if (context.BindingContext.BindingData.TryGetValue("TraceId", out object? traceId))
{
if (traceId is string traceIdStr)
{
if (!string.IsNullOrEmpty(traceIdStr))
{
tracerService.SetTraceId(traceIdStr);
}
}
}
// connector code
if (context.BindingContext.BindingData.TryGetValue("ConnectorCode", out object? connectorCode))
{
if (connectorCode is string connectorCodeStr)
{
if (!string.IsNullOrEmpty(connectorCodeStr))
{
tracerService.SetConnectorCode(connectorCodeStr);
}
}
}
// connector function code
if (context.BindingContext.BindingData.TryGetValue("ConnectorFunctionCode", out object? connectorFunctionCode))
{
if (connectorFunctionCode is string connectorFunctionCodeStr)
{
if (!string.IsNullOrEmpty(connectorFunctionCodeStr))
{
tracerService.SetConnectorFunctionCode(connectorFunctionCodeStr);
}
}
}
string invocationTypeCode = "HTTP";
string? queueMessageId = null;
int queueDequeueCount = int.MinValue;
DateTime queueInsertionTime = DateTime.MinValue;
if (context.IsQueueTrigger())
{
invocationTypeCode = "QUEUE";
queueMessageId = context.GetBindingData("Id");
int.TryParse(context.GetBindingData("DequeueCount"), out queueDequeueCount);
string? dateValue = context.GetBindingData("InsertionTime");
if (dateValue != null)
{
DateTime.TryParse(dateValue.Replace("\"", ""), out queueInsertionTime);
}
}
FunctionContextInfo functionContextInfo = new(
invocationTypeCode,
context.InvocationId,
context.FunctionDefinition.Name,
queueDequeueCount == int.MinValue ? null : queueDequeueCount,
queueInsertionTime == DateTime.MinValue ? null : queueInsertionTime,
queueMessageId);
tracerService.SetFunctionContextInfo(functionContextInfo);
}
}
}So when my |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone.
In the doc Dependency injection, it is noted that you can access
contextfrom withinAddResiliencePipelinelike soIn the project in which I'm working, a specific logger instance is configured and registered as a
scopedservice. I need access to that instance from within theResiliencePipeline, so that I can log retries. But when the time comes to execute the pipeline, thecontextpassed in to the builder doesn't seem to have the samescopeas the executing scope, because when IGetRequiredServicefor my logger, it's not the correct instance.Do you have any guidance on how I can get the correct, scoped instance of my logger? Thank you for your time and consideration.
My service collection extension:
and during execution (note that the logger instance is correct here)
Beta Was this translation helpful? Give feedback.
All reactions