My controller actions are called twice, and I can't figure out why !!
This is a typical controller:
[Authorize]
public abstract class BaseController : ApiController
{
protected readonly ILogService logService;
protected HttpResponseMessage response = null;
protected BaseController(ILogService logService)
{
this.logService = logService;
}
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
}
}
public abstract class EntityController<T> : BaseController
{
public EntityController(ILogService logService) : base(logService) { }
[HttpGet]
public abstract IHttpActionResult Get(int id);
[ActionName("Find")]
[HttpGet]
public virtual IHttpActionResult Find(string param)
{
return null;
}
[HttpPost]
[ValidateModel]
public abstract IHttpActionResult Create(T dto);
[HttpPut]
[ValidateModel]
public abstract IHttpActionResult Update(T dto);
[HttpDelete]
public abstract IHttpActionResult Delete(int id);
}
public class CustomerTypeController : EntityController<CustomerTypeDTO>
{
private readonly ICustomerTypeService customerTypeService;
public CustomerTypeController(ILogService logService,
ICustomerTypeService customerTypeService)
: base(logService)
{
this.customerTypeService = customerTypeService;
}
public override IHttpActionResult Get(int id)
{
var item = Mapper.Map<CustomerTypeDTO>(customerTypeService.Get(id));
return Ok<CustomerTypeDTO>(item);
}
public override IHttpActionResult Find(string param)
{
CustomerType modelItem = customerTypeService.Get(x => x.Abbr == param);
CustomerTypeDTO item = Mapper.Map<CustomerTypeDTO>(modelItem);
return Ok<CustomerTypeDTO>(item);
}
}
Using tracing, I find this in my log files:
Request;;;"httpurlhere"/MyWebAPI/CustomerType/Find?param=100
MessageHandlers;ServerCompressionHandler;SendAsync;
Controllers;DefaultHttpControllerSelector;SelectController;Route='controller:CustomerType,action:Find'
Controllers;DefaultHttpControllerSelector;SelectController;CustomerType
Controllers;HttpControllerDescriptor;CreateController;
Controllers;HttpControllerDescriptor;CreateController;MyWebAPI.API.Controllers.Reference.CustomerTypeController
Controllers;CustomerTypeController;ExecuteAsync;
Action;ApiControllerActionSelector;SelectAction;
Action;ApiControllerActionSelector;SelectAction;Selected action 'Find(String param)'
Filters;AuthorizeAttribute;OnAuthorizationAsync;
Filters;AuthorizeAttribute;OnAuthorizationAsync;
ModelBinding;HttpActionBinding;ExecuteBindingAsync;
ModelBinding;ModelBinderParameterBinding;ExecuteBindingAsync;Binding parameter 'param'
ModelBinding;ModelBinderParameterBinding;ExecuteBindingAsync;Parameter 'param' bound to the value '100'
ModelBinding;HttpActionBinding;ExecuteBindingAsync;Model state is valid. Values: param=100
Filters;ValidateModelAttribute;OnActionExecutingAsync;Action filter for 'Find(String param)'
Filters;ValidateModelAttribute;OnActionExecutingAsync;
Action;ApiControllerActionInvoker;InvokeActionAsync;Action='Find(param=100)'
Action;ReflectedHttpActionDescriptor;ExecuteAsync;Invoking action 'Find(param=100)'
Action;ReflectedHttpActionDescriptor;ExecuteAsync;Action returned 'System.Web.Http.Results.OkNegotiatedContentResult`1[MyWebAPI.CustomerTypeDTO]'
Formatting;DefaultContentNegotiator;Negotiate;Type='CustomerType', formatters=[JsonMediaTypeFormatterTracer, XmlMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer]
Formatting;JsonMediaTypeFormatter;GetPerRequestFormatterInstance;Obtaining formatter of type 'JsonMediaTypeFormatter' for type='CustomerType', mediaType='application/json; charset=utf-8'
Formatting;JsonMediaTypeFormatter;GetPerRequestFormatterInstance;Will use same 'JsonMediaTypeFormatter' formatter
Formatting;DefaultContentNegotiator;Negotiate;Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'
Action;ApiControllerActionInvoker;InvokeActionAsync;
Filters;ValidateModelAttribute;OnActionExecutedAsync;Action filter for 'Find(String param)'
Filters;ValidateModelAttribute;OnActionExecutedAsync;
Controllers;CustomerTypeController;ExecuteAsync;
Formatting;JsonMediaTypeFormatter;WriteToStreamAsync;Value='MyWebAPI.CustomerTypeDTO', type='CustomerType', content-type='application/json; charset=utf-8'
Formatting;JsonMediaTypeFormatter;WriteToStreamAsync;
MessageHandlers;ServerCompressionHandler;SendAsync;
Request;;;Content-type='application/json; charset=utf-8', content-length=93
Controllers;CustomerTypeController;Dispose;
Controllers;CustomerTypeController;Dispose;
I checked the client side and I can confirm that the action is requested by the client only once.
EDIT: I checked how many times the database hits this particular action (Find), and this only once.
So, the action is called once, but the constructor, other methods of the base class and methods of removal are always called twice.
Why is this happening?
source
share