简体   繁体   English

无法等待 Void - 异步方法 C# 中的参数

[英]Cannot Await Void - Parameter within Async Method C#

I'm trying to test the example of the Titanium Proxy Server .我正在尝试测试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);我收到了await e.SetRequestBodyString(bodyString);行的错误await e.SetRequestBodyString(bodyString); and await e.SetRequestBody(bodyBytes);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.有错误消息指出Cannot await 'void'并且它将方法SessionEventArgs的参数称为无效方法本身。

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 .这是异步(SetRequestBody,SetRequestBodyString),但现在它的同步,你会得到异常喜欢这个

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

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

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