简体   繁体   中英

Cannot Await Void - Parameter within Async Method C#

I'm trying to test the example of the Titanium Proxy Server . However, after copying the example within the 'Read Me' section exactly, I am stuck with an error I can't resolve.

Within this method:

public async Task OnRequest(object sender, SessionEventArgs e)
{
    Console.WriteLine(e.WebSession.Request.Url);
    var requestHeaders = e.WebSession.Request.Headers;
    var method = e.WebSession.Request.Method.ToUpper();
    if ((method == "POST" || method == "PUT" || method == "PATCH"))
    {
        byte[] bodyBytes = await e.GetRequestBody();
        await e.SetRequestBody(bodyBytes);
        string bodyString = await e.GetRequestBodyAsString();
        await e.SetRequestBodyString(bodyString);
        e.UserData = e.WebSession.Request;
    }
}

I am getting errors for the lines await e.SetRequestBodyString(bodyString); and await e.SetRequestBody(bodyBytes); .

There error message states Cannot await 'void' and it is referring to the parameter within the method SessionEventArgs as a void method itself.

How do I resolve this? Am I doing something wrong as the example code is specifically as written above?

Their doc is incorrect. It was async (SetRequestBody, SetRequestBodyString), but now it's sync and you get exception like this .

public async Task OnRequest(object sender, SessionEventArgs e)
{
    Console.WriteLine(e.WebSession.Request.Url);
    var requestHeaders = e.WebSession.Request.Headers;
    var method = e.WebSession.Request.Method.ToUpper();
    if ((method == "POST" || method == "PUT" || method == "PATCH"))
    {
        byte[] bodyBytes = await e.GetRequestBody();
        e.SetRequestBody(bodyBytes);
        string bodyString = await e.GetRequestBodyAsString();
        e.SetRequestBodyString(bodyString);
        e.UserData = e.WebSession.Request;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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