How to receive messages in a .Net 6 static class using the Community Toolkit Messenger - mvvm

I'm using the Microsoft Community Toolkit's default Messenger to send messages between modules in my program and it works great - until I try to receive a message in a static class.
I can't make the class derive from ObservableRecipient because it is static.
I can't implement IRecipient because it is static.
I can't use WeakReferenceManager.Default.Register(this, (r, m) => MsgHandler(m)) because I can't use "this" in a static class.
WeakReferenceMessenger.Default.Register(ClassName.handler,
(r, m) => handler(m.newStat)) compiles okay, but "handler" doesn't
get called when the message is sent.
I've been struggling through the Microsoft Learn pages about Messenger and Register, but I don't know enough to be able to follow the descriptions. (These particular pages seem to be written by experts for experts, and I'm not expert (or smart) enough to figure out what they are talking about.)
My question is: Can a static class receive messages (seems very likely), and if so, what syntax do I have to use to register? It must be some variation of the last example above, but I haven't yet been able to figure out what it would be.
Thanks.

Related

How to get a kill switch per user for Akka Http Websocket connection?

I'm new to Akka and Scala and self learning this to do a small project with websockets. End goal is simple, make a basic chat server that publishes + subscribes messages on some webpage.
In fact, after perusing their docs, I already found the pages that are relevant to my goal, namely this and this.
Using dynamic junctions (aka MergeHub & BroadcastHub), and the Flow.fromSinkAndSource() method, I was able to acheive a very basic example of what I wanted. We can even get a kill switch using the example from the akka docs which I have shown below. Code is like:
private lazy val connHub: Flow[Message, Message, UniqueKillSwitch] = {
val (sink, source) = MergeHub.source[Message].toMat(BroadcastHub.sink[Message])(Keep.both).run()
Flow.fromSinkAndSourceCoupled(sink, source).joinMat(KillSwitches.singleBidi[Message, Message])(Keep.right)
}
However, I now see one issue. The above will return a Flow that will be used by Akka's websocket directive: akka.http.scaladsl.server.Directives.handleWebSocketMessages(/* FLOW GOES HERE */)
That means the akka code itself will materialize this flow for me so long as I provide it the handler.
But let's say I wanted to arbitrarily kill one user's connection through a KillSwitch (maybe because their session has expired on my application). While a user's websocket would be added through the above handler, since my code would not be explicitly materializing that flow, I won't get access to a KillSwitch. Therefore, I can't kill the connection, only the user can when they leave the webpage.
It's strange to me that the docs would mention the kill switch method without showing how I would get one using the websocket api.
Can anyone suggest a solution as to how I could obtain the kill switch per connection? Do I have a fundamental misunderstanding of how this should work?
Thanks in advance.
I'm very happy to say that after a lot of time, research, and coding, I have an answer for this question. In order to do this, I had to post in the Akka Gitter as well as the Lightbend discussion forum. Please refer to the amazing answer I got there for some perspective on the problem and some solutions. I'll summarize that here.
In order to get the UniqueKillSwitch from the code that I was using, I needed to use the mapMaterializeValue() method on the Flow that I was returning. Here is the code that I'm using to return a Flow to the handleWebSocketMessages directive now:
// note - state will not be updated properly if cancellation events come through from the client side as user->killswitch mapping may still remain in concurrent map even if the connections are closed
Flow.fromSinkAndSourceCoupled(mergeHubSink, broadcastHubSource)
.joinMat(KillSwitches.singleBidi[Message, Message])(Keep.right)
.mapMaterializedValue { killSwitch =>
connections.put(user, killSwitch) // add kill switch in side effect once value is ready from materialization
NotUsed.notUsed()
}
The above code lives in a Chatroom class I've created that has access to the mergehub and broadcast hub materialized sink and source. It also has access to a concurrent hashmap that persists the kill switch to a user. In this way, we now have access to the Kill Switch through querying it through a map. From there, you can call switch.shutdown() to kill the user's connection from the server side.
My main issue was that I originally thought I could get the switch directly even though I didn't control the materialization. This doesn't seem possible. I suggest this method for when you know that the caller that requires your Flow doesn't care about the materialized value (aka the kill switch).
Please reference the answer I've linked for more scenarios and ways to handle this problem.

Multiple notifications with MediatR and Autofac ContravariantRegistrationSource

I am trying to use MediatR to set up generic notifications, but the notification handler is being called multiple times.
https://github.com/smartaypants/MediatR/blob/master/test/MediatR.Tests/CustomNotificationTests.cs
The test publishes a CustomNotification which implements ICustomNotification. The CustomNotificationHandler is constrained to accept TNotification which must implement ICustomNotification.
I would expect this to only be called once but the handler is being called 3 times - where TNotification is CustomNotification, CustomNotificationBase and ICustomNotification - but they are all the same instance... should that happen?
If I remove the ContravariantRegistrationSource line from the Autofac registration then it works as expected. Similarly if I use the StructureMap registration it works fine.
I don't entirely understand why I need to enable contravariance. I am only using this line because it is included on the MediatR wiki and almost every example I can find.
Please can someone explain why this is happening and if I am doing something wrong, or is it a bug in Autofac. Thanks.

Redefining Mail notifications template for Spring Boot Admin

Can we have a sample of variables available for redefinition of templates, documentation is scarce on this?
In Class package de.codecentric.boot.admin.notify.MailNotifier I read
private static final String DEFAULT_SUBJECT = "#{application.name} (#{application.id}) is #{to.status}";
private static final String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
In my specific use case I inherited a bunch of applications which I want to ping or monitor they are alive. I have no control on them but my app depends on them.
My app doesn't fail but may misbehave (important point!).
So I added a bunch of customs HealthIndicator to get them monitored by ping, that work perfectly, and I was very pleased by changing an IP a notification was sent, great!
But there is a but, the message sent let me think than the app was failing, while instead the health indicator was status OUT_OF_SERVICE, unnecessary stress for DevOps.
Come to my question, how can I add some extra variables whereby some carefully crafted SPEL will distinguish an Health indicator message status change than an application status change ie webapp going offline.
Is a dictionary of SBA keywords available to use for redefining for example spring.boot.admin.notify.mail.text Mail notifications configuration option?
I guess this is valid to other notifiers being hipchat, Slack.
Thank you.
The context for evaluating the SpEL expression is the event.
Therefore all properties from ClientApplicationEvent (or the corresponding subclass for the event instance (e.g. ClientApplicationStatusChangedEvent)) are available.
If it doesn't suffice you can ship your own subclass of the MailNotifier..
Answer is no, cannot do in current code base without code changes.
SBA notification keywords/variables available are the following:
application.name
application.id
application.healthUrl
application.managementUrl
application.serviceUrl
application.statusInfo.status
from.status
to.status
timestamp
type
We don't have anything else.
However the StatusUpdater.queryStatus looks promising to get some extra data.

Quickfix or Quickfix/n, through which message type are custom U1, U2,....,Un messages identified?

I cannot find anywhere how to manage custom U-type messages. I use MessageCracker and need to understand the corresponding OnMessage method signature. For example my broker sends custom U1,U5, U2 messages, how can I capture those incoming messages inside a OnMessage method? I understand that Tag35 identifies those but if I cannot capture them through OnMessage then MessageCracker becomes kind of useless and I need to identify each message by Tag35 within FromApp or FromAdmin. Any suggestion how to handle those kind of custom U-types?
Thanks
Ah, custom messages. Fun stuff.
You need to add your counterparty's customizations to the DataDictionary xml file. Choose the appropriate FIXnn.xml file for your FIX version.
See here: http://quickfixn.org/tutorial/custom-fields-groups-and-messages
Then, because you are adding custom messages, you'll want to regenerate the QF/n source and rebuild the library so you can get classes for your new messages.
Instructions for rebuilding are here: https://github.com/connamara/quickfixn
You'll need to install Ruby. This annoys some people, but we haven't found any more-windows-oriented code-generator that we don't hate. Sorry in advance.
(If you were just adding fields to existing messages, you could probably get away with not rebuilding. But you're adding messages, so you pretty much have to regenerate/rebuild.)
To Windows developers, it may seem annoying that rebuilding the library is required, but it really is the norm for all the QF engines. FIX is just too fudgey a protocol for a single build to satisfy everyone, because it seems that every counterparty likes to screw with the message definitions.

How to use Trampoline IOS

I was looking for this on google and I found some articles on it They say it is used for HigherOrderMessaging and I tried to read the code to but everything was over my head can any body give simple example of it how we can use them? They were saying its used for passing returned object from method to another object. And another question when I develop apps never came situation where I need to use something like this.
In Objective-C, a trampoline is an object returned by a method that exposes some kind of message interface. When messages are received, it bounces the message on to another object.
Example One:
Return a proxy of a service client. When methods are invoked on the proxy, it first checks if the user has permission to proceed.
Example Two:
Make all the objects in an array do something:
[[windowsArray do] setHidesOnDeactivate:YES];