【发布时间】:2021-01-31 18:20:45
【问题描述】:
我已在global.asax 中将MediatR 添加到OnApplicationStarted
但我的控制器无法解决。
它返回一个错误:
{
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'NotificationApiController'. Make sure that the controller has a parameterless public constructor.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Type 'MyDomain.MyProject.Controllers.NotificationApiController' does not have a default constructor",
"ExceptionType": "System.ArgumentException",
"StackTrace": " at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}
}
global.asax:
var builder = new ContainerBuilder();
/* MVC Controllers */
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();
/* WebApi Controllers */
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
/* Umbraco Controllers */
builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
/* Custom Api Controllers */
builder.RegisterApiControllers(typeof(Controllers.SearchResultsApiController).Assembly);
builder.RegisterModule<WebApiConfig>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver =
new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy =
IncludeErrorDetailPolicy.Always;
WebApiConfig:
public class WebApiConfig : Module
{
protected override void Load(ContainerBuilder builder)
{
// Register custom types with Autofac
/* Third-party types */
// This didn't work so I added the below line with the explicit handler
builder.AddMediatR(this.GetType().Assembly);
// But it didn't make any difference
builder.AddMediatR(typeof(Index).Assembly);
/* Umbraco context types */
ApplicationContext applicationContext = ApplicationContext.Current;
builder.RegisterInstance(applicationContext.Services.ContentService)
.As<IContentService>();
builder.RegisterInstance(applicationContext.Services.MemberService)
.As<IMemberService>();
//builder.Register(c => UmbracoContext.Current).AsSelf();
builder.Register(c => UmbracoContext.Current).InstancePerRequest();
builder.Register(x => new UmbracoHelper(UmbracoContext.Current))
.InstancePerRequest();
}
}
控制器:
public class SearchResultsApiController : UmbracoApiController
{
private readonly IMediator _mediator;
public SearchResultsApiController(IMediator mediator)
{
_mediator = mediator;
}
}
我正在使用 .NET 4.7.2(如果重要的话,还有 Umbraco 7.15.3)。
【问题讨论】:
-
请包含异常的完整堆栈跟踪(以及任何内部异常的所有相关信息)。
-
我添加了完整的堆栈跟踪。但是我解决了这个问题,请参阅我的答案
-
异常表明,由于某种原因,您正在使用默认的控制器激活器,而不是您尝试设置的 Autofac。我不知道为什么会发生这种情况,但在调试器中单步执行应用程序可能会揭示原因。
标签: asp.net dependency-injection global-asax asp.net-apicontroller mediatr