asp.net - MediatR 未在 api 控制器中解析
问题描述
我已经添加MediatR
到OnApplicationStarted
global.asax
但这并不能解决我的控制器。
它返回一个错误:
{
"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)。
解决方案
问题是我在两个单独的项目中有 api 控制器,所以解析器可能找不到合适的。
如果一个项目中的 api 控制器正常工作,则第二个项目中的控制器无法显示上述错误。
我已将所有 api 控制器合并到一个项目中,现在一切正常。
推荐阅读
- python - 为什么我在 Python 中的堆栈上出现错误?
- python - 是否有将外部文件导入主文件的功能?
- java - Java,不适用于参数,仿射变换?
- python - Pandas 在错误的图表中显示数据
- javascript - 我的 Axios GET 请求中只有 1 个无法在我的 React 组件中工作
- sass - 从 SCSS 中的变量分配多个值
- node.js - “无法读取未定义的属性'map'”反应和node.js
- swift - 在 SwiftUI 中将数据传递到 ViewBuilder 闭包的正确方法是什么?
- javascript - 函数中的 JavaScript 可选解构参数
- javascript - 在反应本机中按下字段时过滤项目