【发布时间】:2018-07-09 18:26:18
【问题描述】:
我有一个控制器 DailyExpenseController,我将 IQueryExecutor 类注入到控制器中。
DailyExpenseController
using HomeBudgetTrackingSystem.CrossCutting;
using HomeBudgetTrackingSystem.DTO;
using HomeBudgetTrackingSystem.Models;
using HomeBudgetTrackingSystem.Query;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace HomeBudgetTrackingSystem.Controllers
{
[Produces("application/json")]
[Route("api/DailyExpense/{Id}")]
public class DailyExpenseController : Controller
{
private readonly IQueryExecutor queryExecutor;
public DailyExpenseController(IQueryExecutor queryExecutor)
{
this.queryExecutor = queryExecutor;
}
[HttpPost]
[Route("Create")]
public async Task<IActionResult> CreateExpense(long Id, [FromBody] Expenditure expenditure)
{
var findEntity = new FindEntityQuery<Expense>(Id);
var find = queryExecutor.Execute(findEntity);
/*if (find == null)
return NotFound();*/
var createExpenses = new CreateExpenditure(expenditure);
var expenses = queryExecutor.Execute(createExpenses);
return Ok(expenses);
}
/*If I write this method, it will result in 500 Error, because this method has id which is confusing
with the variable Id declared with Controller*/
/*[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}*/
[HttpGet("{id2}")]
public string Get(long id)
{
return "value";
}
[HttpPost]
public void Post([FromBody]string value)
{
}
}
}
以下是我的 Autofac 配置详细信息
Startup.cs
using Autofac;
using Autofac.Extensions.DependencyInjection;
using HomeBudgetTrackingSystem.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace HomeBudgetTrackingSystem
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
//public void ConfigureServices(IServiceCollection services)
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
/*services.AddDbContext<BudgetContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BudgetConnection")
)); */
var builder = new ContainerBuilder();
builder.Populate(services);
builder.RegisterModule(new RepositoryHandlerModule());
//builder.RegisterType<QueryExecutor>().As<IQueryExecutor>();
ApplicationContainer = builder.Build();
return new AutofacServiceProvider(ApplicationContainer);
}
public IContainer ApplicationContainer { get; private set; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());
}
}
}
下面是我注册依赖项的模型生成器类
RepositoryHandlerModule.cs
using Autofac;
using HomeBudgetTrackingSystem.CrossCutting;
namespace HomeBudgetTrackingSystem.Repository
{
public class RepositoryHandlerModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<QueryExecutor>().As<IQueryExecutor>().InstancePerRequest();
}
}
}
我没有对 Program.cs 进行任何更改
程序.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace HomeBudgetTrackingSystem
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
以下是我收到的异常详情
Autofac.Core.DependencyResolutionException:无法解析类型“HomeBudgetTrackingSystem.CrossCutting.QueryExecutor”,因为无法找到它所属的生命周期范围。此注册公开了以下服务: - HomeBudgetTrackingSystem.CrossCutting.IQueryExecutor
详细信息 ---> 从请求实例的范围中看不到带有匹配“AutofacWebRequest”标签的范围。
如果您在执行 Web 应用程序期间看到此情况,通常表示注册为 per-HTTP 请求的组件正在被 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,总是从依赖解析器或请求生命周期范围请求依赖,而不是从容器本身。 (有关详细信息,请参阅内部异常。)---> Autofac.Core.DependencyResolutionException:从请求实例的范围内看不到带有匹配“AutofacWebRequest”标签的范围。
如果您在执行 Web 应用程序期间看到此情况,通常表示注册为 per-HTTP 请求的组件正在被 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,总是从依赖解析器或请求生命周期范围请求依赖,而不是从容器本身。 在 Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope) 在 Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration 注册,IResolveOperation 上下文,ISharingLifetimeScope mostNestedVisibleScope,IEnumerable
1 parameters) --- End of inner exception stack trace --- at Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope, IEnumerable1 参数) 在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration 注册,IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 参数) 在 Autofac.ResolutionExtensions.TryResolveService(IComponentContext 上下文,服务服务,IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 参数) 在 Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp,类型类型,类型 requiredBy,布尔 isDefaultParameterRequired) 在 lambda_method(闭包,IServiceProvider,对象 []) 在 Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.c__DisplayClass4_0.b__0(ControllerContext controllerContext) 在 Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态&下一个,范围&范围,对象&状态,布尔& isCompleted) 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__22.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(状态&下一个,范围&范围,对象&状态,布尔& isCompleted) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()
我已经提到了这些的多个链接。比较突出的是
http://autofac.readthedocs.io/en/latest/faq/per-request-scope.html http://autofac.readthedocs.io/en/latest/integration/aspnetcore.html (我更喜欢没有容器的配置) http://www.codedigest.com/posts/49/using-autofac-instead-of-inbuilt-di-container-with-in-aspnet-core-mvc(这是我介绍的)
谁能帮我解决这些问题?
【问题讨论】:
标签: c# dependency-injection autofac asp.net-core-1.1