Footsteps in the sand, the seashore mind…

.Net Development

If you want to ensure a windows form cannot be dragged out of the view-able area of a multiple monitor screen and also want the option to dock it to the monitor it was actively on, then this code might be helpful. It also has a tolerance level of 50% where 50% of the form can be out of the view-able area.

You might think you do not need to enumerate the screens, but you do, if you want to dock it, especially if some screens are portrait and others are landscape.

You can optimize the code by storing the LeftMost and RightMost screen in a global static location.

When dealing with JSON data, and you dealing with large result sets, say larger than a 1MB or so, it will definitely be feasible in many situation to zip the data before sending it to your client application.

The first step is to add zipping to the pipeline that Nancy uses, we then check that the content type returned in the response is JSON and we check that the client can accept the encoding of GZIP.

Summary

This document explains how to setup a SAGA to SAGA request Response pattern. Bus.Reply is used, as ReplyToOriginator is not supported. We will simulate a service receiving an order, then sending it to an Order Service, which then has a request/response pattern to process payment.

TimeOuts and responding to the original calling (originator) saga

Never use Bus.Reply within a TimeOut handle in the Saga, as it will try to reply to the timeout queue, as Bus.Reply will always respond to the source of the LAST incoming message.

To get ReplyToOriginator working between Saga’s, you need to:

Ensure the called Saga (ProcessOrder) lives LONGER than the called (Payment), by using timeouts in both sagas

You need to add a correlation

This is the message pattern with timeouts and a polling pattern, which you can run indefinitely if ever needed.

Create three endpoints

Click New endpoint

Create an OrderReceiver, as an NServiceBus Host. Do the same for OrderSaga and PaymentSaga

Your canvas will look like this

Send a message from OrderReceiver to OrderSaga

So now we will simulate a service (OrderReceiver) that receives orders on a backend system and then sends them to our OrderSaga for long running transaction processing

Click the OrderReceiver and click “Send Command”

Set the Service name to Orders (Domain) and the command ProcessOrder

Your canvas should look like this

Click the undeployed component and select Deploy

Select OrderSaga as the destination and click Done

Your canvas should look like this, with a bit of interior design J

Edit the ProcessOrder message and add the following propertiesOrderIdAmount

Open the ProcessOrderSender.cs file under the Orders folder, we will configure it to send 3 orders. We will implement IWantToRunWhenBusStartsAndStops

Note that I am not in the infrastructure folder, as this is generated code.

Build the solution

Configure the OrderSaga as a Saga and Message Correlation

Great, so now we have the minimum need to set the OrderSaga endpoint to a real Saga, as a SAGA MUST have a message to handle. In this case ProcessOrder.

Click the ProcessOrderHandler and click “Convert To Saga”

This will open the ProcessOrderHandlerConfigureHowToFindSaga.cs file. Build the solution, so that partial classes are generated.

We want to correlate order messages based on the orderId to the correct Saga instance. So here we will set the properties on how to find it. Add the following code:

Open the file ProcessOrderHandlerSagaData.cs and add the OrderId, set the property to Unique, as this is how the Saga will correlate messages to the correct instance.

Excellent, so now we have correlation established between the OrderReceiver and the OrderSaga. So if ever the Saga receives order updates for the same order, the infrastructure will no which instance to send the processorder command to.

Configure Saga To Saga Command

Here we will configure the OrderSaga to send a message to the PaymentSaga, then we will update the PaymentSaga to become a Saga.

Click the ProcessOrderHandler and click SendCommand

Name the command ProcessOrderPayment

Click Copy to Clipboard. This will then open the ProcessOrderHandler.cs file. Paste the code.

Open the Canvas, it should look like this

Click the ProcessOrderPaymentHandler, and Click Deploy.

Select the Payment Saga, as this will handle the ProcessOrderPayment Request.

Your canvas will look like this. BUILD SOLUTION

Let’s CONVERT PaymentSaga endpoint to a Saga, as we have the minimum needed to do this!WARNING: NEVER convert an endpoint to a saga unless it has at least one message handler, else it cannot implement IAmStartedByMessages interface. You would have to wire it up manually, since the Infrastructure code generator will not know how.

Click ProcessOrderPaymentHandler and click Convert to Saga…

This will open the ProcessOrderPaymentHandlerConfigureHowToFindSaga

Build the solution, to auto generate the Saga partial classes and infrastructure

We want the payment instance to correlate to the correct order Id, so add this:

Build the solution! We added properties so ConfigureHowToFindSaga will compile J

Configure Saga To Saga Response and Bus.Reply

Open the ServiceMAtrix Canvas, confirm your canvas looks like thisnotice the icon for saga’s has a circle in it with a square.

Click the ProcessOrderPaymentHandler in the payment Saga and click Reply with Message…

Click Ok

Copy the code to Clipboard

Click the Copy To Clipboard, note the mouse pointer will show as busy, however you can still click copy to clipboard.

This will open the ProcessOrderPaymentHandler.cs, paste the code here. Put in a Thread.Sleep to simulate a credit card payment.

Your canvas will look like this now

Add the following code to ProcessOrderHandler.cs file

Build the solution

Testing the solution

Following the following in Order, so the msmq’s are created in the correct order, to avoid race conditions on the first time is starts.

Below is code that you can use to terminate processes that belong to the currently logged on user. This is using WMI and will work with all authenticated users, even non administrators.

