Week roundup

Last week recap and links:
Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

Posted in Uncategorized | Tagged , , , , , , | Leave a comment

Call custom .NET code from powershell

Consider the following code in HelloWorld.API.dll.

namespace HelloWorld.API {
    public class Praise {
        public static string Someone(string nameOfPersonToPraise) {
            return string.Format("{0} you're awesome!", 
                                 nameOfPersonToPraise);
        }
    }
}

Make the API available with the Add-Type cmdlet. This makes all the types in a .NET assembly available in the current session.

Add-Type -Path PATH_TO\HelloWorld.API.dll

Now the static method can be called by using the :: (two colons) syntax.

[HelloWorld.API.Praise]::Someone("eric")

The output is

eric you’re awesome!

The output can be further processed by using a variable to hold it.

$praised = [HelloWorld.API.Praise]::Someone("eric")
$praised.ToUpper();

The output is

ERIC YOU’RE AWESOME!

Gotcha

When developing remember that the assembly is locked after the Add-Type until the session ends.

When to use

We use this to setup our automated (test) environment with an API. No need for custom console applications, just plain powershell.

This works for all .NET assemblies. Use it wisely.

Posted in Tooling | Tagged , | Leave a comment

Week roundup

Last week recap and links:
Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

Posted in Uncategorized | Tagged , , , , | Leave a comment

Reduce wait time with tasks

In our project we group multiple machines and request information from each one. Sometimes this takes very long.

All machines are contacted in sequence. When one machine is offline the request will timeout in 10 seconds (default WCF behavior) This way the user must wait very long when some machines are offline. The graph below shows the messages captured with fiddler when one machine is offline. Look for the exclamation mark.

Sequential calls captured with fiddler

We could call the services asynchronous but that would mean handling events and messing up the code.

Using tasks we now call the services in parallel. In the code sample below are the old and new sources.

// old code
foreach(var machine in machines) CallTheService(machine);

// new code
var tasks = machines.Select(machine => 
   Task.Factory.StartNew(() => CallTheService(machine))
).ToArray();
Task.WaitAll(tasks);

This way we don’t reduce the wait for an offline machine, but make better use of the idle time by doing other requests. The graph below shows the messages captured with fiddler of the same group of machine as the graph above. The total process took 16 seconds in stead of 35.

fiddler.parallel.with.tasks

When a user has to wait it is always to long. Make sure you do the best you can to reduce the wait.

Posted in Development | Tagged , , , | Leave a comment