ChilliCream / graphql-workshop

Getting started with GraphQL on ASP.NET Core and Hot Chocolate - Workshop
465 stars 199 forks source link

[ScopedService] is Obsolete, but using [Service(ServiceKind.Pooled)] fails #106

Closed pankaj-kansodariya closed 1 month ago

pankaj-kansodariya commented 1 year ago

As per GraphQL-Workshop and Session #3 Understanding GraphQL query execution and DataLoader

I followed Steps 3, 4, 5, 6 & 7 and everything worked as expected. But Warning is shown while using [ScopedService] that it is Obsolete and I should use [Service(ServiceKind.Pooled)] or [Service(ServiceKind.Resolver)]. But when I use either [Service(ServiceKind.Pooled)] or [Service(ServiceKind.Resolver)] then getting error as following.

"No service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`1[ApplicationDbContext]' has been registered."

PascalSenn commented 1 year ago

If you still want use the pooled db context with 13, the easiest thing to do is to register the db context on the schema as pooled. RegisterDbContext<FooContext>(Pooled). You then do not need any attribute

pankaj-kansodariya commented 1 year ago

So if we consider to solve the issue of Parallel execution of DbContext, we have following multiple options

Option 1: If DbContext is registered as Services.AddDbContext() then need to use [Service(ServiceKind.Synchronized)]

Option: 2 If DbContext is registered as Services.AddDbContext() then need to use IRequestExecutorBuilder.RegisterDbContext(DbContextKind.Synchronized) and no need specify [Service] attribute.

Option: 3 If DbContext is registered as Services.AddPooledDbContextFactory() then need to use IRequestExecutorBuilder.RegisterDbContext(DbContextKind.Pooled) and no need specify [Service] attribute.

in that case, which scenario is suitable for using ObjectFieldDescriptorExtensions and UseApplicationDbContextAttribute ?

PascalSenn commented 1 year ago

@pankaj-kansodariya You do not need the UseApplicationDbContextAttribute anymore when you do option 3

If you do not have a pooled db context then you can also just use the DbContextKind.Resolver where we open a DI scope per resolver. But if you already have it set up to use the Pooled version of it, just go for DbContextKind.Pooled and do not worry about UseDbContext and [Service]

jeffvan98 commented 1 year ago

It would be helpful to get an updated set of instructions for Session #3, just like @pankaj-kansodariya mentioned.

weirdyang commented 1 year ago

I am trying to resolve this using option 3, but there doesn't seem to be RegisterDbContext ?

image

using "HotChocolate.AspNetCore" Version="13.5.1"

solution: Add package HotChocolate.Data.EntityFramework

glen-84 commented 1 month ago

I'm closing this issue as outdated, as we have an updated workshop on the main branch, for Hot Chocolate 14.