ABM Abdul Muttalib
ABM Abdul Muttalib

Reputation: 205

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.

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. 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...

Upvotes: 1

Views: 498

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456437

Just remove all async, await, and (the horrible) StartNew from your code:

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

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

Upvotes: 4

Related Questions