C# Web API控制器动作调用两次

C# Web API控制器动作调用两次,c#,asp.net-web-api,C#,Asp.net Web Api,我的控制器动作被调用了两次,我不知道为什么 这是一个典型的控制器: [Authorize] public abstract class BaseController : ApiController { protected readonly ILogService logService; protected HttpResponseMessage response = null; protected BaseController(ILogService logServic

我的控制器动作被调用了两次,我不知道为什么

这是一个典型的控制器:

[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);

        //check authorization here
    }
}

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);
    }
}
我已经检查了客户端,我可以确认该操作仅由客户端请求一次

编辑: 我检查了数据库在该特定操作(Find)上被命中的次数,但只有一次

因此,操作被调用一次,但是构造函数、其他基类方法和dispose方法总是被调用两次


为什么会发生这种情况?

我遇到了完全相同的问题,我的一些控制器操作被调用了两次。我发现这些操作和其他操作之间的唯一区别是,这些操作比其他操作花费更多的时间来执行

我意识到我的客户代码是这个问题的原因。在我的例子中,我使用了
截击
进行网络通话
Volley
具有一些重试调用web服务方法的策略。这意味着如果服务器响应太晚,
Volley
可以重试调用。默认情况下,
Volley
在1.5秒内未收到任何响应时重试呼叫。我更改了客户端代码(截击重试策略)并解决了问题。 以下是我所做的更改:

req.setRetryPolicy(new DefaultRetryPolicy(
                   (int) TimeUnit.SECONDS.toMillis(7), //time out before retry
                   0, //number of retries
                   DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

我希望这能有所帮助。

如果您的控制器被调用两次,很可能是调用代码,我建议您在类似fiddler的内容中确认这一点,然后发布调用代码。每一个操作还是仅特定操作?你需要更具体一点。@James编辑我的问题时遇到了完全相同的问题。谢谢
req.setRetryPolicy(new DefaultRetryPolicy(
                   (int) TimeUnit.SECONDS.toMillis(7), //time out before retry
                   0, //number of retries
                   DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));