A Tech Log

April 7, 2009

Accessing Strongly Typed XML Data using XPathNavigator

Filed under: Development — adallow @ 4:56 pm
Tags: ,

As an instance of the XPath 2.0 data model, the XPathNavigator class can contain strongly-typed data that maps to common language runtime (CLR) types. According to the XPath 2.0 data model, only elements and attributes can contain strongly-typed data. The XPathNavigator class provides mechanisms for accessing data within an XPathDocument or XmlDocument object as strongly-typed data as well as mechanisms for converting from one data type to another.



March 17, 2009

ADO.Net XML Provider

Filed under: Development — adallow @ 12:58 pm
Tags: ,

The WilsonXmlDbClient is an ADO.NET provider that enables Xml to be worked with just like any other database in .NET. It supports the most common Select, Insert, Update, and Delete SQL syntax, as well as tranactions and parameters.

Wilson.XmlDbClient is the original open source release by Paul Wilson. It includes:



February 2, 2009

Thread Local Storage and .Net

Filed under: Development — adallow @ 9:48 am
Tags: ,

the below is taken from:


The Thread class and System. Threading namespace also contain some methods and classes for realizing thread local storage. Thread local storage is a manner of storing data in a container that is unique to the thread. Many threads could then use the same named container to store their data without concern of collision. Each thread’s local storage is distinct from another thread’s local storage and is only available in the one thread. Listing 3 shows a small sample using the thread-local-storage methods and classes.

using System;
using System.Threading;

namespace ConsoleApplication11
class Class1
public void ThreadStart()
string str1 = "My Cookie "+
Console.WriteLine("worker thread: {0}", str1);
LocalDataStoreSlot lds =
Thread.SetData(lds, str1);
LocalDataStoreSlot lds2 =
string str2 = "";
str2 = (string) Thread.GetData(lds2);
Console.WriteLine("worker thread: {0}", str2);

static void Main(string[] args)
string str1 = "My Cookie "+
Console.WriteLine("main thread: {0}", str1);
LocalDataStoreSlot lds =
Thread.SetData(lds, str1);
Class1 obj = new Class1();
Thread thread = new Thread(
new ThreadStart(obj.ThreadStart));
LocalDataStoreSlot lds2 =
string str2 = "";
str2 = (string) Thread.GetData(lds2);
Console.WriteLine("main thread: {0}", str2);

You could also create and start more than one thread and the behavior of the thread local storage becomes more obvious. I have played with Win32 thread-local-storage functions and created my own for portability to UNIX, but I have rarely found them very useful. I strongly believe in stateless computing and thread-local-storage contradicts this belief.

January 27, 2009

Ordered Execution With ThreadPool

Filed under: Development — adallow @ 9:38 pm
Tags: ,

Interesting from:


“Consider a pipeline implementation that obtains parallelism benefits by having multiple stages of the pipeline active at any one time.

For example, you could have a pipeline that reads in data from a file, compresses it, encrypts it, and writes it out to a new file. The compression can be done concurrently with the encryption, but not on the same data at the same time, since the output of one needs to be the input to the other. Rather, the compression routine can compress some data and send it off to the encryption routine to be processed, at which point the compression routine can work on the next piece of data.
Since many compression and encryption algorithms maintain a state that affects how future data is compressed and encrypted, it’s important that ordering is maintained. (Never mind that this example deals with files, and it’d be nice if you could decrypt and decompress the results in order to get back the original with all of the data in the correct order.)
There are several potential solutions. The first solution is simply to dedicate a thread to each component. This DedicatedThread would have a first-in-first-out (FIFO) queue of work items to be executed and a single thread that services that queue. When the component has work to be run, it dumps that work into the queue, and eventually the thread will get around to picking up the work and executing it. Since there’s only one thread, only one item will be run at a time. And as a FIFO queue is being used, the work items will be processed in the order they were generated.”

December 1, 2008

ASP.NET Performance Checklist

Filed under: Development — adallow @ 11:31 pm
Tags: ,

Design Considerations

ms998596.checkbox(en-us,MSDN.10).gif Consider security and performance.
ms998596.checkbox(en-us,MSDN.10).gif Partition your application logically.
ms998596.checkbox(en-us,MSDN.10).gif Evaluate affinity.
ms998596.checkbox(en-us,MSDN.10).gif Reduce round trips.
ms998596.checkbox(en-us,MSDN.10).gif Avoid blocking on long-running tasks.
ms998596.checkbox(en-us,MSDN.10).gif Use caching.
ms998596.checkbox(en-us,MSDN.10).gif Avoid unnecessary exceptions.


