C# 需要示例触发并忘记对 WCF 服务的异步调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/774648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need sample fire and forget async call to WCF service
提问by apolfj
On a scheduled interval I need to call a WCF service call another WCF Service asyncronously. Scheduling a call to a WCF service I have worked out.
在预定的时间间隔内,我需要调用一个 WCF 服务异步调用另一个 WCF 服务。安排对我制定的 WCF 服务的调用。
What I think I need and I have read about here on stackoverflow that it is necessary to.., (in essence) prepare or change the code of your WCF services as to be able to handle an async call to them. If so what would a simple example of that look like?(Maybe a before and after example) Also is it still necessary in .Net 3.5?
我认为我需要什么,并且我已经在 stackoverflow 上读到过,有必要……(本质上)准备或更改 WCF 服务的代码,以便能够处理对它们的异步调用。如果是这样,一个简单的例子会是什么样的?(也许是一个之前和之后的例子)在.Net 3.5中它仍然是必要的吗?
Second I am using a proxy from the WCF Service doing the call to the next WCF Service and need a sample of an async call to a WCF service if it looks any different than what is typical with BeginEnvoke and EndEnvoke with typical async examples.
其次,我正在使用来自 WCF 服务的代理来调用下一个 WCF 服务,如果它看起来与典型的 BeginEnvoke 和 EndEnvoke 的典型异步示例有任何不同,则需要一个对 WCF 服务的异步调用示例。
I would believe it if I am completely off on my question and would appreciate any correction to establish a better question as well.
如果我完全离开我的问题,我会相信它,并且希望得到任何更正以建立一个更好的问题。
采纳答案by joshperry
Set the IsOneWayproperty of the OperationContract attribute to true on the WCF method that you are calling to. This tells WCF that the call only matters for one direction and the client won't hang around for the method to finish executing.
在您调用的 WCF 方法上,将 OperationContract 特性的IsOneWay属性设置为 true。这告诉 WCF 调用只对一个方向有影响,客户端不会等待方法完成执行。
Even when calling BeginInvoke your client code will still hang-out waiting for the server method to finish executing but it will do it on a threadpool thread.
即使在调用 BeginInvoke 时,您的客户端代码仍将挂起等待服务器方法完成执行,但它将在线程池线程上执行。
[ServiceContract]
interface IWCFContract
{
[OperationContract(IsOneWay = true)]
void CallMe()
}
The other way to do what you want is to have the WCF service spin its work off onto a background thread and return immediately.
另一种方法是让 WCF 服务将其工作转移到后台线程上并立即返回。
回答by urig
Be sure to carefully test the way a OneWay WCF call performs. I've seen it stall when you reach X number of simultaneous calls, as if WCF actually does wait for the call to end.
请务必仔细测试 OneWay WCF 调用的执行方式。我已经看到当您达到 X 个同时调用时它会停止,就好像 WCF 确实在等待调用结束一样。
A safer solution is to have the "target" code return control ASAP: Instead of letting it process the call fully, make it only put the data into a queue and return. Have another thread poll that queue and work on the data asynchronously.
一个更安全的解决方案是尽快让“目标”代码返回控制:与其让它完全处理调用,不如让它只将数据放入队列并返回。让另一个线程轮询该队列并异步处理数据。
And be sure to apply a thread safety mechanism to avoid clashes between the two threads working on that queue.
并确保应用线程安全机制以避免在该队列上工作的两个线程之间发生冲突。
回答by Stix
Don't use BeginInvoke or even a thread for your pattern. Make sure you decorate your classes with the AsyncPattern according to Microsoft website, otherwise your Async delegates and threads will be run in a synchronous mode. WCF forces this behavior. This info was posted by another op who found a solution to blocking callbacks question on stack .. sorry but I do not remember the link.
不要为您的模式使用 BeginInvoke 甚至线程。确保根据 Microsoft 网站使用 AsyncPattern 装饰您的类,否则您的 Async 委托和线程将以同步模式运行。WCF 强制执行此行为。此信息是由另一位操作员发布的,他在堆栈上找到了阻止回调问题的解决方案.. 抱歉,我不记得链接了。