You can use ExcludeMe to exclude a process, e.g. if you running a program and want to guarantee one instance on the machine, but it must not kill the current program.
KillProcesses(Process.GetCurrentProcess().ProcessName + “.exe”, false, Process.GetCurrentProcess());
The above will kill all other processes with same name, except the calling program.
e.g.

Of course no developer wants to write software that is not Unit Testable, else you going to spend 1 months building a project and 2 month debugging it! Why not get your Unit Tests started up front, then write all your use cases as unit tests and bang them one by one.

PowerScribe360 has allot of new features for Radiologists and we want to test when events get published or received, as well as fake all the internal dependencies of the COM e.g. Terminate require PowerScribe to be open, and of course when unit testing, this is a no no, as we want NO external dependencies.

So we will create a wrapper, here is the code, and you can get it from here as well:Source Code

Note that PowerScribe360 is a class I created to wrap up the RadWhereCom into a public property e.g.

After spending some time working with Medical DICOM Images, I have found that it can be a rather complex process to try get Image Counts off a PACS server, as the way to get it, is different when running a DICOM query at the STUDY or IMAGE level.

When CT Scans are being uploaded into your RIS server, it is very important to know what the number of images, especially for large studies with over 1000 images. So that you can ensure all images are received, so that Radiologists can start reporting immediately.

We leverage NService Bus to handle all the Study orders coming in, and the SAGA needs to know when to end, and part of the dependency is based on the number of images arrived during the import phase, akin to when a purchase order and all it’s items have been received by an order processor.

Basically, when you query a MASTER PACS server, the query level must be set to Study, and the way you get your image counts is based on a dataset field (0x0020, 0x1208).

When you query a Modality PACS server (Preferred), you use the Image Level query, and then can get the image count directly off the DicomDataSetCollection.

I have created a DICOM query Wrapper, that you can now use. Which can support backup servers to query, in the event the current server you are querying is not available. This can happen, if the PACS server is used allot, and it on the 50 connection limit.

Notice above, you execute a BuildPreferred() or BuildMaster(), this logic encapsulates how the query will be built, if it is a Modality server, it will set the query level to Image, and if it is a Master server, will set it to Study level. The logic then to find the ImageCount is automatically done for you.

I am not sure why, but in many organisation, there is allot of unnecessary complexity when looking at source code. From Programmers using try/catch blocks all over the show to unnecessary synchronous service calls with multiple threads, causing deadlocks on SQL Server.

The above code is extremely dangerous. You have no control over the load it will cause to backend calls, especially when it is calling Stored Procs etc. Secondly, it is not durable.

The above code, can easily be replaced by a NServiceBus Saga or handler, in the above, a Saga is appropriate, as this message is a Root Aggregate, we have an order to process. Sagas will provide an environment to preserve state and all the threading and correlation is all handled for you.

From the above, you can see state is preserved for each unique OrderId. This Saga processes multiple order items for an OrderId. Which can be submitted at any time during the day. We do not have to worry about multiple threads, correlation is dealt with automatically, well we set the correlation id to the OrderId, so order items coming in can be matched to the correct Saga instance.

We can now get rid of the unreliable issues with in memory worker threads, and uncontrolled pounding of the database.

By using a MSMQ infrastructure and leveraging NServiceBus, these sort of issues, you find in code, can be easily mitigated.

I have just completed a script that can be used to install a service bus host as a windows service. It is nice to use, when delegating the installs to the support team, this is always done via NServiceBus.Host.exe.

It can always be tricky parsing arguments in PowerShell. The trick is to use the Start-Process command, and isolate the arguments into it’s own variable.

Now, remember, if you install Subscriber endpoints before publishers, you will get a race condition, so with this script, add the endpoints in the array argument in the correct dependent order. First install all the publisher endpoints first, then the subscriber endpoints.

In a future post, I will post source code for a Balanced Binary Search Tree (Red Black Tree RBTree).

Remember with an unbalanced Binary Search Tree, if you insert data in order, it will actually resemble a linked list and not a binary tree, hence why it is important to use a Balanced Binary Search Tree.

Of course, you can use built in .NET framework types like SortedSet and SortedDictionary, but for those of you that want to write your own implementation, then this can be a good starting point.

Remember, the rules for a Binary Search Tree, which will also return data in a sorted order, using your IComparer implementation.

I also wrote a method to delete nodes from a Binary Search Tree, which will treats leaf nodes, nodes with one child and nodes with two children differently.

There is allot of articles out there on BST’s, but it is important, that if you want to understand them, that you must understand the rules governing the Insertion, Deletion and Retrieval algorithms.

I ran into a scenario where I needed to transfer data from an Azure Cloud Drive VHD that was 250GB in Size from Production to Preproduction. So this means that I want to transfer the VHD from one azure account to another.

The thing with VHD’s and Cloud Drive, is that it is a Page Blob, and the VHD has to be a fixed size, so even though I might have 200MB of data in a 250GB VHD, you would need to download 250GB worth of data.

The Solution?

Remove desktop into the Azure Virtual Machine, Mount the VHD Manually, copy the data, zip it up and send it through other means, so in essence, I only send or download the data that is USED in the VHD i.e. 200MB and not 250GB.

This utility can use the concept of a blobsnapshot VHD backup to restore, and what it will do it mount it. This is ideal when you using blob snapshots as a backup mechanism and you need a way to restore the blob snapshot VHD data, as fast as possible to another Azure Account.

Below is the code for the helper and you can download the source code for the project here:

NOTE: This application must be run in a Windows Azure Virtual Machine, it will NOT WORK on a development machine/emulator.