ms998596.checkbox(en-us,MSDN.10).gif Tune the thread pool by using the formula to reduce contention.
ms998596.checkbox(en-us,MSDN.10).gif Consider minIoThreads and minWorkerThreads for burst load.
ms998596.checkbox(en-us,MSDN.10).gif Do not create threads on a per-request basis.
ms998596.checkbox(en-us,MSDN.10).gif Avoid blocking threads.
ms998596.checkbox(en-us,MSDN.10).gif Avoid asynchronous calls unless you have additional parallel work.

Resource Management

ms998596.checkbox(en-us,MSDN.10).gif Pool resources.
ms998596.checkbox(en-us,MSDN.10).gif Explicitly call Close or Dispose on resources you open.
ms998596.checkbox(en-us,MSDN.10).gif Do not cache or block on pooled resources.
ms998596.checkbox(en-us,MSDN.10).gif Know your application allocation pattern.
ms998596.checkbox(en-us,MSDN.10).gif Obtain resources late and release them early.
ms998596.checkbox(en-us,MSDN.10).gif Avoid per-request impersonation.


ms998596.checkbox(en-us,MSDN.10).gif Trim your page size.
ms998596.checkbox(en-us,MSDN.10).gif Enable buffering.
ms998596.checkbox(en-us,MSDN.10).gif Use Page.IsPostBack to minimize redundant processing.
ms998596.checkbox(en-us,MSDN.10).gif Partition page content to improve caching efficiency and reduce rendering.
ms998596.checkbox(en-us,MSDN.10).gif Ensure pages are batch compiled.
ms998596.checkbox(en-us,MSDN.10).gif Ensure debug is set to false.
ms998596.checkbox(en-us,MSDN.10).gif Optimize expensive loops.
ms998596.checkbox(en-us,MSDN.10).gif Consider using Server.Transfer instead of Response.Redirect.
ms998596.checkbox(en-us,MSDN.10).gif Use client-side validation.

Server Controls

ms998596.checkbox(en-us,MSDN.10).gif Identify the use of view state in your server controls.
ms998596.checkbox(en-us,MSDN.10).gif Use server controls where appropriate.
ms998596.checkbox(en-us,MSDN.10).gif Avoid creating deep hierarchies of controls.

Data Binding

ms998596.checkbox(en-us,MSDN.10).gif Avoid using Page.DataBind.
ms998596.checkbox(en-us,MSDN.10).gif Minimize calls to DataBinder.Eval.


ms998596.checkbox(en-us,MSDN.10).gif Separate dynamic data from static data in your pages.
ms998596.checkbox(en-us,MSDN.10).gif Configure the memory limit.
ms998596.checkbox(en-us,MSDN.10).gif Cache the right data.
ms998596.checkbox(en-us,MSDN.10).gif Refresh your cache appropriately.
ms998596.checkbox(en-us,MSDN.10).gif Cache the appropriate form of data.
ms998596.checkbox(en-us,MSDN.10).gif Use output caching to cache relatively static pages.
ms998596.checkbox(en-us,MSDN.10).gif Choose the right cache location.
ms998596.checkbox(en-us,MSDN.10).gif Use VaryBy attributes for selective caching.
ms998596.checkbox(en-us,MSDN.10).gif Use kernel caching on Microsoft® Windows Server™ 2003.

State Management

ms998596.checkbox(en-us,MSDN.10).gif Store simple state on the client where possible.
ms998596.checkbox(en-us,MSDN.10).gif Consider serialization costs.

Application State

ms998596.checkbox(en-us,MSDN.10).gif Use static properties instead of the Application object to store application state.
ms998596.checkbox(en-us,MSDN.10).gif Use application state to share static, read-only data.
ms998596.checkbox(en-us,MSDN.10).gif Do not store single-threaded apartment (STA) COM objects in application state.

Session State

ms998596.checkbox(en-us,MSDN.10).gif Prefer basic types to reduce serialization costs.
ms998596.checkbox(en-us,MSDN.10).gif Disable session state if you do not use it.
ms998596.checkbox(en-us,MSDN.10).gif Avoid storing STA COM objects in session state.
ms998596.checkbox(en-us,MSDN.10).gif Use the ReadOnly attribute when you can.

View State

ms998596.checkbox(en-us,MSDN.10).gif Disable view state if you do not need it.
ms998596.checkbox(en-us,MSDN.10).gif Minimize the number of objects you store in view state.
ms998596.checkbox(en-us,MSDN.10).gif Determine the size of your view state.

HTTP Modules

