Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active January 21, 2020 07:27
Show Gist options
  • Save gistlyn/355338cd60a32ee9c9fc4761269f7782 to your computer and use it in GitHub Desktop.
Save gistlyn/355338cd60a32ee9c9fc4761269f7782 to your computer and use it in GitHub Desktop.
Add /messaging pages and Services to test sending MQ messages
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Messaging;
namespace MyApp
{
/**
Register Services you want available via MQ in your AppHost, e.g:
var mqServer = container.Resolve<IMessageService>();
mqServer.RegisterHandler<MyRequest>(ExecuteMessage);
*/
public class ConfigureMq : IConfigureServices, IAfterInitAppHost
{
public void Configure(IServiceCollection services)
{
services.AddSingleton<IMessageService>(c => new BackgroundMqService());
}
public void AfterInit(IAppHost appHost)
{
appHost.Resolve<IMessageService>().Start();
}
}
}
using ServiceStack;
using ServiceStack.Script;
using ServiceStack.Messaging;
using MyApp.ServiceModel;
namespace MyApp
{
public class FeatureMq : IConfigureAppHost, IPostInitPlugin
{
public void Configure(IAppHost appHost)
{
var mqServer = appHost.Resolve<IMessageService>();
mqServer.RegisterHandler<TestMq>(appHost.ExecuteMessage);
appHost.AssertPlugin<SharpPagesFeature>().ScriptMethods.Add(new MqScripts(mqServer));
}
public void AfterPluginsLoaded(IAppHost appHost)
{
View.NavItems.Add(new NavItem {
Label = "Messaging",
Href = "/messaging",
});
}
}
public class MqScripts : ScriptMethods
{
IMessageService mq;
public MqScripts(IMessageService mqService) => this.mq = mqService;
public IMessageService mqService() => mq;
public string mqStatsDescription(IMessageService mqService) => mqService.GetStatsDescription();
}
}
using System;
using ServiceStack;
using ServiceStack.Messaging;
using MyApp.ServiceModel;
namespace MyApp.ServiceInterface
{
public class MqServices : Service
{
public IMessageService MqService { get; set; }
public void Any(PublishMq request)
{
PublishMessage(request.ConvertTo<TestMq>());
}
public object Any(TestMq request)
{
if (!string.IsNullOrEmpty(request.Name))
Cache.Set("mq.name", request.Name);
if (request.Add > 0)
Cache.Increment("mq.counter", (uint)request.Add);
else if (request.Add < 0)
Cache.Decrement("mq.counter", (uint)(request.Add * -1));
return new TestMqResponse {
Name = Cache.Get<string>("mq.name"),
Counter = Cache.Get<long>("mq.counter"),
StatsDescription = MqService.GetStatsDescription(),
};
}
}
}
using System;
using ServiceStack;
namespace MyApp.ServiceModel
{
[Route("/mq/publish")]
public class PublishMq : IReturnVoid
{
public string Name { get; set; }
public int Add { get; set; }
}
[Route("/mq/test")]
public class TestMq : IReturn<TestMqResponse>
{
public string Name { get; set; }
public int Add { get; set; }
}
public class TestMqResponse
{
public string Name { get; set; }
public long Counter { get; set; }
public string StatsDescription { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
}
<!--
title: Test MQ Page
-->
<div class="row justify-content-between pt-3">
<div class="col">
<h4 class="pb-3">Test MQ</h4>
<div class="row">
<div class="col col-2">
<div class="btn-group ml-3">
<button class="btn btn-outline-primary" data-click="incr:-1">-</button>
<button class="btn btn-outline-primary" data-click="incr:1">+</button>
</div>
</div>
<div class="col col-auto">
<input class="form-control ml-1" type="text" id="txtName" value="Test" placeholder="Name" data-input="incr:0">
</div>
</div>
<div class="py-3 ml-3">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="mq-publish" id="radOneWay" value="OneWay" checked>
<label class="form-check-label" for="radOneWay">OneWay Endpoint</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="mq-publish" id="radPublishMq" value="PublishMq">
<label class="form-check-label" for="radPublishMq">PublishMq Service</label>
</div>
</div>
<div class="py-2">
<button class="btn btn-secondary" data-click="refresh">Refresh State</button>
</div>
<dl>
<dt>Name:</dt>
<dd id="name"></dd>
<dt>Counter:</dt>
<dd id="counter"></dd>
</dl>
</div>
<div class="col">
<pre id="mqstats">{{ mqService.mqStatsDescription() }}</pre>
</div>
</div>
{{#raw appendTo scripts}}
<script>
// Can remove generated DTO's from MqServices once included in App's dtos.js
if (typeof TestMq == 'undefined') {
TestMq = /** @class */ (function () {
function TestMq(init) {
Object.assign(this, init);
}
TestMq.prototype.createResponse = function () { return new TestMqResponse(); };
TestMq.prototype.getTypeName = function () { return 'TestMq'; };
return TestMq;
}());
}
if (typeof PublishMq == 'undefined') {
PublishMq = /** @class */ (function () {
function PublishMq(init) {
Object.assign(this, init);
}
PublishMq.prototype.createResponse = function () { };
PublishMq.prototype.getTypeName = function () { return 'PublishMq'; };
return PublishMq;
}());
}
if (typeof client == 'undefined')
client = new JsonServiceClient('/');
var timeout,
$name = document.getElementById("name"),
$counter = document.getElementById("counter"),
$mqstats = document.getElementById("mqstats"),
$txtName = document.getElementById("txtName"),
$chkOneWay = document.getElementById("chkOneWay");
function refresh() {
client.get(new TestMq())
.then(function (r) {
$name.innerHTML = r.name;
$counter.innerHTML = r.counter;
$mqstats.innerHTML = r.statsDescription;
});
timeout = null;
}
function refreshTimeout() {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(refresh, 250);
}
bindHandlers({
incr(add) {
var oneway = document.querySelector('input[name=mq-publish]:checked').value === "OneWay";
if (oneway) {
client.publish(new TestMq({ name: $txtName.value, add: parseInt(add, 10) }))
.then(refreshTimeout);
} else {
client.post(new PublishMq({ name: $txtName.value, add: parseInt(add, 10) }))
.then(refreshTimeout);
}
},
refresh: refresh
});
</script>
{{/raw}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment