【问题标题】:TimeoutException when WCF Host and Client are in the same processWCF 主机和客户端在同一进程中时的 TimeoutException
【发布时间】:2011-01-02 19:29:39
【问题描述】:

我遇到了一个非常奇怪的问题。我正在构建一个高度分布式的应用程序,其中每个应用程序实例都可以是 WCF 服务的主机和/或客户端(非常类似于 p2p)。一切正常,只要客户端和目标主机(我的意思是应用程序,而不是主机,因为目前一切都在一台计算机上运行(所以没有防火墙问题等))不一样。 如果它们相同,则应用程序会挂起正好 1 分钟,然后引发 TimeoutException。 WCF-Logging 没有产生任何有用的东西。 这是一个演示问题的小应用程序:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var binding = new NetTcpBinding();
        var baseAddress = new Uri(@"net.tcp://localhost:4000/Test");

        ServiceHost host = new ServiceHost(typeof(TestService), baseAddress);
        host.AddServiceEndpoint(typeof(ITestService), binding, baseAddress);

        var debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        if (debug == null)
            host.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
        else
            debug.IncludeExceptionDetailInFaults = true;

        host.Open();

        var clientBinding = new NetTcpBinding();
        var testProxy = new TestProxy(clientBinding, new EndpointAddress(baseAddress));
        testProxy.Test();
    }
}

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    void Test();
}

public class TestService : ITestService
{
    public void Test()
    {
        MessageBox.Show("foo");
    }
}

public class TestProxy : ClientBase<ITestService>, ITestService
{
    public TestProxy(NetTcpBinding binding, EndpointAddress remoteAddress) :
        base(binding, remoteAddress) { }

    public void Test()
    {
        Channel.Test();
    }
}

我做错了什么?

问候, 法老2k

【问题讨论】:

  • 你有任何配置设置吗?

标签: wcf nettcpbinding timeoutexception


【解决方案1】:

您将所有内容放在同一个线程中。你不能在同一个线程上有一个客户端和一个服务器,至少在这种代码中是这样。

如果您改为这样做,例如:

    ThreadPool.QueueUserWorkItem(state =>
    {
        var clientBinding = new NetTcpBinding();
        var testProxy = new TestProxy(clientBinding, new EndpointAddress(baseAddress));
        testProxy.Test();
    });

你的代码应该会更好。

PS:即使在同一台机器上,您也可能遇到防火墙问题 - 嗯,这是一个功能,而不是问题 :-)。

【讨论】:

  • 哇,谢谢,这解决了问题。从来没有想过这是一个线程问题^^我会投票给你的答案,但我还没有 15 分 xD
猜你喜欢
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多