ms998596.checkbox(en-us,MSDN.10).gif Avoid long-running and blocking calls in pipeline code.
ms998596.checkbox(en-us,MSDN.10).gif Consider asynchronous events.

String Management

ms998596.checkbox(en-us,MSDN.10).gif Use Response.Write for formatting output.
ms998596.checkbox(en-us,MSDN.10).gif Use StringBuilder for temporary buffers.
ms998596.checkbox(en-us,MSDN.10).gif Use HtmlTextWriter when building custom controls.

Exception Management

ms998596.checkbox(en-us,MSDN.10).gif Implement a Global.asax error handler.
ms998596.checkbox(en-us,MSDN.10).gif Monitor application exceptions.
ms998596.checkbox(en-us,MSDN.10).gif Use try/finally on disposable resources.
ms998596.checkbox(en-us,MSDN.10).gif Write code that avoids exceptions.
ms998596.checkbox(en-us,MSDN.10).gif Set timeouts aggressively.

COM Interop

ms998596.checkbox(en-us,MSDN.10).gif Use ASPCOMPAT to call STA COM objects.
ms998596.checkbox(en-us,MSDN.10).gif Avoid storing COM objects in session state or application state.
ms998596.checkbox(en-us,MSDN.10).gif Avoid storing STA components in session state.
ms998596.checkbox(en-us,MSDN.10).gif Do not create STA components in a page constructor.
ms998596.checkbox(en-us,MSDN.10).gif Supplement classic ASP Server.CreateObject with early binding.

Data Access

ms998596.checkbox(en-us,MSDN.10).gif Use paging for large result sets.
ms998596.checkbox(en-us,MSDN.10).gif Use a DataReader for fast and efficient data binding.
ms998596.checkbox(en-us,MSDN.10).gif Prevent users from requesting too much data.
ms998596.checkbox(en-us,MSDN.10).gif Consider caching data.

Security Considerations

ms998596.checkbox(en-us,MSDN.10).gif Constrain unwanted Web server traffic.
ms998596.checkbox(en-us,MSDN.10).gif Turn off authentication for anonymous access.
ms998596.checkbox(en-us,MSDN.10).gif Validate user input on the client.
ms998596.checkbox(en-us,MSDN.10).gif Avoid per-request impersonation.
ms998596.checkbox(en-us,MSDN.10).gif Avoid caching sensitive data.
ms998596.checkbox(en-us,MSDN.10).gif Segregate secure and non-secure content.
ms998596.checkbox(en-us,MSDN.10).gif Only use Secure Sockets Layer (SSL) for pages that require it.
ms998596.checkbox(en-us,MSDN.10).gif Use absolute URLs for navigation.
ms998596.checkbox(en-us,MSDN.10).gif Consider using SSL hardware to offload SSL processing.
ms998596.checkbox(en-us,MSDN.10).gif Tune SSL timeout to avoid SSL session expiration.

Deployment Considerations

ms998596.checkbox(en-us,MSDN.10).gif Avoid unnecessary process hops.
ms998596.checkbox(en-us,MSDN.10).gif Understand the performance implications of a remote middle tier.
ms998596.checkbox(en-us,MSDN.10).gif Short-circuit the HTTP pipeline.
ms998596.checkbox(en-us,MSDN.10).gif Configure the memory limit.
ms998596.checkbox(en-us,MSDN.10).gif Disable tracing and debugging.
ms998596.checkbox(en-us,MSDN.10).gif Ensure content updates do not cause additional assemblies to be loaded.
ms998596.checkbox(en-us,MSDN.10).gif Avoid XCOPY under heavy load.
ms998596.checkbox(en-us,MSDN.10).gif Consider precompiling pages.
ms998596.checkbox(en-us,MSDN.10).gif Consider Web garden configuration.
ms998596.checkbox(en-us,MSDN.10).gif Consider using HTTP compression.
ms998596.checkbox(en-us,MSDN.10).gif Consider using perimeter caching.

Patterns and Practices home

.Net 2.0 and Config MaxConnection Values

Filed under: Development — adallow @ 11:15 pm
Tags: ,

