Esempio n. 1
0
        static void Main(string[] args)
        {

            var amqpUrl = "amqp:localhost:5672"; //AMQP URL for RabbitMQ installation
            var serviceName = "madagascar"; //The unique identifier for the target service

            var msgMapper = new BasicMessageMapper(amqpUrl, serviceName);

            RestBusClient client = new RestBusClient(msgMapper);

            RequestOptions requestOptions = null;
            /* 
             * //Uncomment this section to get a response in JSON format
             * 
            requestOptions = new RequestOptions();
            requestOptions.Headers.Add("Accept", "application/json");
             */

            var response = SendMessage(client, requestOptions).Result;

            //Display response
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);

            client.Dispose();
            Console.ReadKey();

        }
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //Init RestBus client
            var amqpUrl = "amqp://localhost:5672"; //AMQP URL for RabbitMQ installation
            var serviceName = "samba"; //The unique identifier for the target service

            var msgMapper = new BasicMessageMapper(amqpUrl, serviceName);
            HelloServiceClient = new RestBusClient(msgMapper);
        }
Esempio n. 3
0
        public async static void Run(int iterations)
        {
            /*
             * An example that performs a speed test via the RestBus RabbitMQ client.
             * 
             * For more examples, see the https://github.com/tenor/RestBus.Examples repo
             * For more elaborate speed tests see the https://github.com/tenor/RestBus.Benchmarks repo
             * 
             */

            //Start Web API 2 host to receive messages.
            WebAPISelfHost host = new WebAPISelfHost();
            var servers = host.Start();

            //Create client
            BasicMessageMapper msgMapper = new BasicMessageMapper(ConfigurationManager.AppSettings["rabbitmqserver"], "test");
            RestBusClient client = new RestBusClient(msgMapper);

            //Compose message
            var msg = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, "api/test/random")
            {
                Content = new System.Net.Http.StringContent("{\"Val\":10}", new UTF8Encoding(), "application/json")
            };

            msg.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01, */*; q=0.01");

            Stopwatch watch = new Stopwatch();

            HttpResponseMessage res;
            watch.Start();
            for (int i = 0; i < iterations; i++)
            {
                //Send message
                res = await client.SendAsync(msg, System.Threading.CancellationToken.None);
            }

            watch.Stop();

            Console.WriteLine("Elapsed time: " + watch.Elapsed);
            Console.ReadKey();

            //Dispose client
            client.Dispose();
            //Dispose servers
            foreach (var server in servers) { server.Dispose(); }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            int iterationsPerTask = Int32.Parse(ConfigurationManager.AppSettings["MessagesPerThread"]);
            int taskCount = Int32.Parse(ConfigurationManager.AppSettings["NoOfThreads"]);
            bool expectReply = Boolean.Parse(ConfigurationManager.AppSettings["ExpectReply"]);

            var amqpUrl = ConfigurationManager.AppSettings["ServerUri"]; //AMQP URI for RabbitMQ server
            var serviceName = "speedtest"; //Uniquely identifies this service

            var msgMapper = expectReply ? new BasicMessageMapper(amqpUrl, serviceName) : new SendOnlyMessageMapper(amqpUrl, serviceName);
            var subscriber = new RestBusSubscriber(msgMapper);

            var client = new RestBusClient(msgMapper);

            Task[] tasks = new Task[taskCount];
            for(int t = 0; t < taskCount; t++)
            {
                tasks[t] = new Task(() =>
                {
                    Message msg;
                    for (int i = 0; i < iterationsPerTask; i++)
                    {
                        msg = new Message { Body = BodyGenerator.GetNext() };
                        var res = client.PostAsJsonAsync("api/test/", msg, null).Result;
                    }

                }, TaskCreationOptions.LongRunning);
            }

            Console.WriteLine("Sending " + iterationsPerTask * taskCount + " messages across " + taskCount + " threads");
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for(int t = 0; t < taskCount; t++)
            {
                tasks[t].Start();
            }

            Task.WaitAll(tasks);

            watch.Stop();
            Console.WriteLine("Total time: " + watch.Elapsed);
            Console.ReadKey();
            client.Dispose();
        }
        private static void AttachOptions(HttpRequestMessage request, RequestOptions options)
        {
            if (options != null && options.Headers != null)
            {
                //Copy headers over

                foreach (var header in options.Headers)
                {
                    if (request.Headers.Contains(header.Key))
                    {
                        request.Headers.Remove(header.Key);
                    }
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            //Attach options to request
            RestBusClient.SetRequestOptions(request, options);
        }
Esempio n. 6
0
        public async static void Run()
        {
            /*
             * An example that composes a HttpRequest Message and sends it via the RestBus RabbitMQ client.
             * 
             * For more examples, see the https://github.com/tenor/RestBus.Examples repo
             * 
             */

            //Start Web API 2 host to receive message.
            WebAPISelfHost host = new WebAPISelfHost();
            var servers = host.Start();

            //Create client
            BasicMessageMapper msgMapper = new BasicMessageMapper("amqp://localhost:5672", "test");
            //msgMapper = new QueueingMessageMapper("amqp://localhost:5672", "test"); //Uncomment this to only queue messages.

            RestBusClient client = new RestBusClient(msgMapper);

            //Compose message
            var msg = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, "/api/test")
            {
                Content = new System.Net.Http.StringContent("{\"Val\":10}", new UTF8Encoding(), "application/json")
            };

            msg.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01, */*; q=0.01");

            //Send message
            Console.WriteLine("Sending Message ...");

            var res = await client.SendAsync(msg, System.Threading.CancellationToken.None);

            Console.WriteLine("Response Received:\n{0}\nContent:\n{1}\n", res, Encoding.UTF8.GetString((res.Content as ByteArrayContent).ReadAsByteArrayAsync().Result));

            Console.WriteLine("Press any key to quit.");
            Console.ReadKey();

            //Dispose client
            client.Dispose();
            //Dispose servers
            foreach(var server in servers) { server.Dispose(); }
        }
Esempio n. 7
0
        private static void SetResponseResult(HttpRequestMessage request, bool timedOut, ExpectedResponse arrival, TaskCompletionSource <HttpResponseMessage> taskSource)
        {
            if (timedOut)
            {
                //NOTE: This really ought to return an "Operation Timed Out" WebException and not a Cancellation as noted in the following posts
                // http://social.msdn.microsoft.com/Forums/en-US/d8d87789-0ac9-4294-84a0-91c9fa27e353/bug-in-httpclientgetasync-should-throw-webexception-not-taskcanceledexception?forum=netfxnetcom&prof=required
                // http://stackoverflow.com/questions/10547895/how-can-i-tell-when-httpclient-has-timed-out
                // and http://stackoverflow.com/questions/12666922/distinguish-timeout-from-user-cancellation
                //
                // However, for compatibility with the HttpClient, it returns a cancellation
                //

                taskSource.SetCanceled();
            }
            else
            {
                if (arrival.DeserializationException == null)
                {
                    if (arrival.Response == null)
                    {
                        //TODO: Log this -- Critical issue (or just assert)
                    }
                }

                HttpResponseMessage msg;
                if (arrival.DeserializationException == null && TryGetHttpResponseMessage(arrival.Response, out msg))
                {
                    msg.RequestMessage = request;
                    taskSource.SetResult(msg);
                }
                else
                {
                    taskSource.SetException(RestBusClient.GetWrappedException("An error occurred while reading the response.", arrival.DeserializationException));
                }
            }
        }
Esempio n. 8
0
        public static void Run()
        {
            /*
             * An example that composes a HttpRequest Message and sends it via the RestBus RabbitMQ client.
             *
             * For more examples, see the https://github.com/tenor/RestBus.Examples repo
             *
             */

            BasicMessageMapper msgMapper = new BasicMessageMapper("amqp://localhost:5672", "test");
            RestBusClient client = new RestBusClient(msgMapper);

            var msg = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, "/test/my_api")
            {
                Content = new System.Net.Http.StringContent("{\"Val\":10}", new UTF8Encoding(), "application/json")
            };

            msg.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01, */*; q=0.01");

            var res = client.SendAsync(msg, System.Threading.CancellationToken.None).Result;

            Console.ReadKey();
            client.Dispose();
        }
Esempio n. 9
0
 private async static Task<System.Net.Http.HttpResponseMessage> SendMessage(RestBusClient client, RequestOptions requestOptions)
 {
     //Send Request
     var uri = "api/values"; //Substitute "hello/random" for the ServiceStack example
     return await client.GetAsync(uri, requestOptions);
 }