-
Notifications
You must be signed in to change notification settings - Fork 56
Calling a Service Endpoint (via RabbitMQ)
Sunny Ahuwanya edited this page Jan 27, 2016
·
2 revisions
To call service endpoints, use the RestBusClient
class which is used similarly to the HttpClient class.
Compose a HttpRequestMessage and use the RestBusClient.SendAsync
method to send the message.
Alternatively, use one of the convenience extension methods defined on the RestBusClient
class to send your message, as illustrated in the following walkthrough:
- Create a new Console Application and install the RestBus.RabbitMQ NuGet package.
PM> Install-Package RestBus.RabbitMQ
- Call the target service with the following code example.
using RestBus.Client;
using RestBus.RabbitMQ;
using RestBus.RabbitMQ.Client;
using System;
class Program
{
static void Main(string[] args)
{
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);
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");
*/
//Send Request
var uri = "api/values"; //Substitute "hello/random" for the ServiceStack example
var response = client.GetAsync(uri, requestOptions).Result;
//NOTE: You can use 'var response = await client.GetAsync(uri, requestOptions);' in your code
//Display response
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
//Dispose client
client.Dispose();
}
}
Complete examples of RabbitMQ RestBus clients in console and web applications are available at the RestBus.Examples repo.