1

私は本当に奇妙な問題に遭遇しました。私は、各アプリ インスタンスが 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();
    }
}

私は何を間違っていますか?

よろしく、Pharao2k

4

1 に答える 1

5

すべてを同じスレッドに入れます。少なくともこの種のコードでは、クライアントとサーバーを同じスレッドに置くことはできません。

代わりにこれを行う場合、たとえば:

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

あなたのコードはもっとうまくいくはずです。

PS:同じマシンでも、ファイアウォールの問題が発生する可能性があります。これは機能であり、問​​題ではありません:-)。

于 2011-01-02T19:51:24.153 に答える