Freitag, 22. Oktober 2010

Replace Text in Streams

Today, I thought about replacing text in streams. I noticed that .NET 4 provides methods for type

String
to replace some text, but no methods for type
Stream
I found a way by converting the stream to a string in a first step and by replacing text in a second step.
new StreamReader(i).ReadToEnd().Replace("old text", "new text")

For a lot of cases this is alright and there is nothing to worry about. But if the source is a huge amount of data and I do not want to hold it in memory all the time, things get tricky. For these cases I wrote a simple method called

ReplaceTextInStream

The method gets four arguments: an input stream, an output stream, a string with a pattern to search for and a string with the replacement value. If one argument is null, a ArgumentNullException is thrown. Here is the code: Enjoy!

public static void ReplaceTextInStream(Stream input, Stream output, string pattern, string replacement) 
{
    if (input == null)
    {
        throw new ArgumentNullException("input");
    }
    if (output == null)
    {
        throw new ArgumentNullException("output");
    }
    if (pattern == null)
    {
        throw new ArgumentNullException("pattern");
    }
    if (replacement == null)
    {
        throw new ArgumentNullException("replacement");
    }
    StreamReader r = new StreamReader(input);
    StreamWriter w = new StreamWriter(output);
    // Input and Pattern are empty => Output = Replacement
    if(input.Length == 0 && pattern.Length == 0)
    {
        w.Write(replacement);
    }
    // Pattern is empty => Output = Input
    if (input.Length != 0 && pattern.Length == 0)
    {
        while (!r.EndOfStream)
        {
            w.Write((char)r.Read());
        }
    }
    // Init empty Stack
    List<char> stack = new List<char>();
    // Loop over input characters
    while (!r.EndOfStream)
    {
        // Read next input character
        char c = (char)r.Read();
        // Compare characters
        if (pattern[stack.Count] == c)
        {
            // Match => Put it on Stack
            stack.Add(c);
            // Match complete?
            if (stack.Count == pattern.Length)
            {
                // Write Replacement and clear Stack
                w.Write(replacement);
                stack.Clear();
            }
        }
        else
        {
            // No Macth => Stack filled?
            if (stack.Count != 0)
            {
                // Write and Clear Stack
                foreach (var tc in stack)
                {
                    w.Write(tc);
                }
                stack.Clear();
            }
            // Copy current character
            w.Write(c);
        }
    }
    // After Loop: Stack filled?
    if (stack.Count != 0)
    {
        // Write and Clear Stack
        foreach (var tc in stack)
        {
            w.Write(tc);
        }
        stack.Clear();
    }
    w.Flush();
}

Useful .NET Tools

Here is a list of tools I use for .NET development. Each of them saves me a lot of time.

JetBrains ReSharper
Resharper is an extension by Jetbrain for Visual Studio, which improves the development experience. ReSharper executes solution-wide static code analysis, error detection on-the-fly, without the need to compile, provides additional features for error correction, code completion, navigation, search, refactorings, syntax highlighting, formatting, code generation and optimization among other features.
Download

redgate .NET Reflector
.NET Reflector is a class browser and analysis tool for .NET. It allows you to navigate, search, disassemble and analyze .NET components.
Download

WinMerge
WinMerge is a powerful differencing and merging tool. It’s Open Source. WinMerge can compare folders and files, presenting differences in a visual text format that is easy to understand and handle.
Download

Instant SQL Formatter
Online tool for formatting SQL statements. Supported database types are MSSQL, Oracle, DB2, MySQL and others
Link

C# Regular Expression Helper
Litte tool for testing regular expressions
Download

XPath Tester
Little tool for testing XPath statements
Download

Freitag, 8. Oktober 2010

Resumed Orchestration Instances are not processed

Today, in BizTalk 2009 I runned into the following problem: I resumed orchestration instances. They get a processing server, but they are not processed. The orchestration debugger showed, that nothing has happened. Restarting host instance does not change anyhting. Running the MsgBoxViewer showed a MsgBox database integrity problem. The text said

Missing Restart Msg in Spool table (can prevent some svc instances to be resumed, terminated or suspended) !!

Missing Suspend Msg in Spool table (can prevent some svc instances to be resumed, terminated or suspended) !!

Missing Terminate Msg in Spool table (can prevent some svc instances to be resumed, terminated or suspended) !!

Missing ResumeInDebugMode Msg in Spool table (can prevent some svc instances to be resumed, terminated or suspended) !!

Following the hint to run the Terminator tool is not necessary. Just do THIS. Afterwards, restart the host instances. All running, but not processed orchestration instances are processed.

Sonntag, 3. Oktober 2010

BizTalk Server 2010 released

Microsoft has just released a new version of BizTalk Server. Since 1st of October BizTalk Server 2010 is available. For a brief overview of the new features have a look at the MSDN BizTalk Server Team Blog.

Links:
Download BizTalk Server 2010 - Developer Edition
Download BizTalk Server 2010 - Evaluation Edition
MSDN Library on BizTalk Server 2010

Dienstag, 4. Mai 2010

BizTalk 2009 Light and Easy Webcast Series

I just found a series of webcasts on MSDN focussing on BizTalk Server 2009 features created by worldwide MVPs and industry experts. They are great stuff.
http://code.msdn.microsoft.com/bts09

Microsoft Knowledge Base Monitoring

Problems with BizTalk are more or less time consuming. Usually, it is important to quickly find a solution. For a particular problem you have a good chance to find a useful article or hotfix in the Microsoft Knowledge Base. For that these two links can help (not only BizTalk):
  • http://kbupdate.info/ is a monitoring system that scans the entire Microsoft Knowledge Base every night
  • http://kbalertz.com/ is an e-mail notification system that scans the entire Microsoft Knowledge Base every night, and e-mails you when updates or additions are made to the technologies you subscribe to