【问题标题】:How to set handler for WebRequest.GetResponseAsync in IronRuby?如何在 IronRuby 中为 WebRequest.GetResponseAsync 设置处理程序?
【发布时间】:2014-07-29 11:13:41
【问题描述】:

我正在尝试:

request = WebRequest.Create( uri )
responseTask = request.GetResponseAsync
action = Action.new { process_request( sender, e ) }
task = Task.new( action )
responseTask.Wait( TimeSpan.new( -1 ) )
responseTask.ContinueWith( task )

但出现以下错误:

can't convert System::Threading::Tasks::Task into System::Action[System::Threading::Tasks::Task] (TypeError)  

UPD

开启

responseTask = request.GetResponseAsync  
action = Action[Task].new { process_request( sender, e ) }  
responseTask.Wait( TimeSpan.new( -1 ) )  
responseTask.ContinueWith( action )  

我收到此错误:

Found multiple methods for 'ContinueWith': ContinueWith(System::Action[System::Threading::Tasks::Task]), ContinueWith(System::Action[System::Threading::Tasks::Task[System::Net::WebResponse]]) (System::Reflection::AmbiguousMatchException)

【问题讨论】:

    标签: asynchronous webrequest ironruby


    【解决方案1】:

    ContinueWith 需要一个

    Action<Task> 
    

    还有其他重载,但不是针对任务。

    例如:

    action = Action.new {Console.WriteLine("action")}
    task = Task.new( action )
    action2 = Action[Task].new{Console.WriteLine("action2")}
    task.ContinueWith(action2)
    task.Start()
    #prints "action" then "action2".
    

    IronRuby 可能会让你跳过泛型类型特化 那是非泛型任务,这里是你的问题的完全类型化版本:

     action = Action[Task[WebResponse]].new { process_request( sender, e ) }
    

    【讨论】:

    • 你能告诉我工作的例子吗? msdn.microsoft.com/ru-ru/library/… Task 需要 Action 用于初始化参数。 responseTask.ContinueWith( action ) 得到同样的错误。
    • 我正在尝试但有错误,请查看 UPD 部分。
    猜你喜欢
    • 1970-01-01
    • 2022-10-04
    • 2013-01-28
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    相关资源
    最近更新 更多