【发布时间】:2014-05-14 21:03:41
【问题描述】:
让我从一个事实开始,这个问题的类似形式在这里被问到,但没有工作示例或可用的主题答案(我已经能够找到)。这篇文章将尝试提供一个更简洁的示例,并为此推进一个代码块。参考以下类似但不是结论性/工作示例的链接。
similar link #1 Background worker
similar link #2 Multi Threading in web service
similar link #3 threading in ASP.net
similar link #4 acknowledge quickly but continue processing
其他链接:
#2 Calling Web Service Async functions from web page
#3 MS ThreadPool.QueueUserWorkItem Method
#4 MS Multithreading Walk Through Forms based
问题是向使用 Web 服务的客户端返回响应,以便客户端可以在 Web 服务完成处理任务时继续前进。 Web 服务(身份验证后)从客户端接收文件名,该文件名应从远程 ftp 服务器检索。将文件下载到 Web 服务托管服务器后,即可释放客户端,因为文件名有效并已复制到本地服务器。
Web 服务器在向客户端返回一个简单的“真”布尔响应后,继续将下载的文件压缩到数据库中,并愉快地等待下一个文件的名称。
当前调用是一个 SOAP 消息,这是一个传统的 asmx Web 服务。
网络服务:
<WebService details .....
Public sourceFile As String
Public myData as Boolean = False
<WebMethod()> _
Public Function fileReady_Log2(ByVal user As String, ByVal PW As String, ByVal fileName As String) As String
' This function receives a web service call notifying this server to retrieve a file from a remote ftp server When the file is downloaded it is processed into this servers database.
'
' This routine attempts to process an ASYNC routine to respond to the call and release the caller as soon as possible while continuing processing requirements.
' Validation removed for simplicity
If authorized Then
' Need to retrieve ftp server information and pull down the file and load it to the database.
' Call Async function
sourceFile = filename
Dim myObject = New ftpThread()
myObject.SendDataAsync()
' Ok, now return the result to client.
Return myData
Dim ftpInfo As New ftpFileImport ' continue with processing the uploaded file.
results = ftpInfo.retrieveLogFile(fileName, deviceID, usr)
Return results
End If
Return results
End Function
目前这是调用函数的结构。我尝试了多种变体,并决定现在是停下来寻求帮助的好时机。
ftpThread.vb
Imports Microsoft.VisualBasic
Imports System.Threading
Public Class ftpThread
Public Sub SendDataAsync()
ThreadPool.QueueUserWorkItem(Sub(getFtpFileExists(sourceFile )))
End Sub
End Class
我在上面的单词 Sub 上也收到了 Expression Expected 异常。
ftp函数这个过程是同步的,需要异步的。
Public Class ftpFileExistsCheck
Public Sub getFtpFileExists()
' This all works without ASYNC
' Retrieve FTP credentials from DB
' Set up ftp request
' download the file.
' But continuing process to DB at this point causes client to wait.
myData = True
End Sub
End Class
我以为我可以从我所阅读的所有内容中弄清楚这一点,但我就是无法为 ASYNC 处理设置正确的编码。
【问题讨论】:
-
ASMX 是一项遗留技术,不应用于新开发。 WCF 或 ASP.NET Web API 应该用于 Web 服务客户端和服务器的所有新开发。一个提示:Microsoft 已停用 MSDN 上的 ASMX Forum。
-
@John Saunders 谢谢,我知道遗留技术。由于处理较大文件的延迟,这是对现有服务的增强。寻找快速修复认为线程可以做到这一点。
-
WCF 支持单向消息传递和补偿执行,这正是您想要的。如果可以,请采纳约翰的建议,然后继续。坦率地说,我认为只有肥皂服务器和客户端的自定义实现才能处理您的场景。
-
另外,最好不要在 Web 服务中执行长时间运行的任务。相反,创建一个单独的服务并将工作排队到单独的服务。您甚至可以使用 WCF 对请求进行排队,其他服务可以自行托管。
-
长时间运行预计不会超过 1 分钟左右,但在我们的案例中,这对于客户端应用程序来说已经很长了。
标签: vb.net multithreading web-services asynchronous asmx