- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要一个控制台应用程序来调用 web 方法。
必须是异步的,没有超时(我们不知道这个方法处理任务需要多少时间。
这样好吗:
[WebMethod]
[SoapDocumentMethod(OneWay = true)]
??
最佳答案
如果您需要结果,请不要使用单向
首先,如果您需要方法的响应,您不需要 [SoapDocumentMethod(OneWay = true)]
。此属性创建一个“即发即弃”调用,它从不向调用者返回响应并且必须返回 void
。相反,使用常规方法调用并将其称为异步。
一种还是两种方法?
如果您使用的是 ASMX,则有两种基本解决方案:一种超时时间很长的方法,或两种方法(如@Aaronaught suggested above):一种启动操作并返回操作 ID,另一个传递 ID 并检索结果(如果可用)。
就我个人而言,在大多数情况下我不推荐这种两种方法的方法,因为它涉及到额外的复杂性,包括:
Request
和 Response
是 not available when called from a background task launched with ThreadPool.QueueUserWorkItem
. 是的,在某些情况下,2 法方法可以更好地扩展,并且对客户端和服务器之间断开的网络连接更有弹性。如果您需要在数小时后获取结果,则需要考虑这一点。但是你的操作只需要几分钟,你可以保证客户端将保持连接,考虑到 2-method 方法的额外开发复杂性,我认为它是最后的手段,只有在 one-method 解决方案没有的情况下才使用满足您的需求。
无论如何,解决方案需要两部分。首先,您需要从客户端异步调用该方法。其次,您需要延长客户端和服务器的超时时间。我在下面介绍了两者。
异步调用 ASMX Web 服务
要从命令行应用程序异步调用 ASMX 网络服务,请查看 this article从第 2 页开始。它展示了如何使用较新的 Event-Based Async Pattern 从 .NET cilent 应用程序异步调用 Web 服务.请注意,旧的 .NET 1.0 方法描述了 here ,它依赖于代理上的 BeginXXX/EndXXX 方法,不再推荐使用,因为 Visual Studio 的代理生成器不会创建这些方法。最好使用上面链接的基于事件的模式。
这是上面文章的摘录/改编,因此您可以了解所涉及的代码:
void KickOffAsyncWebServiceCall(object sender, EventArgs e)
{
HelloService service = new HelloService();
//Hookup async event handler
service.HelloWorldCompleted += new
HelloWorldCompletedEventHandler(this.HelloWorldCompleted);
service.HelloWorldAsync();
}
void HelloWorldCompleted(object sender,
HelloWorldCompletedEventArgs args)
{
//Display the return value
Console.WriteLine (args.Result);
}
延长服务器和客户端超时时间
为了防止超时,http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-web-services/5202/Web-Method-TimeOut对如何调整客户端和服务器超时有一个很好的总结。如果您拥有服务器端方法或仅拥有客户端调用,您没有在问题中指定,因此下面的摘录涵盖了这两种情况:
there has two setttings that will affect the webservice call timeout behavior:
** The ASP.NET webservice's server-side httpruntime timeout setting, this is configured through the following element:
httpRuntime Element (ASP.NET Settings Schema)
http://msdn2.microsoft.com/en-us/library/e1f13641.aspx<configuration> <system.web>
<httpRuntime .............
executionTimeout="45"
.............../> </system.web> </configuration>Also, make sure that you've set the <compilation debug="false" /> so as to make the timeout work correctly.
** If you're using the wsdl.exe or VS IDE "add webreference" generated proxy to call webservice methods, there is also a timeout setting on the client proxy class(derived from SoapHttpClientProtocol class). This is the "Timeout" property derived from "WebClientProtocol" class:
WebClientProtocol.Timeout Property http://msdn2.microsoft.com/en-us/library/system.web.services.protocols.webclientprotocol.timeout.aspx
Therefore, you can consider adjusting these two values according to your application's scenario. Here is a former thread also mentioned this:
请注意,我强烈建议您将超时设置得足够长以涵盖您最长的操作(加上足够的缓冲区以确保在事情变慢时安全),但我不建议完全关闭超时。允许无限超时通常是不好的编程习惯,因为错误的客户端或服务器可以永久禁用另一个。相反,只需将超时设置得非常长 --- 并确保记录客户端或服务器超时的实例,这样您就可以在问题发生时检测和诊断问题!
最后,回应上面的评论:对于新代码,最好使用 WCF。但如果您无法使用 ASMX 网络服务,上述解决方案应该有效。
关于c# - 没有超时的异步 web 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2454377/
wait() 和 wait(timeout) 之间有什么区别。无论如何 wait() 需要等待通知调用,但为什么我们有 wait(timeout)? 那么 sleep(timeout) 和 wait(
如何向以下脚本添加超时?我希望它将文本显示为“超时”。 var bustcachevar = 1 //bust potential caching of external pages after in
我正在使用 Firebase once() 方法来检索 React Native 移动应用中的值。问题是,如果手机离线,once() 永远不会返回。文档说 ref.off() 方法应该取消回调,但这似
我在一个表中有一个大型数据集(超过 200 万行,每行超过 100 列),存储在 cassandra 中,几个月前(也许是 2 个月?)我能够执行一个简单的命令来跟踪该表中的记录数量: SELECT
我使用 jquery 开发移动应用程序,下面是我的代码,当我向包含的页面添加 5 或 6 行时,一切正常。但如果我添加多行显示错误消息:Javascript 执行超时。 function succes
我正在使用一个 javascript 确认,它将在 15 分钟后重复调用。如果用户未选择确认框中的任何选项我会在等待 1 分钟后重定向他。如何实现这一目标?我的代码是这样的 var timeo
每次我在沙箱环境中运行这段代码时,我都会超时并最终崩溃。我已经通过多个 IDE 运行它,但仍然找不到任何语法错误。如果有人看到了我没有看到的东西,我将非常感谢您的意见。 //assign variab
更新联系人后我会显示一条消息,1500 毫秒后我会转到另一个页面。我是这样做的: onSubmit() { if (this.form.valid) {
从昨天开始,我拼命尝试使用最新版本的 PHPMailer 运行一个非常简单的电子邮件脚本。 最荒谬的是,同一个脚本在两台服务器上不起作用,但在另一台服务器上却起作用。 这是我的尝试(来自 PHPMai
我已阅读以下 2 篇文章并尝试实现相同的文章。 我的代码是这样的,超时发生在这里 HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
我正在尝试连接到 wsdl 服务, 但收到此错误: wsdl 错误:获取 http://api.didww.com/api/?wsdl - HTTP 错误: header 的套接字读取超时 本地没有问
我在使用 Ansible 的 CentOs7 实例上从 Artifactory 下载 jar 文件时遇到问题。这是我第一次在 Linux 实例上这样做。 我在每个 Windows 实例上都使用了 wi
在过去的两天里,我一直在寻找原因,我在互联网上和堆栈上尝试了很多解决方案。 我有一个带有 ubuntu 16.04 和 apache2 的专用 VM -> 服务器版本:Apache/2.4.18 (U
我正处于构建 PHP 应用程序的早期阶段,其中一部分涉及使用 file_get_contents()从远程服务器获取大文件并将它们传输给用户。例如,要获取的目标文件是 200 mB。 如果下载到服务器
我正在尝试连接到本地网络内的路由器。到目前为止,我已经使用了 TcpClient。 检查我的代码: public static void RouterConnect() {
我正在尝试构建一段代码来搜索使用 Mechanize 和 Ruby 超时的页面。我的测试台包括一个专门写入超时的页面,以及 3 个正常运行的页面。这是代码: urls = ['http://examp
我是 python 的新手,也是语义网查询领域的新手。我正在使用 SPARQLWrapper 库查询 dbpedia,我搜索了库文档但未能找到从 sparqlWrapper 触发到 dbpedia 的
我正在从 GenServer 中的句柄信息功能调用 elixir genserver 以添加电话号码获取表单客户端。但是一旦调用了handle_call,所有者进程就会崩溃[超时]。请帮忙。 全局创建
假设我的 WCF 服务中有以下执行链: ServiceMethod 调用并等待 Method1,然后调用并等待 Method2,后者调用并等待 Method3。最后 ServiceMethod 在返回
目前我正在开发一个从远程服务器发送和接收文件的应用程序。为了进行网络操作,我正在使用 QNetworkAccessManager。 要上传文件,我使用 QNetworkAccessManager::p
我是一名优秀的程序员,十分优秀!