Ejemplo n.º 1
0
        private static async Task RunAsync()
        {
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(
                KeyName, Key);
            var uri     = new Uri(string.Format("https://{0}/{1}", RelayNamespace, ConnectionName));
            var token   = (await tokenProvider.GetTokenAsync(uri.AbsoluteUri, TimeSpan.FromHours(1))).TokenString;
            var client  = new HttpClient();
            var request = new HttpRequestMessage()
            {
                RequestUri = uri,
                Method     = HttpMethod.Get,
            };

            request.Headers.Add("ServiceBusAuthorization", token);
            var response = await client.SendAsync(request);

            Console.WriteLine(await response.Content.ReadAsStringAsync()); Console.ReadLine();
        }
Ejemplo n.º 2
0
        static async Task RunAsync(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("dotnet client [ns] [hc] [keyname] [key]");
                return;
            }

            var ns      = args[0];
            var hc      = args[1];
            var keyname = args[2];
            var key     = args[3];

            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyname, key);
            var uri           = new Uri(string.Format("https://{0}/{1}", ns, hc));
            var token         = (await tokenProvider.GetTokenAsync(uri.AbsoluteUri, TimeSpan.FromHours(1))).TokenString;

            // TODO:
            // use this code (line 39) to hard code a specific proxy URL.
            //var webProxy = new WebProxy(new Uri("http://192.168.1.140:808"));

            // TODO:
            // use this code (line 43) to use the system settings for the user running the code. Lines 43-51 were added or modified from original sample.
            var webProxy = HttpWebRequest.DefaultWebProxy;
            //var proxysetting = Program.DefaultWebProxy;

            var proxyHttpClientHandler = new HttpClientHandler
            {
                Proxy    = webProxy,
                UseProxy = true,
            };
            var client  = new HttpClient(proxyHttpClientHandler);
            var request = new HttpRequestMessage()
            {
                RequestUri = uri,
                Method     = HttpMethod.Get,
            };

            request.Headers.Add("ServiceBusAuthorization", token);
            var response = await client.SendAsync(request);

            Console.WriteLine(await response.Content.ReadAsStringAsync());
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            TokenProvider    token    = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "H+rYJ3XugZAx4AwBWbgkgiqBzdxiFOY2ZR9FicPK840=");
            Uri              path     = ServiceBusEnvironment.CreateServiceUri("sb", "ajaybus", "");
            MessagingFactory factory  = MessagingFactory.Create(path, token);
            MessageReceiver  reciever = factory.CreateMessageReceiver("DetailmessageTopic/subscriptions/High");
            BrokeredMessage  msg;

            while ((msg = reciever.Receive()) != null)
            {
                Console.WriteLine("hello");
                Console.WriteLine("Message:" + msg.GetBody <String>() + "\t Priority:   " + msg.Properties["priority"]);
                msg.Complete();
            }
            Console.WriteLine("The High Priority Queue completed");
            reciever = factory.CreateMessageReceiver("DetailmessageTopic/subscriptions/Low");
            while ((msg = reciever.Receive()) != null)
            {
                Console.WriteLine("hello");
                Console.WriteLine("Message:" + msg.GetBody <String>() + "\t Priority: " + msg.Properties["priority"]);
                msg.Complete();
            }
            Console.WriteLine("The Low Priority Queue completed");
        }
