Cannot Await Void - Parameter within Async Method C#

665 Views Asked by At

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?

1

There are 1 best solutions below

0
Access Denied On BEST ANSWER

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;
    }
}