我ApiController
为我的项目创建了一个自定义基础:
public abstract class MyApiControllerBase : ApiController
{
private IContextService<DomainUnitOfWork> ContextService;
private UnitOfWorkScopeBase<DomainUnitOfWork> _unitOfWorkScope;
private bool _autoDispose;
public bool DisposeUnitOfWorkOnResultExecuted
{
get { return this._autoDispose; }
set { this._autoDispose = value; }
}
protected DomainUnitOfWork UnitOfWork
{
get { return UnitOfWorkScope.Current; }
}
private UnitOfWorkScopeBase<DomainUnitOfWork> UnitOfWorkScope
{
get
{
if (this._unitOfWorkScope == null)
{
this._unitOfWorkScope = new PerRequestUnitOfWorkScope<DomainUnitOfWork>(this.LightSpeedContext);
}
return this._unitOfWorkScope;
}
}
private void DisposeUnitOfWorkScope()
{
if (this._autoDispose && this._unitOfWorkScope != null && this._unitOfWorkScope.HasCurrent)
{
DomainUnitOfWork current = this._unitOfWorkScope.Current;
current.Dispose();
}
}
public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
return base.ExecuteAsync(controllerContext, cancellationToken)
.ContinueWith<HttpResponseMessage>(ant =>
{
DisposeUnitOfWorkScope();
return ant.Result;
});
}
public MyApiControllerBase(IContextService<DomainUnitOfWork> contextService)
{
ContextService = contextService;
this._autoDispose = true;
}
~MyApiControllerBase()
{
Dispose(false);
}
public new void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeUnitOfWorkScope();
ContextService = null;
}
base.Dispose(disposing);
}
protected LightSpeedContext<DomainUnitOfWork> LightSpeedContext
{
get { return ContextService.Context; }
}
}
接下来,我创建了一个从 MyApiControllerBase 派生的控制器:
public class MyApiController : MyApiControllerBase
{
(...)
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get()
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(int pageIdx, int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(string id)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(string id, int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(string id, int pageIdx, int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public HttpResponseMessage Post(MyDto dto)
{
(...)
}
public MyApiController(
IContextService<DomainUnitOfWork> contextService)
: base(contextService)
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
(...)
}
base.Dispose(disposing);
}
}
事情是 :
我添加了一个 API 帮助页面。每当 Controller 派生自MyApiControllerBase
时,POST 方法都不会显示。每当我从 派生时ApiController
,一切都很好。此外,从我派生时,MyApiControllerBase
我实际上无法发布任何内容。
谁能解释这种行为?我究竟做错了什么?
请注意,我使用 Ninject 作为我的依赖解析和 LightSpeed 作为我的 ORM。
*转自http://forums.asp.net/t/1855019.aspx/1?Problems+using+custom+ApiControllers