简体   繁体   English

异步和等待:MVC中的错误

[英]Async and Await : Error In MVC

Hi I have a problem to use Async And Wait in my following method. 嗨,我在以下方法中使用“异步并等待”时遇到问题。 Basically I used Neevia Converter to Convert My Document and I want to wait my method until conversion to be completed. 基本上,我使用Neevia Converter转换了我的文档,我想等待我的方法直到转换完成。

Could you please help: 能否请你帮忙:

public void AsyncAndWaitTillExecutionMethod(string fileName) {
      CallForConverter(fileName);
    }

    private async void CallForConverter(string fileName) {
        var result = await CallForConverterAsync(fileName);
    }

    private Task<string> CallForConverterAsync(string fileName) {
        string fromLocation;
        string toLocation;

        // For  data source
        string fileFullName = fileName;
        fromLocation = "abc"; 
        toLocation = "xyz";
        return Task.Factory.StartNew(() => ConvertUsingNeevia(fromLocation, toLocation));

    }
    private string ConvertUsingNeevia(string from, string to) {
        string success = "false";

        int TimeOut = 3000;
        try {
            Neevia.docConverter NVDC = new Neevia.docConverter();
            int Res = NVDC.convertFile(from, to, TimeOut);
        }
        catch (Exception ex) {
            throw new Exception("Exception ", ex);
        }
        success = "True";
        return success;

    } 

It is giving me the Following Error From the CallForConverter() method. 它给了我来自CallForConverter()方法的以下错误。 An asynchronous operation cannot be started at this time. 目前无法启动异步操作。 Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle in MVC... 异步操作只能在异步处理程序或模块中或在MVC的Page生命周期中的某些事件期间启动...

Just remove all async , await , and (the horrible) StartNew from your code: 只需从代码中删除所有asyncawait和(可怕的) StartNew

public void AsyncAndWaitTillExecutionMethod(string fileName) {
    string fromLocation;
    string toLocation;

    // For  data source
    string fileFullName = fileName;
    fromLocation = "abc"; 
    toLocation = "xyz";
    ConvertUsingNeevia(fromLocation, toLocation);
}

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

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