我有这个奇怪的问题,当我的客户端将从我的WCF服务调用方法时挂起。现在真正奇怪的是,当客户端是控制台应用程序时,不会发生这种情况。它发生在客户端是WinForm或WPF应用程序时。从服务调用方法WCF客户端冻结
我创建了一个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();
,服务成功执行了这些命令,但是由于某些原因,客户端卡在这里并且不会继续执行。客户端不会例外,并且堆栈跟踪显示[外部代码]。
这就是说,控制台客户端运行完美。我想我在这里错过了一些简单的东西。我感谢您提供的任何帮助。
感谢拉迪斯拉夫您的快速回复。我的回拨在我的合同中已经有'[OperationContract(IsOneWay = true)]'。我尝试在我的Callback实现中添加'[CallbackBehavior(ConcurrencyMode = ConcurrencyModel.Reentrant)]'并且客户端仍然会冻结。然后我使用了'[CallbackBehavior(UseSynchronizationContext = false)]',它工作!是否有任何安全问题,我应该知道,因为它是在一个不同的线程? – 2011-06-01 19:31:41
我在描述中添加了我的答案。 – 2011-06-01 19:38:44
+1。在尝试了其他一些事情之后,'[CallbackBehavior(UseSynchronizationContext = false)]'也是我的问题的解决方案。 – 2015-05-21 09:41:33