简体   繁体   English

处理执行时间太长而无法等待的方法

[英]Handling a method that takes too long to execute and is not awaitable

We are building a custom way to process timesheets using eConnect. 我们正在构建一种使用eConnect处理时间表的自定义方式。 A method is exposed that allows out timesheets documents to be submitted to GP. 公开了一种方法,允许将时间表文档提交给GP。 This method is run synchronously, but can take a long time to complete. 此方法同步运行,但可能需要很长时间才能完成。 How can I handle this so that the user's client can make additional requests in the meantime? 我该如何处理这个问题,以便用户的客户可以在此期间提出额外的请求?

I have attempted to use async/await on this method, but because the method isn't awaitable this will not work. 我试图在此方法上使用async / await,但由于该方法不可行,因此无效。 The method depends on a windows service. 该方法取决于Windows服务。 I have researched potentially wrapping it in Task.Run but have hesitations since this sounds like a bad practice. 我已经研究过可能将它包装在Task.Run中但有犹豫,因为这听起来像是一种不好的做法。

    public bool SaveTimesheets(string ConnectionString, List<PATimeSheetsType> Timesheets)
    {
        string timesheetDocument = string.Empty;
        //Creating timesheet document

        bool result = false;

        eConnectMethods eConnectMethods = new eConnectMethods();

        //CreateEntity takes minutes to complete and return
        result = eConnectMethods.CreateEntity(ConnectionString, timesheetDocument);


        return result;
    }

The behavior I currently get is that, if for instance I am doing an ajax calls on the client-side, the call doesn't seem to get there while the method above is executing. 我目前得到的行为是,如果我在客户端进行ajax调用,那么当上面的方法执行时,调用似乎没有到达那里。 I would like it so that the method call executes in the background so that the client can still communicate with the server to execute other requests. 我希望方法调用在后台执行,以便客户端仍然可以与服务器通信以执行其他请求。

How can I handle this so that the user's client can make additional requests in the meantime? 我该如何处理这个问题,以便用户的客户可以在此期间提出额外的请求?

The easiest solution is to change your session state to be None or Read-Only (for both this and the other requests). 最简单的解决方案是将会话状态更改为“无”或“只读”(对于此请求和其他请求)。 Then ASP.NET will allow multiple client requests for the same session. 然后,ASP.NET将允许同一会话的多个客户端请求。

If you're on pre-Core, the session state docs are here . 如果您使用的是pre-Core,那么会话状态文档就在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM