【问题标题】:Why do I get a WCF timeout even though my service call and callback are successful?为什么即使我的服务调用和回调成功,我也会收到 WCF 超时?
【发布时间】:2012-08-01 03:47:48
【问题描述】:

我正在尝试将游戏内控制台连接到 WCF 界面,以便外部应用程序可以发送控制台命令并接收控制台输出。为此,我创建了以下服务合同:

public interface IConsoleNetworkCallbacks
{
    [OperationContract(IsOneWay = true)]
    void NewOutput(IEnumerable<string> text, string category);
}

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IConsoleNetworkCallbacks))]
public interface IConsoleInterface
{
    [OperationContract]
    void ProcessInput(string input);

    [OperationContract]
    void ChangeCategory(string category);
}

在我实现它的服务器上:

public class ConsoleNetworkInterface : IConsoleInterface, IDisposable
{
    public ConsoleNetworkInterface()
    {
        ConsoleManager.Instance.RegisterOutputUpdateHandler(OutputHandler);
    }

    public void Dispose()
    {
        ConsoleManager.Instance.UnregisterOutputHandler(OutputHandler);
    }

    public void ProcessInput(string input)
    {
        ConsoleManager.Instance.ProcessInput(input);
    }

    public void ChangeCategory(string category)
    {
        ConsoleManager.Instance.UnregisterOutputHandler(OutputHandler);
        ConsoleManager.Instance.RegisterOutputUpdateHandler(OutputHandler, category);
    }

    protected void OutputHandler(IEnumerable<string> text, string category)
    {
        var callbacks = OperationContext.Current.GetCallbackChannel<IConsoleNetworkCallbacks>();
        callbacks.NewOutput(text, category);
    }
}

在客户端我实现了回调:

public class Callbacks : IConsoleNetworkCallbacks
{
    public void NewOutput(IEnumerable<string> text, string category)
    {
        MessageBox.Show(string.Format("{0} lines received for '{1}' category", text.Count(), category));
    }
}

最后,我用以下类建立服务主机:

public class ConsoleServiceHost : IDisposable
{
    protected ServiceHost _host;

    public ConsoleServiceHost()
    {
        _host = new ServiceHost(typeof(ConsoleNetworkInterface), new Uri[] { new Uri("net.pipe://localhost") });
        _host.AddServiceEndpoint(typeof(IConsoleInterface), new NetNamedPipeBinding(), "FrbConsolePipe");

        _host.Open();
    }

    public void Dispose()
    {
        _host.Close();
    }
}

并在我的客户端上使用以下代码建立连接:

    protected Callbacks _callbacks;
    protected IConsoleInterface _proxy;

    protected void ConnectToConsoleServer()
    {
        _callbacks = new Callbacks();
        var factory = new DuplexChannelFactory<IConsoleInterface>(_callbacks,
            new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/FrbConsolePipe"));
        _proxy = factory.CreateChannel();
        _proxy.ProcessInput("Connected");
    }

所以发生的情况是我的ConnectToConsoleServer() 被调用,然后它一直到_proxy.ProcessInput("Connected");。在我的游戏中(在服务器上),我立即看到由 ProcessInput 调用引起的输出,但客户端仍然在 _proxy.ProcessInput() 调用上停滞不前。

一分钟后,我的客户收到了 JIT TimeoutException然而,同时我的 MessageBox 消息出现了。

显然,不仅我的命令被立即发送,我的回调也被正确调用。那么为什么会出现超时异常呢?

注意:即使移除 MessageBox 调用,我仍然有这个问题,所以这不是 GUI 阻塞回调响应的问题。

【问题讨论】:

标签: c# wcf


【解决方案1】:

您需要为实现 Callback 接口的客户端类指定 CallbackBehavior [CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Multiple, UseSynchronizationContext=false)]

请参阅以下内容以获得更好的解释 http://www.switchonthecode.com/tutorials/wcf-callbacks-hanging-wpf-applications

【讨论】:

    【解决方案2】:

    有可能是您的_proxy.ProcessInput("Connected") 阻止了呼叫 - 这与您的超时经历一致,因为服务器立即发送响应,但客户端无法接收它,因为它卡在“ProcessInput”上。当您的调用最终超时时,阻塞调用终止,此时回调完成。

    要验证这一点,您可以尝试使用此(非阻塞)调用来调用吗?

    ((Action)(() => _proxy.ProcessInput("Connected"))).BeginInvoke(null, null);
    

    【讨论】:

    • 有意思,我回家试试这个
    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2021-04-16
    • 2015-05-13
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多