Auto advancing state machine with Stateless - c#

I've been experimenting with Stateless (HSM in C#) (https://code.google.com/p/stateless/) lately and I've come across something that I'm not really sure how to achieve.
Let's say I have the following states:
Start.
Connect
Read
Finish
What I'm trying to achieve is: when the TCP connection (in the Connect state) is established, advance to the Read state. Or, if it fails, advance to the Finish state (where it may return to the Connect state and attempt a new connection after a timeout period).
How can I achieve this auto advancing feature using Stateless, since firing triggers from within the states can cause a stack overflow exception?
Cheers

Given that I found no native solution on Stateless to do what I asked, I ended up wrapping the .Fire(trigger) in a task
Task.Start(() => _stateMachine.Fire(trigger));
Doing so, means that the state machine does not advance itself per say, but it's rather advanced by an external source, solving the SO exception.

Related

Monitoring disconnects in QuickFIX/n

Working with QuickFIX/n and need to find a way to monitor potential crashes on the executor side (I am developing the client side). I see there is logging when a connection stops but no way of tracking and triggering anything in the code.
I have looked at Quickfix/n - No event for connection timeout or host not found? but it only addresses initial connection, not crashes post connection. QuickFIX/J has SessionStateListener https://quickfixj.org/javadoc/1.6.4/quickfix/SessionStateListener.html but not finding anything similar in the C# variant.
Basically, need to find a way to create an observer but do not see anything built in that could be of use.
This isn't a definitive solution but ends justify the means for now.
Have created own implementation of the ILogFactory (rather than use builtins) and it listens to the message containing " disconnecting: " then sends info to applicable services. Causes some strange coupling but currently not seeing a clean way like the java implementation does it.

UWP App using Windows.Devices.WiFi

I am writing a UWP app using the Windows.Devices.WiFi to basically get a lists of networks. Everything was working fine when I retrieved the information a time or two. However, I wanted to put the code into a timer so I can report regularly. Once I did this, I got "an attempt was made to establish a session to a network server, but there are already too many sessions established to that server."
I am not sure what is establishing connections as I am just trying to read the information. I am not even calling the ConnectAsync calls.
Can anyone help me out? I need to know what to dispose, or close, etc.
Update: Further analysis, I am finding that the call to FindAllAdaptersAsync multiple times is causing this issue.
I decided to cache up the list of adapters by only calling FindAllAdaptersAsync once. Thanks for the idea Henk. This seemed to fix my issue for now. However, I think that it is a bug with FindAllAdaptersAsync. I would think you should be able to call this as much as you like, unless maintaining the network connection is necessary every time. Or at least a way to free them up.

using Cassandra c# driver with multi-thread

I'm using c# driver for Cassandra with multi-threads processing.
At first, I tried to create a connection and execute command and then close the connection after done the work. But it's seem not to work for me, sometimes it's got an exception that No hosts available.
So, I changed to working with static connection. it's seem to work as well.
But when thread is working too fast, it's broken again. I've to put some Thread.Sleep for 1 second then it works.
And with this static solution, I tried to use Asynchronous process, BeginExecute and it's not work for me as well, exception No Hosts Available.
So, anyone has better ideas or better implementation on multi-threads processing working with Cassandra c# driver, it would be appreciate if you can share.
Thank you in advance.
Cheers,
Kin
CassandraSession can only have one connection at a time. It probably isn't thread safe now that I think about it. But the Connection Pool is thread safe, so if you use that you will always have a high availability connection
If you use asynchronous method,it looks like that:
Statement sta=new SimpleStatement("Select * from XXX where XXX;");
session.ExecutAsync(sta);
Make sure your setup is satisfying the connection requirements
The C# driver should definitely work in a multi-threading environment (1 cluster object, 1 session object/keyspace)
It's difficult to say why you are seeing the NoHostAvailable exception without seeing any code.

web calls never timing out

I have a number of applications using various web technologies such as SOAP, WCF Services, or just simple XmlReader. However they all seem to suffer the same problem of missing the timeout and hanging infinitely if the internet connection suffers problems at the wrong time.
I have set the timeout in all scenarios to something small, e.g. for wcf
closeTimeout="00:00:15" openTimeout="00:00:15"
receiveTimeout="00:00:15" sendTimeout="00:00:15"
or for soap
_Session.Timeout = (int)TIMEOUT.TotalMilliseconds;
These timeouts do generally get hit, however it appears there is some special case where if the internet drops out at the right time, the call will hang and never time out (using synchronous calls).
I was considering starting up a timer every time I make a call, and using the appropriate .Abort() function if the timer expires to cancel the call. However, I was wondering if there was a simpler way to fix the issue and ensure the timeout gets hit.
Does anyone know why this occurs, and if so what a clean/simple/good way is to ensure the calls always time out?
I can guess at why it occurs, but without giving a solution :(
I suspect it's getting caught up on DNS resolution. I've seen various situations where that "doesn't count" - e.g. where it ends up happening on the initiating thread of an asynchronous call, or where it's definitely not been included in timeouts.
If you're able to reproduce this by pulling out your network cable, I'd suggest using Wireshark to confirm my guess - that would at least suggest further avenues for investigation. Maybe there's a DNS timeout somewhere in the .NET stack which is normally infinite but which can be tweaked, for example.

Is it possible for one WCF session to throw an exception in another session?

if an administrator logs on to my service, he may wish to disconnect sessions which meet (or don't meet) certain requirements, be it automated or manual. throwing exceptions seems like a simple and effective solution, as all resources are released.
i could use a local bool field which, if true, would disconnect this user the next time he calls any of the methods, but that doesn't seem like an elegant solution.
and, it doesn't have to be throwing exception, as i've already noticed you can use OperationContext.Current.Channel.Close(), or abort, to disable access to that session.
is there a "standard" way to do this in wcf?
From within a given 'session', I do not think there is an out-of-the-box way to do anything with another session (i.e. instance context).
You can however track clients that connect to your service by utilizing the IChannelInitializer interface in conjunction with a service or contract behavior. This would give you access to each clients' associated channel which you could then close if you wanted to, although this would not necessarily produce a very informative fault on the client end as it would look like a communication issue.
Another option is to look at callbacks if you have control over the clients that are accessing the service. By utilizing the above mentioned client tracking in conjunction with callbacks you can actually cause the client to more gracefully close its own channel and potentially inform the user of what happened.

Categories