Nearly all microsoft.com Web sites have ASP.NET applications that make calls to remote Web service clusters. The maximum number of concurrent remote Web service calls that can be made from a single Web server is determined by the maxConnection attribute of the <connectionManagement> element in the machine.config file. In ASP.NET 1.1, by default the maxConnection value was set to 2. This old default maxConnection value was much too low for sites like microsoft.com that have hundreds of different applications that make remote Web service calls. The result was that ASP.NET requests would queue while waiting for the remote Web service calls to complete. (You can view the number of queued ASP.NET requests via the perfmon counter ASP.NET\Requests Queued.) To enable more calls to execute concurrently to a remote Web service (and thereby improve the performance of applications on the site), we increased the maxConnection value to 40 for our quad processor Web servers. (The general recommended value for maxConnection is 12 times the number of CPUs, but tune this to suit your specific situation.)
In ASP.NET 2.0, however, you no longer need to configure maxConnection manually as it is now automatically scaled and set. This is a result of the new configuration section for the processModel tag in machine.config (for more information about the processModel element, see “processModel Element (ASP.NET Settings Schema))”.

    <processModel autoConfig="true" />
With autoConfig enabled in machine.config (this is the default setting), ASP.NET sets the value of the maxConnection parameter to 12n (where n is the number of CPUs). Enabling autoConfig also causes the following: the maxWorkerThreads parameter and the maxIoThreads parameter are set to 100, the minFreeThreads parameter is set to 88n, the minLocalRequestFreeThreads parameter is set to 76n, and the minWorkerThreads is set to 50.
Prior to making use of autoConfig in ASP.NET 2.0 to automatically scale and set values for maxConnection and the other attributes in the list, be sure to remove any manually set values for these parameters as these values would be used instead of the autoConfig values. This is something to keep in mind when migrating from ASP.NET 1.1 (where maxConnection needed to be explicitly set) to ASP.NET 2.0, where there are defaults.
Again, the autoConfig values for maxConnection and the other attributes listed previously are somewhat arbitrary and may not work for absolutely every instance, but I have found that these limits work well for nearly all microsoft.com applications.
If you decide that you need to tune the maxConnection value manually, be cautious when increasing its value as this can lead to an increase in CPU utilization. This increase is caused by the fact that more incoming requests can be processed by ASP.NET instead of having them wait for their turn to call the Web service. Of course, you should remember that the maxConnection attribute does not affect local Web service calls, only remote calls.
Interesting I came across this code fragment, wonder if it works….
System.Net.ServicePointManager.DefaultConnectionLimit=<your value here>

November 13, 2008

Processing Collections Using .Net Rules Engine

Filed under: Development — adallow @ 11:18 pm
Tags: ,

Using the rules engines’ chaining behaviour it’s possible to loop through a collection of items and execute a rule for each item in the collection.

E.g. I created a spam filter that uses the rules engine to check for keywords in an email. Instead of creating a separate rule for each keyword you can just store all the keywords in a collection and use the chaining behaviour of the rules engine to loop through the collection executing a rule for each keyword.

To do this i followed a pattern taken from MSDN: Processing Collections In Rules

Rule 1 (Priority = 2) //Always execute this rule once to create the enumerator.
IF 1==1
THEN this.enumerator = this.myCollection.GetEnumerator()
Rule 2 (Priority = 1)
IF this.enumerator.MoveNext()
THEN this.currentInstance = this.enumerator.Current
Rules 3-N (Priority = 0)
…. //Additional rules written against this.currentInstance
Rule N+1 (Priority = -1)
// can be any condition as long as it is evaluated every time;
// this.currentInstance will be evaluated each time this.currentInstance changes, whereas
// “1==1” would only be evaluated once.
IF this.currentInstance == this.currentInstance


Update(“this/enumerator”) //this will cause Rule 2 to be reevaluated

In my spam filter example I had 4 rules which did the following:

  1. Get the enumerator for my collection (List<string>) of spam words
  2. Move the enumerator to the next spam word, and set current spam word variable.
  3. Check if the email contains the current spam word
  4. Update enumerator, so that rule 2 will be re-evaluated (due to chaining behaviour of the rules engine).

Windows Workflow Foundation Rules Engine

Filed under: Development — adallow @ 11:17 pm
Tags: ,

With .Net 3.0 or later there is the ‘Windows Workflow Foundation’ which includes the Rules Engine.

Using the rules engine you can create an external set of rules and execute them against any .Net object.

from http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/10/11/RuleExecutionWithoutWorkflow.aspx

Create a RuleSet

The RuleSet Editor Dialog is exposed as part of the API, so you can use it to create RuleSets as part of your program:

// Create a RuleSet that waorks with Orders (just another .net Object)

RuleSetDialog ruleSetDialog = new RuleSetDialog(typeof(Order), null, null);

// Show the RuleSet Editor


// Get the RuleSet after editing

RuleSet ruleSet = ruleSetDialog.RuleSet;

Serialize and Deserialize rules

Windows Workflow Foundation uses the WorkflowMarkupSerializer to Serialize and Deserialize rules.

// Serialize to a .rules file

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

