相关疑难解决方法(0)

.net core DI中的异步提供程序

我只是想知道async/await在DI期间是否有可能.

执行以下操作,DI无法解析我的服务.

services.AddScoped(async provider => 
{
  var client = new MyClient();
  await client.ConnectAsync();
  return client;
});
Run Code Online (Sandbox Code Playgroud)

以下作品完美无缺.

services.AddScoped(provider => 
{
  var client = new MyClient();
  client.ConnectAsync().Wait();
  return client;
});
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection async-await asp.net-core-mvc asp.net-core-webapi

11
推荐指数
1
解决办法
5141
查看次数