Ejemplo n.º 4
0
        static async Task RunAsync(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("client [ns] [hc] [keyname] [key]");
                return;
            }

            Console.WriteLine("Enter lines of text to send to the server with ENTER");

            var ns      = args[0];
            var hc      = args[1];
            var keyname = args[2];
            var key     = args[3];

            // Create a new hybrid connection client
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyname, key);
            var client        = new HybridConnectionClient(new Uri(String.Format("sb://{0}/{1}", ns, hc)), tokenProvider);

            // Initiate the connection
            var relayConnection = await client.CreateConnectionAsync();

            // We run two conucrrent loops on the connection. One
            // reads input from the console and writes it to the connection
            // with a stream writer. The other reads lines of input from the
            // connection with a stream reader and writes them to the console.
            // Entering a blank line will shut down the write task after
            // sending it to the server. The server will then cleanly shut down
            // the connection will will terminate the read task.

            var reads = Task.Run(async() => {
                // initialize the stream reader over the connection
                var reader = new StreamReader(relayConnection);
                var writer = Console.Out;
                do
                {
                    // read a full line of UTF-8 text up to newline
                    string line = await reader.ReadLineAsync();
                    // if the string is empty or null, we are done.
                    if (String.IsNullOrEmpty(line))
                    {
                        break;
                    }
                    // write to the console
                    await writer.WriteLineAsync(line);
                }while (true);
            });

            // read from the console and write to the hybrid connection
            var writes = Task.Run(async() => {
                var reader = Console.In;
                var writer = new StreamWriter(relayConnection)
                {
                    AutoFlush = true
                };
                do
                {
                    // read a line form the console
                    string line = await reader.ReadLineAsync();
                    // write the line out, also when it's empty
                    await writer.WriteLineAsync(line);
                    // quit when the line was empty
                    if (String.IsNullOrEmpty(line))
                    {
                        break;
                    }
                }while (true);
            });

            // wait for both tasks to complete
            await Task.WhenAll(reads, writes);

            await relayConnection.CloseAsync(CancellationToken.None);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("server [ns] [hc] [keyname] [key]");
                return;
            }

            var ns      = args[0];
            var hc      = args[1];
            var keyname = args[2];
            var key     = args[3];

            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyname, key);
            var sendAddress   = new Uri($"sb://{ns}/{hc}");
            var relayClient   = new HybridConnectionClient(sendAddress, tokenProvider);

            try
            {
                var relayConnection = relayClient.CreateConnectionAsync().GetAwaiter().GetResult();

                TTransport        transport = new TStreamTransport(relayConnection, relayConnection);
                TProtocol         protocol  = new TBinaryProtocol(transport);
                Calculator.Client client    = new Calculator.Client(protocol);

                transport.Open();
                try
                {
                    client.ping();
                    Console.WriteLine("ping()");

                    int sum = client.add(1, 1);
                    Console.WriteLine("1+1={0}", sum);

                    Work work = new Work();

                    work.Op   = Operation.DIVIDE;
                    work.Num1 = 1;
                    work.Num2 = 0;
                    try
                    {
                        int quotient = client.calculate(1, work);
                        Console.WriteLine("Whoa we can divide by 0");
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    work.Op   = Operation.SUBTRACT;
                    work.Num1 = 15;
                    work.Num2 = 10;
                    try
                    {
                        int diff = client.calculate(1, work);
                        Console.WriteLine("15-10={0}", diff);
                    }
                    catch (InvalidOperation io)
                    {
                        Console.WriteLine("Invalid operation: " + io.Why);
                    }

                    SharedStruct log = client.getStruct(1);
                    Console.WriteLine("Check log: {0}", log.Value);
                }
                finally
                {
                    transport.Close();
                }
            }
            catch (TApplicationException x)
            {
                Console.WriteLine(x.StackTrace);
            }
        }
        private static async Task RunAsync()
        {
            Console.WriteLine("Enter lines of text to send to the server with ENTER");

            // Create a new hybrid connection client.
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key);
            var client        = new HybridConnectionClient(new Uri(String.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider);

            // Initiate the connection.
            var relayConnection = await client.CreateConnectionAsync();

            // Run two concurrent loops on the connection. One
            // reads input from the console and then writes it to the connection
            // with a stream writer. The other reads lines of input from the
            // connection with a stream reader and then writes them to the console.
            // Entering a blank line shuts down the write task after
            // sending it to the server. The server then cleanly shuts down
            // the connection, which terminates the read task.

            var reads = Task.Run(async() => {
                // Initialize the stream reader over the connection.
                var reader = new StreamReader(relayConnection);
                var writer = Console.Out;
                do
                {
                    // Read a full line of UTF-8 text up to newline.
                    string line = await reader.ReadLineAsync();
                    // If the string is empty or null, you are done.
                    if (String.IsNullOrEmpty(line))
                    {
                        break;
                    }
                    // Write to the console.
                    await writer.WriteLineAsync(line);
                }while (true);
            });

            // Read from the console and write to the hybrid connection.
            var writes = Task.Run(async() => {
                var reader = Console.In;
                var writer = new StreamWriter(relayConnection)
                {
                    AutoFlush = true
                };
                do
                {
                    // Read a line from the console.
                    string line = await reader.ReadLineAsync();
                    // Write the line out, also when it's empty.
                    await writer.WriteLineAsync(line);
                    // Quit when the line is empty.
                    if (String.IsNullOrEmpty(line))
                    {
                        break;
                    }
                }while (true);
            });

            // Wait for both tasks to finish.
            await Task.WhenAll(reads, writes);

            await relayConnection.CloseAsync(CancellationToken.None);
        }