static void Main(string[] args) { Console.Write("Your Service Namespace: "); string serviceNamespace = Console.ReadLine(); Console.Write("Your Issuer Name: "); string issuerName = Console.ReadLine(); Console.Write("Your Issuer Secret: "); string issuerSecret = Console.ReadLine(); TransportClientEndpointBehavior relayCredentials = new TransportClientEndpointBehavior(); relayCredentials.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret); Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "HelloService"); ChannelFactory <IHelloChannel> channelFactory = new ChannelFactory <IHelloChannel>("RelayEndpoint", new EndpointAddress(serviceUri)); channelFactory.Endpoint.Behaviors.Add(relayCredentials); IHelloChannel channel = channelFactory.CreateChannel(); channel.Open(); IHybridConnectionStatus hybridConnectionStatus = channel.GetProperty <IHybridConnectionStatus>(); hybridConnectionStatus.ConnectionStateChanged += (o, e) => { Console.WriteLine("Upgraded!"); }; Console.WriteLine("Press any key to exit"); DateTime lastTime = DateTime.Now; int count = 0; while (!Console.KeyAvailable) { channel.Hello("Hello"); count++; if (DateTime.Now - lastTime > TimeSpan.FromMilliseconds(250)) { lastTime = DateTime.Now; Console.WriteLine(" Sent {0} messages...", count); count = 0; } } channel.Close(); channelFactory.Close(); }
void EnsureConnection() { lock (connectLock) { if (this.dataChannel == null || this.dataChannel.State != CommunicationState.Opened) { this.multiplexedOutputStream = new ThrottledQueueBufferedStream(5); QueueBufferedStream multiplexedInputStream = new QueueBufferedStream(); this.dataChannelFactory = CreateDataChannelFactory(multiplexedInputStream); this.dataChannelFactory.Open(); this.dataChannel = dataChannelFactory.CreateChannel(new EndpointAddress("sb:"), endpointVia); try { this.dataChannel.Open(); this.dataChannel.Closed += DataChannelClosed; this.dataChannel.Faulted += DataChannelClosed; IHybridConnectionStatus status = dataChannel.GetProperty <IHybridConnectionStatus>(); if (status != null) { status.ConnectionStateChanged += (o, e) => { Trace.TraceInformation("Data channel upgraded to direct connection."); }; } this.dataChannel.Connect("tcp:" + toPort.ToString()); this.inputPump = new MultiplexConnectionInputPump(multiplexedInputStream.Read, CorrelateConnection, null); this.inputPump.Run(false); this.outputPump = new StreamBufferWritePump(multiplexedOutputStream, WriteToDataChannel); this.dataChannel.Extensions.Add(new DataExchangeChannelFaultHelper(outputPump)); this.outputPump.BeginRunPump(MultiplexPumpCompleted, null); return; } catch (AuthorizationFailedException af) { Trace.TraceError("Authorization failed: {0}", af.Message); if (dataChannel != null) { dataChannel.Abort(); dataChannel = null; } throw; } catch (Exception ex) { this.dataChannelFactory.Abort(); this.dataChannelFactory = null; Trace.TraceError("Unable to establish data channel: {0}", ex.Message); if (dataChannel != null) { dataChannel.Abort(); dataChannel = null; } throw; } } } }