对 Windows 服务中托管的 WCF 服务的异步调用
我在Windows服务中托管了WCF服务。 我有一个控制台应用程序,我为其添加了 WCF 服务引用并为其生成了客户端。
我可以对服务进行同步调用,但异步调用似乎不起作用。 如果我附加服务器进程,它根本不会影响服务。
client= new ServiceClient();
client.DoSomething();//Works fine
client.DoSomethingAsync()//Doesnot work
这是一个已知问题吗?
I have hosted a WCF service in windows service. I have console app for which I added a WCF service reference and generated Client for it.
I can make Sync call to the service,but Async call doesn't seem to work. If i attach server process it doesn't hit the service at all.
client= new ServiceClient();
client.DoSomething();//Works fine
client.DoSomethingAsync()//Doesnot work
Is this a known problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有一个调用,
那么您是否指定了回调完成事件的处理程序?
发生的情况是异步调用关闭,但它如何向您发送回任何结果? 您需要为此提供一个处理程序方法 - 将其连接到 DoSomethingCompleted 事件处理程序! 在该方法中,您将获得异步调用的结果,并且可以使用它们执行您需要执行的任何操作。
马克
If you have a call to
then did you specify a handler for the callback completed event??
What happens is that the async call goes off, but how would it send you back any results?? You'll need to provide a handler method for that - hook it up to the
DoSomethingCompleted
event handler! In that method, you'll get the results of the async call and you can do with them whatever you need to do.Marc
从发布的代码来看,我假设您尚未设置处理程序来处理异步方法的响应。 您需要类似于此 msdn 帖子底部的示例,您使用 AddHanlder 来处理响应。
在进行异步调用之前,如下所示:
使用处理结果的方法:
From the code posted, i'm assuming you have not set up handlers to deal with the response from the async method. You'll need something like the example at the bottom of this msdn post where you use AddHanlder to handle the response.
Something like the below before you make the async call:
With a method to deal with the outome:
当您生成客户端时,您是否勾选了指定“生成异步操作”的框?
When you generated the client, did you tick the box to specify "Generate asynchronous operations"?
asynccall 可能会在后台工作线程中启动。 因此,可能发生的情况是,您的异步线程正在消失,因为前台线程已完成其处理。
除非您在进行此调用后有一些逻辑来等待响应,或者在主线程上继续执行其他一些工作,否则后台线程可能没有时间在应用程序退出之前创建。
通过在异步调用后添加 Thread.Sleep 可以轻松测试这一点。
发生这种情况的症状是您的服务意外启动/停止并且 Windows 抛出错误。
The asynccall will probably be started in a background workerthread. So what could be happening is that your async thread is dieing out because the foreground thread has finished it's processing.
Unless you have some logic after you make this call to wait for the reponse, or continue with some other work on your main thread, the background thread may not have the time to get created before the application exits.
This can be easily tested by adding Thread.Sleep after the async call.
A symptom of this happening is that your service starts / stops unexpectedly and windows throws an error.