0

有人可以告诉我如何在不发生死锁的情况下从回调中调用 WCF 服务的属性吗?

我尝试将 [CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)] 添加到实现回调的类中,但没有成功。

该服务具有以下属性:

 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]

public class SAPUploadService :    ISAPUploadService
{

谢谢MM

这是调用回调方法的代码

        foreach (var otherConnection in _users.Keys)
        {

            SAPUploadInstruction ins = AddMessageToInstruction(message);
            ins.UserName = user.UserName;
            Task.Factory.StartNew(() =>
            {
                otherConnection.ReceiveInstruction(ins);
            });

这是 ReceiveInstruction 的回调实现

public void ReceiveInstruction(SAPUploadInstruction instruction)
        {
            // fire this objects call back....
            if (OnReceiveInstruction != null) OnReceiveInstruction(this, instruction);
        }

在上面,事件 OnReceiveInstruction 附加到 UI。处理如下:

 public void ReceiveInstruction(object sender, SAPUploadInstruction instruction)
        {
             DispatchIfNecessary(() => {
                 ProcessInstruction(instruction);
                        });
        }

上述方法 - ProcessInstruction - 根据服务属性/功能设置各种控件。正是这一点陷入僵局,即 Label1.Content = myService.SomeProperty。

顺便说一句, DispatchIfNecessary 实现为:

public void DispatchIfNecessary(Action action)
        {
            if (!Dispatcher.CheckAccess())
                Dispatcher.Invoke(action);
            else
                action.Invoke();
        }
4

1 回答 1

0

DispatchIfNecessary使用异步版本Invoke,因此您的回调不会等待完成 UI 更改,这是无法完成的,因为 UI 线程正在等待回调处理结束(因此我们有死锁):

Dispatcher.BeginInvoke(action);
于 2013-11-12T09:03:06.720 回答