question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is using app.UseMiddleware<IServiceProvidersFeature>() not recommended?

See original GitHub issue

Hi team,

I found sample code on the gRPC project about not allocating request scopes: https://github.com/grpc/grpc-dotnet/blob/master/perf/benchmarkapps/GrpcAspNetCoreServer/Startup.cs

The class of interest is ServiceProvidersMiddleware.

The way i get my application to perpetually error is to:

  1. Start service
  2. Start client
  3. Perform one successful service call
  4. Restart service
  5. The client using the same gRPC channel now always triggers the following exception:

Connection id ““0HM3V8CGMTP5C””, Request id "“0HM3V8CGMTP5C:00000003"”: An unhandled exception was thrown by the application. System.ObjectDisposedException: IFeatureCollection has been disposed. Object name: ‘Collection’. at Microsoft.AspNetCore.Http.Features.FeatureReferences1.ThrowContextDisposed() at Microsoft.AspNetCore.Http.Features.FeatureReferences1.ContextDisposed() at Microsoft.AspNetCore.Http.Features.FeatureReferences1.Fetch[TFeature](TFeature& cached, Func2 factory) at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_StatusCode(Int32 value) at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleChallengeAsync(AuthenticationProperties properties) at Microsoft.AspNetCore.Authentication.AuthenticationHandler1.ChallengeAsync(AuthenticationProperties properties) at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication1 application)

Can you please confirm if this error is to be expected if one uses an IServiceProvidersFeature implemenation to avoid creating the request scope?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Nov 3, 2020

I would stay away from trying to do this as it breaks too many assumptions about scoped (and sometimes transient) services

1reaction
davidfowlcommented, Nov 2, 2020

That sample is a just bad implementation. I assume it existed purely for measuring a very specific use case but it’s broken as it doesn’t actually create a scoped container

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Adding custom middleware not working when using ...
The problem is the "InvokeAsync" method of your custom middleware program is not be executed even though you built in it in "Configure"...
Read more >
Custom middleware with dependency injection in ASP.NET ...
In ASP.NET Core middleware functions are defined in the Configure method of the Startup class. This method is executed automatically by the ...
Read more >
Custom ASP.NET Core Middleware Example
NET Core app's HTTP request handling pipeline can be easily customized by ... The recommended way of providing web services with ASP.
Read more >
Dependency injection in ASP.NET Core
Learn how ASP.NET Core implements dependency injection and how to use it.
Read more >
Styles of Writing ASP.NET Core Middleware
In this post, I discuss the differences between convention and factory styles of writing middleware in ASP.NET Core along with the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found