//Shared DTOs
public class Hello {
public string Name { get; set; }
}
public class HelloResponse {
public string Result { get; set; }
}
//Server
public class HelloService : IService<Hello> {
public void Execute (Hello req) {
return new HelloResponse { Result = "Hello, " + req.Name }
}
}
public class ServerAppHost : AppHostHttpListenerBase {
public void ServerAppHost()
: base ("Hello Server Services", typeof(HelloService).Assembly) {}
public void Configure(Container container) {
base.Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}");
var redisFactory = new PooledRedisClientManager("localhost:6379");
var mqHost = new RedisMqHost(redisFactory, noOfRetries:2, null);
//Server - MQ Service Impl:
//Listens for 'Hello' messages sent with 'mqClient.Publish(new Hello { Name = "Client" });'
mqHost.RegisterHandler<Hello>(m =>
new HelloResponse { Result = "Hello, " + m.GetBody().Name });
mqHost.Start(); //Starts listening for messages
}
}
var serverAppHost = new ServerAppHost();
serverAppHost.Init();
Console.ReadLine(); //Block the server from exiting (i.e. if running inside Console App)
//Client
public class ClientAppHost : AppHostHttpListenerBase {
public void ClientAppHost()
: base ("Hello Client Services", typeof(HelloService).Assembly) {}
public void Configure(Container container) {
var redisFactory = new PooledRedisClientManager("redishost:6379");
var mqHost = new RedisMqHost(redisFactory, noOfRetries:2, null);
//Client - MQ Service Impl:
//Listens for 'HelloResponse' as returned by the 'Hello' service on the server
mqHost.RegisterHandler<HelloResponse>(m => {
Consle.Log("Received: " + m.GetBody().Result); //Fired
});
mqHost.Start(); //Starts listening for messages
}
}
var clientAppHost = new ClientAppHost();
clientAppHost.Init();
Console.ReadLine(); //Block the server from exiting (i.e. if running inside Console App)
With the above configuration the client can talk to the server with:
var mqClient = mqHost.CreateMessageQueueClient();
mqClient.Publish(new Hello { Name = "Client 1" });