我是 GraphQL 世界的新人,正在尝试找到一种拥有多种查询类型或如何将查询类型拆分为多个文件的方法。我使用 Hot Chocolate for Asp.Net Core,一切看起来都很好并且有效。但是,如果我需要在一个 GraphQL API 中组合几个查询怎么办?一些完全不相关的东西,如 DogsQuery 和 CarsQuery。
在 Asp.Net 中,我写的类似于:
public void ConfigureServices(IServiceCollection services)
{
services
.AddGraphQLServer()
//.AddQueryType<DogsQuery>()
.AddQueryType<CarsQuery>();
}
Run Code Online (Sandbox Code Playgroud)
如果我同时只使用一个查询类(狗或汽车),它就可以正常工作。但如何同时使用两者呢?我搜索了很多但找不到答案。
我需要一段代码,根据参数键只允许同时由 1 个线程执行:
private static readonly ConcurrentDictionary<string, SemaphoreSlim> Semaphores = new();
private async Task<TModel> GetValueWithBlockAsync<TModel>(string valueKey, Func<Task<TModel>> valueAction)
{
var semaphore = Semaphores.GetOrAdd(valueKey, s => new SemaphoreSlim(1, 1));
try
{
await semaphore.WaitAsync();
return await valueAction();
}
finally
{
semaphore.Release(); // Exception here - System.ObjectDisposedException
if (semaphore.CurrentCount > 0 && Semaphores.TryRemove(valueKey, out semaphore))
{
semaphore?.Dispose();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我时不时地收到错误:
The semaphore has been disposed. : System.ObjectDisposedException: The semaphore has been disposed.
at System.Threading.SemaphoreSlim.CheckDispose()
at System.Threading.SemaphoreSlim.Release(Int32 releaseCount)
at Project.GetValueWithBlockAsync[TModel](String valueKey, Func`1 valueAction) …Run Code Online (Sandbox Code Playgroud)