2

我已经尝试了 msdn 示例代码 ( http://code.msdn.microsoft.com/windowsazure/Relayed-Messaging-Bindings-ca039161 ) 并且在我自己的测试应用程序中遇到了同样的问题。我有一个在中继模式下运行良好的示例,但是一旦我切换到混合模式,客户端就无法连接到服务器并出现超时异常。我已经增加了超时并打开了跟踪,但所有的跟踪显示都是一堆警告,例如

Failed to open Microsoft.ServiceBus.Channels.ClientFramingDuplexSessionChannel
Failed to open Microsoft.ServiceBus.SocketConnectionChannelFactory`1+DuplexSessionChannel[System.ServiceModel.Channels.IDuplexSessionChannel]

我尝试在 Tcp、Http 和 AutoDetect 之间设置系统连接模式环境设置,但没有成功。

服务器代码:

            ServiceHost sh = new ServiceHost(typeof(Service1));
        var uri = ServiceBusEnvironment.CreateServiceUri("sb", "<serverNamespace>", "testing");
        var binding = new NetTcpRelayBinding();
        binding.ConnectionMode = TcpRelayConnectionMode.Hybrid;
        binding.Security.Mode = EndToEndSecurityMode.None;
        binding.OpenTimeout = new TimeSpan(0,0,10,0);
        binding.ReceiveTimeout = new TimeSpan(0, 0, 10, 0);
        binding.SendTimeout = new TimeSpan(0, 0, 10, 0);

        sh.AddServiceEndpoint(
           typeof(IService1), new NetTcpRelayBinding(), uri
           )
            .Behaviors.Add(new TransportClientEndpointBehavior
            {
                TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("<keyName>", "<sharedaccesskey>")
            });

        sh.Open();

        Console.WriteLine("Press ENTER to close");
        Console.ReadLine();

        sh.Close();

客户端代码:

            var binding = new NetTcpRelayBinding {ConnectionMode = TcpRelayConnectionMode.Hybrid};
        binding.Security.Mode = EndToEndSecurityMode.None;
        binding.OpenTimeout = new TimeSpan(0, 0, 10, 0);
        binding.ReceiveTimeout = new TimeSpan(0, 0, 10, 0);
        binding.SendTimeout = new TimeSpan(0, 0, 10, 0);
        binding.CloseTimeout = new TimeSpan(0, 0, 10, 0);

        var cf = new ChannelFactory<IService1>(
            binding,
            new EndpointAddress(
                ServiceBusEnvironment.CreateServiceUri("sb", "<serviceNamespace>", "testing")
                ));

        cf.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior
        {
           TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("<keyName>", "<sharedAccessKey>")
        });

        var ch = cf.CreateChannel();

        ((ICommunicationObject)ch).Open();
        Console.WriteLine(ch.GetData(5));
        Console.ReadLine();

谢谢阅读,

罗宾

4

1 回答 1

0

您需要在 ChannelFactory 创建的客户端上设置 OperationTimeout:

    var ch = cf.CreateChannel();

    // Add this Line
    ch.OperationTimeout = new TimeSpan(0, 0, 10, 0);

    ((ICommunicationObject)ch).Open();
    Console.WriteLine(ch.GetData(5));
    Console.ReadLine();
于 2017-03-02T11:21:24.023 回答