CorrelationId icon indicating copy to clipboard operation
CorrelationId copied to clipboard

No 'ICorrelationIdProvider' has been registered but I use CorrelationIdGenerator

Open arphox opened this issue 4 years ago • 0 comments

Hi!

While setting up the usage of the nuget package (version 3.0.0), I encountered the following problem on application startup:

{
  "exception": {
    "ClassName": "System.InvalidOperationException",
    "Message": "No 'ICorrelationIdProvider' has been registered. You must either add the correlation ID services using the 'AddDefaultCorrelationId' extension method or you must register a suitable provider using the 'ICorrelationIdBuilder'.",
    "Data": null,
    "InnerException": null,
    "HelpURL": null,
    "StackTraceString": "   at CorrelationId.CorrelationIdMiddleware.Invoke(HttpContext context, ICorrelationContextFactory correlationContextFactory)\r\n   at HIDDEN"
  (...)
}

Which is unexpected as I don't want to use an ICorrelationIdProvider. I expect my setup to work, this is it:

Inside ConfigureServices

services.AddCorrelationId(options =>
{
    options.CorrelationIdGenerator = () => Guid.NewGuid().ToString("N");
    options.RequestHeader = LoggingConstants.CorrelationHeaderName;
    options.ResponseHeader = LoggingConstants.CorrelationHeaderName;
    options.AddToLoggingScope = true;
    options.LoggingScopeKey = LoggingConstants.SerilogCorrelationIdPropertyName;
});

Inside Configure:

app.UseCorrelationId();

According to the documentation of options.CorrelationIdGenerator, "When set, this function will be used instead of the registered ICorrelationIdProvider" which makes sense and I like it, but then do not require an ICorrelationIdProvider to be registered since it will never be used.

I understand that instead of AddCorrelationId I could use AddDefaultCorrelationId which would probably solve the problem since it registers a default Guid correlation id provider, but why would I register a default that should never be used and is forbidden to use?

arphox avatar Jan 05 '21 10:01 arphox