XmlWriter rulesWriter = XmlWriter.Create(fileName);

serializer.Serialize(rulesWriter, ruleSet);


// Deserialize from a .rules file.

XmlTextReader rulesReader = new XmlTextReader(fileName);

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

ruleSet = (RuleSet)serializer.Deserialize(rulesReader);


Execute RuleSet againt an object

Execute your RuleSet after editing, serializing and deserialising it, but first validate that the rules can be executed against the input object.

// Execute the rules and print the entity’s properties

Order myOrder = new Order(…);

RuleValidation validation = new RuleValidation(typeof(Order), null);

RuleExecution execution = new RuleExecution(validation, myOrder);


An Introduction to WWF and rules engine is available here:


includes useful information about chainging:

Each rule in a RuleSet has a priority value with a default of 0. The rules in a RuleSet can be considered a sorted collection, ordered by the priority values. The WF Rules evaluator evaluates rules individually and executes the rule’s actions based on the results of the rule’s condition evaluation

A selection of examples are available from


November 11, 2008

Mono at Version 2.0

Filed under: Development — adallow @ 11:29 am
Tags: ,

I have a soft spot for Mono having used it a bit a few years ago (and first looking at it in 2001). Anyway a lot of progress has been made. Great for writing .Net apps in a MAC or Linux world:

John BouAntoun has posted about Mono going to Version 2 with ASP.Net 2 support as well as C#3.0

see: http://jbablog.com/2008/10/mono-goes-20/

November 10, 2008

Deploying ASP.NET MVC on ASP.NET 2.0

Filed under: Development — adallow @ 12:01 pm
Tags: ,

Scott Hansleman has an interesting post on deploying ASP.Net MVC on .Net 2 (normally .Net 3.5 sp1 is required), although I am not sure if I would do it in production:


October 17, 2008

Trouble Installing .Net 3.5 in some cases

Filed under: Development — adallow @ 5:54 am
Tags: ,

We were upgrading some old servers which had previously had .Net 1.1 and 2.0 to .net 3.5 and ran into some troubles.

The following blog post had some helpful info:


October 16, 2008

FileSystemWatcher Tips

Filed under: Development — adallow @ 4:14 am

from http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx

“The .NET FileSystemWatcher class makes it possible to quickly and easily launch business processes when certain files or directories are created, modified, or deleted. The FileSystemWatcher class, for example, can be quite useful in application integration, by way of monitoring incoming data files and processing it once an event is raised. It listens to the file system change notifications and raises events when a directory, or file(s) in a directory, changes. The component can watch files on a local computer, a network drive, or a remote computer.

I’ve come to realize that it does need some understanding of this class to make it work efficiently. For example, a denial-of-service attack is possible if a malicious program gains access to a directory the FileSystemWatcher component is monitoring, and which thereby generates so many changes that the component cannot manage them and can cause a buffer overflow or other drastic effects. Following are some tips and notes on how to use the FileSystemWatcher class to build a more robust solution:

1. Events being raised twice – An event will be raised twice if an event handler (AddHander FSW.Created, AddressOf FSW_Created) is explicitly specified. This is because, by default, the public events automatically call the respective protected methods (OnChanged, OnCreated, OnDeleted, OnRenamed). To correct this problem, simply remove the explicit event handler (AddHandler …).

2. Events being raised multiple times – In some cases a single event can generate multiple events that are handled by the component. Because FileSystemWatcher monitors the operating system activities, all events that applications fire will be picked up. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Such multiple events can be correctly handled and processed by using a simple boolean flag check (for a first-come-first-serve approach) or by setting the FileSystemWatcher.NotifyFilter property to one of the NotifyFilters values.

3. Thread Safety – Any public static (Shared in VB) members of this class type are thread safe. Any instance members are not guaranteed to be thread safe.

4. File System Type – The FileSystemWatcher does not raise events for CDs and DVDs, because time stamps and properties cannot change for such media types.

5. Filter – The Filter property can be used to watch for changes in a certain type(s) of file(s) (eg: “*.txt”). To watch for changes in all files, set the Filter property to an empty string (“”). Hidden files are also monitored by the FileSystemWatcher.

