Django 文档中的示例:
def index(request):
return HttpResponse('<h1>hellworld!</h1>')
def detail(request, question_id):
return HttpResponse("Question: %s" % question_id)
Run Code Online (Sandbox Code Playgroud)
由于request从不使用该参数,为什么要在每个函数签名中包含它?
我的aspnet核心应用需要在指定的时间间隔内抓取数据。我选择实现IHostedService使其与api并行运行。此托管服务需要注入一些服务。现在我在启动时注册它们,但出现此错误:
System.InvalidOperationException:'无法从单例'Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor'中使用作用域服务'IXService'。
我的startup.cs:
services.AddScoped<IXService, XService>();
services.AddHostedService<MyHostedService>();
Run Code Online (Sandbox Code Playgroud)
我在DbContext中遇到了类似的问题,我通过/sf/answers/3385825411/解决了这个问题,但是这次我需要通过更深层次的依赖注入并在每个服务中处理IServiceScopeFactory优雅的解决方案。
c# dependency-injection asp.net-core asp.net-core-hosted-services