WCF“发射后不管”方法不允许主机执行按预期继续
我有一个 Windows 服务托管一个单例 WCF 服务,该服务缓存大量数据。在启动 Windows 服务时,我正在执行以下操作:
// start client service
wcfService= new ServiceHost(typeof(MyWcfService));
wcfService.Open();
using (HostedServiceReference.WcfServiceProxy wcfServiceProxy = new HostedServiceReference.WcfClientServiceProxy())
{
wcfServiceProxy.RefreshDisplayCacheFromSource();
// 1st echo to console
Console.WriteLine("Display Cache Refreshed"));
}
// 2nd echo to console
Console.WriteLine("Begin other processing"))
并且我在服务合同中配置了如下方法:
[OperationContract(IsOneWay=true)]
void RefreshDisplayCacheFromSource();
我原本希望立即看到控制台中显示的第一个和第二个回声,但我实际看到的只是第一个回声。直到我的“即发即忘”方法完成其长时间操作后,才会显示第二行。
谁能解释一下后台发生了什么?
到目前为止我的理论:
单例模式下 wcf 服务的操作是否会阻塞托管它的服务?
有什么办法吗?与 using 语句?
I have a windows service hosting a singleton WCF service which caches a large amount of data. On start up of the windows service I am doing the following:
// start client service
wcfService= new ServiceHost(typeof(MyWcfService));
wcfService.Open();
using (HostedServiceReference.WcfServiceProxy wcfServiceProxy = new HostedServiceReference.WcfClientServiceProxy())
{
wcfServiceProxy.RefreshDisplayCacheFromSource();
// 1st echo to console
Console.WriteLine("Display Cache Refreshed"));
}
// 2nd echo to console
Console.WriteLine("Begin other processing"))
and I have the method configured as follows in the service contract:
[OperationContract(IsOneWay=true)]
void RefreshDisplayCacheFromSource();
I had expected to immediately see the 1st and 2nd echos displayed in the console, but what I am actually seeing is just the 1st echo. The 2nd line is not displayed until my "fire and forget" method has completed it's long operation.
Can anyone explain what is going on in the background?
My theories so far:
Is the operation of the wcf service in singleton mode blocking the service hosting it?
Is it something to do with the using statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WCF 服务的代理对象将在 using 块结束时被释放,但这只有在客户端收到服务收到其消息的确认后才会发生。看来服务在执行完方法体中的所有代码(包括处理大量数据)之前不会发送确认,因此客户端上的执行会在 using 块的末尾等待,直到服务完成其操作。加工。
我可以使用两种方法来解决此问题:
将代理对象设置为客户端中的全局变量。
在服务器中分出一个线程来处理长时间的处理,允许服务器方法立即返回客户端正在等待的确认。
The proxy object for the WCF Service will be disposed of at the end of the using block, but this cannot happen until the client receives acknowledgement that the service received its message. It seems that the service does not send the acknowledgement until it had executed all the code in the method body (including processing the large amount of data), therefore execution on the client waits at the end of the using block until the service has finished its processing.
There are 2 ways I was able to use to get around this issue:
Make the proxy object a global variable in the client.
Spin off a thread in the server to handle the long processing, allowing the server method to return immediately with the acknowledgement the client is waiting for.
如果您的目标是在 WCF 调用执行其任务时能够继续进行其他工作,请右键单击您的服务引用并点击“更新服务引用”。选中生成异步方法的选项。
然后将其调用为 wcfServiceProxy.RefreshDisplayCacheFromSourceAsync();
如果您想在完成时收到通知,您也可以为其添加事件处理程序。
If your goal is to just be able to continue on with other work while the WCF call does its thing, right click on your service reference and hit "Update Service Reference." Check the option to generate Asynchronous Methods.
Then call it as wcfServiceProxy.RefreshDisplayCacheFromSourceAsync();
If you want to be notified when it finishes you can add an event handler for it too.