6. Internal Buffer – The FileSytemWatcher component uses an internal buffer to keep track of file system actions. You should set the buffer to an appropriate size for the approximate number of events you expect to receive. By default, the buffer is set to a size of 4 KB. A 4 KB buffer can track changes on approximately 80 files in a directory. Each event takes up 16 bytes in the buffer, plus enough bytes to store the name of the file, in Unicode (2 bytes per character), that the event occurred on. You can use this information to approximate the buffer size you will need. You (re)set the buffer size by setting the InternalBufferSize property. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small as possible. Setting the Filter does not decrease what goes into the buffer. If you are using Microsoft Windows 2000, you should increase the buffer size in increments of 4 KB, because this corresponds to the operating system’s default page size. With any other operating system, you should increase the buffer size in increments that correspond to the operating system’s default page size. If you are unsure of the default page size for the operating system you are using, the safest way to proceed is to just double the original size of the buffer. This will maintain the original interval needed for your operating system.

7. Directories – Changing a file within a directory you are monitoring with a FileSystemWatcher component generates not only a Changed event on the file but also a similar event for the directory itself. This is because the directory maintains several types of information for each file it contains – the names and sizes of files, their modification dates, attributes, and so on. Use the IncludeSubdirectories property to indicate whether or not you want to include the subdirectories of the directory you are watching. If you turn this off when you do not need it, you will receive fewer events than when it is turned on.

8. Stop Watching – When file system monitoring is not needed anymore, you should set the EnableRaisingEvents property to False to disable the component, otherwise the component will continue listening.

The FileSystemWatcher class provides a useful feature but it should be implemented along with a thorough examination.”

FileSystemWatcher – Why Does It Stop Working

Filed under: Development — adallow @ 4:12 am

Interesting post at (all content bleow taken from this link):

Taken from: http://www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c9113/

“An application I was working on that used the FileSystemWatcher class and was intended to run 24/7 would periodically stop working. After a little searching, I managed to narrow it down to network outages. You see, the watcher was looking at a network drive, and intermittently (as networks will) it was dropping out and then coming back again. I soon learnt that, when this happens, the FileSystemWatcher loses track of what it was supposed to do.

Easy to fix? Well, it’s not too hard. Check out the example below. You can even test this on your local machine by simply removing the directory that is being searched:”

using System;
using System.IO;

namespace ConsoleApplication1
class Class1
private static FileSystemWatcher watcher =
new FileSystemWatcher();

static void Main(string[] args)

// set up the FileSystemWatcher object
private static void WatchFile()
// ***** Change this as required
watcher.Path = @”\\myServer\c$\test\”;

// For this example, I only care about when new files are
// created
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = “*.txt”;

// Add event handlers: 1 for the event raised when a file
// is created, and 1 for when it detects an error.
watcher.Changed += new FileSystemEventHandler(NewFile);
watcher.Error += new ErrorEventHandler(WatcherError);

// Begin watching.
watcher.EnableRaisingEvents = true;

// Define the found event handler.
private static void NewFile(object source, FileSystemEventArgs e)
Console.WriteLine(“A file has been found!”);

// The error event handler
private static void WatcherError(object source, ErrorEventArgs e)
Exception watchException = e.GetException();
MessageBox.Show(“A FileSystemWatcher error has occurred: ”
+ watchException.Message);
// We need to create new version of the object because the
// old one is now corrupted
watcher = new FileSystemWatcher();
while (!watcher.EnableRaisingEvents)
// This will throw an error at the
// watcher.NotifyFilter line if it can’t get the path.
Console.WriteLine(“I’m Back!!”);
// Sleep for a bit; otherwise, it takes a bit of
// processor time

October 6, 2008

Mono and Tibco RV

Filed under: Development — adallow @ 11:46 am

The below is taken from: http://weblogs.asp.net/mdavey/pages/79456.aspx I came across it a few years ago and found it useful based on what i was doing at the time, I just found it again in my browser favourites so thought i would post it for the sake of completeness….

Tibco RV is often used in investment banks to send market data. Its therefore going to be a requirement for any front-office trading application running on Linux and Mono to subscribe to Tibco RV subjects. What follows is a simple example of the code required to do this – effectively a Mono language binding for Tibco RV. This code was written on RedHat 9.0, with Mono 0.30, and Tibco 7.2.

First we need a C++ library (.so) which uses the Tibco RV C++ API to setup a simple subscription. Callback’s from C++ to C# (unmanaged to managed) are handled by C++ function pointers and normal C# delegates. The C++ “Start” function below will be called by C#, passing a C# delegate, which the C++ will callback on when a message is received on the Tibco RV subject.


#include “tibrv/tibrvcpp.h”

/* Transport parameters */

char* serviceStr = NULL;
char* networkStr = NULL;
char* daemonStr = “tcp::7500”;

typedef void (*stringFunction)(char*);

stringFunction stringFunctionPtr;
/* Message callback class */
class MsgCallback : public TibrvMsgCallback

void onMsg(TibrvListener* listener, TibrvMsg& msg)
const char* sendSubject = NULL;
const char* replySubject = NULL;
const char* msgString = NULL;

