从服务调用方法时 WCF 客户端卡住

标签 wcf wcf-client wcfserviceclient

我有一个奇怪的问题,当我的客户端从我的 WCF 服务调用方法时,它会挂起。现在真正奇怪的是,当客户端是控制台应用程序时,这不会发生。当客户端是 WinForm 或 WPF 应用程序时确实会发生这种情况。

我创建了一个客户端库,WCF 客户端可以使用它来连接到服务,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;  //needed for WCF communication

namespace DCC_Client
{
    public class DCCClient
    {
        private DuplexChannelFactory<ServiceReference1.IDCCService> dualFactory;

        public ServiceReference1.IDCCService Proxy;

        public DCCClient()
        {
            //Setup the duplex channel to the service...
            NetNamedPipeBinding binding = new NetNamedPipeBinding();
            dualFactory = new DuplexChannelFactory<ServiceReference1.IDCCService>(new Callbacks(), binding, new EndpointAddress("net.pipe://localhost/DCCService"));
        }

        public void Open()
        {
            Proxy = dualFactory.CreateChannel();
        }

        public void Close()
        {
            dualFactory.Close();
        }
    }

    public class Callbacks : ServiceReference1.IDCCServiceCallback
    {
        void ServiceReference1.IDCCServiceCallback.OnCallback(string id, string message, Guid key)
        {
            Console.WriteLine(string.Format("{0}: {1}", id, message));
        }
    }
}

这是的代码工作 WCF 控制台客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DCC_Client;

namespace Client_Console_Test
{
    class Program
    {
        private static DCCClient DCCClient;

        static void Main(string[] args)
        {
            try
            {
                DCCClient = new DCCClient();

                DCCClient.Open();

                DCCClient.Proxy.DCCInitialize(); //returns fine from here

                Console.ReadLine();
                DCCClient.Proxy.DCCUninitialize();

                DCCClient.Close();
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}

这是 的 WPF 客户端代码卡住 (见评论)
using System; //etc

using DCC_Client;  //Used for connection to DCC Service

namespace Client_WPF_Test
{
    public partial class Main : Window
    {
        private static DCCClient DCCClient;

        public Main()
        {
            InitializeComponent();

            DCCClient = new DCCClient();

            DCCClient.Open();
        }

        private void Connect_btn_event() {

            try
            {
                DCCClient.Proxy.DCCInitialize(); //**never returns from this**
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

我踏入密码DCCClient.Proxy.DCCInitialize();并且服务成功地执行了命令,但是,由于某种原因,客户端卡在这里并且不会继续执行。客户端也不异常(exception),堆栈跟踪显示[外部代码]。

话虽如此,控制台客户端运行完美。我想我在这里遗漏了一些简单的东西。感谢您提供的任何帮助。

最佳答案

如果您的服务回调直接来自 DCCInitialize 的客户端并且操作和回调操作都没有标记为您的应用程序将死锁的一种方式。尝试使用此属性标记您的回调实现:

[CallbackBehavior(ConcurrencyMode=ConcurrencyModel.Reentrant)]

除了这个,您还可以尝试在两个契约(Contract)中标记操作
[OperationContract(IsOneWay=true)]

但是两个操作都必须返回 void
最后,如果这些都没有帮助尝试标记您的回调实现:
[CallbackBehavior(UseSynchronizationContext=false)]

但在这种情况下,您的回调操作将在另一个线程中运行,并且无法直接使用 UI 控件进行操作。

编辑:

WCF 在 UI 线程中托管时的行为有所不同。在这种情况下,所有请求都在标准 Windows 消息循环中按顺序处理,因此如果您调用该服务,您阻止了当前线程,但该服务回调您的客户端并等待处理消息,但它不能因为线程被阻止初始调用 = 死锁,直到初始请求超时。通过使用最后提到的行为,您会说 WCF 不加入 Windows 消息循环,而是像往常一样在单独的线程中处理消息。除了您无法从其他线程中运行的方法访问 UI 控件这一事实之外,这没有任何安全问题——WinForms 和 WPF 都有从其他线程传递命令的方法。

关于从服务调用方法时 WCF 客户端卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6206160/

相关文章:

c# - 在 WCF 中设置最大下载速度

.net - 从 WSDL 生成服务契约

wcf - 如何从 Web 服务调用获取 WCF 客户端中的原始 XML

wcf - 使用 WCF 从类库中公开对象

wcf - 如何确保不会出现 WCF 故障状态异常?

javascript - 网络应用程序设计思想

c# - 无法隐式转换类型 'System.Threading.Tasks.Task<String> to ' 字符串'

wcf - 初学 Windows Service/WCF 和前端 GUI 实现问题

c# - 可以接受内容类型 : text/plain? 的 WCF WebInvoke