// Get the subject name to which this message was sent

// If there was a reply subject, get it

// Convert the incoming message to a string

if (replySubject != NULL)
printf(“subject=%s, reply=%s, message=%s\n”, sendSubject, replySubject, msgString);
printf(“subject=%s, message=%s\n”, sendSubject, msgString);

printf(“About to do callback to C#\n”);

extern “C” void Start(stringFunction f)
stringFunctionPtr = f;
TibrvStatus status;

// open Tibrv
status = Tibrv::open();
if (status != TIBRV_OK)
fprintf(stderr,”Error: could not open TIB/RV, status=%d, text=%s\n”, (int)status,status.getText());

// Create network transport
TibrvNetTransport transport;
status = transport.create(serviceStr,networkStr,daemonStr);
if (status != TIBRV_OK)
fprintf(stderr,”Error: could not create transport, status=%d, text=%s\n”, (int)status,status.getText());

TibrvListener* listener = new TibrvListener();
status=listener->create(Tibrv::defaultQueue(),new MsgCallback(),&transport,”SomeSubject”);
if (status != TIBRV_OK)
fprintf(stderr,”Error: could not create listener on %s, status=%d, text=%s\n”, “SomeSubject”,(int)status,status.getText());
printf(“Listening on: %s\n”,”SomeSubject”);

// dispatch Tibrv events
while((status=Tibrv::defaultQueue()->dispatch()) == TIBRV_OK);

// if everything is Ok we’ll never get here
fprintf(stderr,”Error: dispatch failed, status=%d, text=%s\n”, (int)status,status.getText());

We use the DllImport .NET attribute to tell C# where the C++ “Start” method should be located. The TibCallback delegate must match the C++ function pointer stringFunction from an argument perspective.

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace Test
public delegate void TibCallback(string s);

public class Class1
[DllImport(“”, CharSet=CharSet.Ansi)]
public static extern void Start(Delegate d);

private Thread tibThread;

static void Main(string[] args)
new Class1().Run();

public void Run()
tibThread = new Thread(new ThreadStart(StartTib));
tibThread.IsBackground = true;
tibThread.Name = “TibThread”;

private void StartTib()
Start(new TibCallback(this.TibMsg));

public void TibMsg(string s) {
Console.WriteLine(“TibMsg called”);

.Net moving average calculator

Filed under: Development — adallow @ 11:34 am

I found this in my web travels, It was useful in a simple trading app demo I was working on. Pretty easy stuff, but useful anyway….

// create a new calculator with a window size of 5
MovingAverageCalculator calculator = new MovingAverageCalculator(5);
// loop through the values 1 to 10
for (int i=1; i<=10; i++)
// write out the average

This sample code outputs values:

Source Code:
    /// <summary>
    /// Calculates a moving average value over a specified window.  The window size must be specified
    /// upon creation of this object.
    /// </summary>
    /// <remarks>Authored by Drew Noakes, February 2005.  Use freely, though keep this message intact and
    /// report any bugs to me.  I also appreciate seeing extensions, or simply hearing that you're using
    /// these classes.  You may not copyright this work, though may use it in commercial/copyrighted works.
    /// Happy coding.
    /// Updated 29 March 2007.  Added a Reset() method.</remarks>
    public sealed class MovingAverageCalculator
        private readonly int _windowSize;
        private readonly float[] _values;
        private int _nextValueIndex;
        private float _sum;
        private int _valuesIn;

        /// <summary>
        /// Create a new moving average calculator.
        /// </summary>
        /// <param name="windowSize">The maximum number of values to be considered
        /// by this moving average calculation.</param>
        /// <exception cref="ArgumentOutOfRangeException">If windowSize less than one.</exception>
        public MovingAverageCalculator(int windowSize)
            if (windowSize < 1)
                throw new ArgumentOutOfRangeException("windowSize", windowSize, "Window size must be greater than zero.");

            _windowSize = windowSize;
            _values = new float[_windowSize];


        /// <summary>
        /// Updates the moving average with its next value, and returns the updated average value.
        /// When IsMature is true and NextValue is called, a previous value will 'fall out' of the
        /// moving average.
        /// </summary>
        /// <param name="nextValue">The next value to be considered within the moving average.</param>
        /// <returns>The updated moving average value.</returns>
        /// <exception cref="ArgumentOutOfRangeException">If nextValue is equal to float.NaN.</exception>
        public float NextValue(float nextValue)
            if (float.IsNaN(nextValue))
                throw new ArgumentOutOfRangeException("nextValue", "NaN may not be provided as the next value.  It would corrupt the state of the calculation.");

            // add new value to the sum
            _sum += nextValue;

            if (_valuesIn < _windowSize)
                // we haven't yet filled our window
                // remove oldest value from sum
                _sum -= _values[_nextValueIndex];

            // store the value
            _values[_nextValueIndex] = nextValue;

            // progress the next value index pointer
            if (_nextValueIndex==_windowSize)
                _nextValueIndex = 0;

            return _sum / _valuesIn;

        /// <summary>
        /// Gets a value indicating whether enough values have been provided to fill the
        /// speicified window size.  Values returned from NextValue may still be used prior
        /// to IsMature returning true, however such values are not subject to the intended
        /// smoothing effect of the moving average's window size.
        /// </summary>
        public bool IsMature
            get { return _valuesIn == _windowSize; }

        /// <summary>
        /// Clears any accumulated state and resets the calculator to its initial configuration.
        /// Calling this method is the equivalent of creating a new instance.
        /// </summary>
        public void Reset()
            _nextValueIndex = 0;
            _sum = 0;
            _valuesIn = 0;

Sorting System.Collections.Generic.List

Filed under: Development — adallow @ 11:27 am

To get the correct sorting on the .Sort method of the the Generic List, then objects that have been added to the list (e.g. of type <T>) should have implemented the IComparable interface and shold have implemented the CompareTo Method, such as:

class Person:IComparable { private string _FirstName; private int _Age; public Person(string _FirstName, int _Age) { this._FirstName = _FirstName; this._Age = _Age; } public string FirstName { get { return _FirstName; } set { _FirstName = value; } } public int Age { get { return _Age; } set { _Age = value; } } // sorting in ascending order public int CompareTo(object obj) { Person person = (Person) obj; return this.FirstName.CompareTo(person); } } However you can skip the need to implement the interface and CompareTo if you use a delegate on the call to the .Sort method as follows:
List<Person> persons = new List<Person>(); persons.Add( new Person("Tom",30) ); persons.Add(new Person("Harry", 55)); // sort in ascending order persons.Sort( delegate(Person person0, Person person1) { return person0.FirstName.CompareTo(person1.FirstName); } ); // sort in descending order persons.Sort( delegate(Person person0, Person person1) { return person1.FirstName.CompareTo(person0.FirstName); } ); From: http://devintelligence.com/blogs/netadventures/archive/2007/01/16/sorting-system-collections-generic-list-t.aspx

Improving .NET Application Performance and Scalability

Filed under: Development,Enterprise & Application Architecture — adallow @ 6:00 am
Tags: ,

from JD Meir, check out the following link for some really useful tips: http://msdn.microsoft.com/en-us/library/ms998530.aspx

Summary from the link: This guide provides end-to-end guidance for managing performance and scalability throughout your application life cycle to reduce risk and lower total cost of ownership. It provides a framework that organizes performance into a handful of prioritized categories where your choices heavily impact performance and scalability success. The logical units of the framework help integrate performance throughout your application life cycle. Information is segmented by roles, including architects, developers, testers, and administrators, to make it more relevant and actionable. This guide provides processes and actionable steps for modeling performance, measuring, testing, and tuning your applications. Expert guidance is also provided for improving the performance of managed code, ASP.NET, Enterprise Services, Web services, remoting, ADO.NET, XML, and SQL Server.

reCAPTCHA: a freely available CAPTCHA implementation.

Filed under: Development — adallow @ 5:54 am

From the website: http://recaptcha.net/

A CAPTCHA is a program that can tell whether its user is a human or a computer. You’ve probably seen them — colorful images with distorted text at the bottom of Web registration forms. CAPTCHAs are used by many websites to prevent abuse from “bots,” or automated programs usually written to generate spam. No computer program can read distorted text as well as humans can, so bots cannot navigate sites protected by CAPTCHAs.

  • A registration page for a forum or wiki.
  • A comment form.
  • Hiding information that could be useful to spammers (we recommend the reCAPTCHA Mailhide API for this).

September 30, 2008

Linq to Sharepoint

Filed under: Development — adallow @ 12:12 pm
Tags: , ,

David Riddell McGhee, enterprise Account Technology Specialist for Microsoft Australia, has a really interesting blog, full of interesting things. Most recently i was reading about LinqToSharepoint.

This could be really useful for us as sharepoint is essentially our internal ECM system so integrating with it will become more common for us. From his blog:

Codeplex project:


Link I found this from:



How Do I: Use LINQ To SharePoint to Retrieve SharePoint Data?

Blog at WordPress.com.