dinsdag 18 januari 2011

GAC assemblies with right-click

As a developer you'll often need to place an assembly into the GAC (Global Assemblie Cache).
There are several ways to do this. You can do it in command line with "gacutil -if 'assembly path'" or by dragging your assembly in explorer to "C:\Windows\assembly".

Well there is another even easier way to do this. By just adding a registry key, you can place an assembly into the GAC with a simple right-click, as you can see below.

br /> To achieve this you'll need to create the registry key.
This can be done by copying following code to notepad, and save it with the .reg extension. Now double-click the created .reg file to add the registry key.
That's it!

You can also change the shown text when you right-click by changing the 'GAC-It' (written in red) to the desired text.

*****************************************************************
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\dllfile\shell\GAC-It\command]
@="C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\NETFX 4.0 Tools\\gacutil.exe /if \"%1\""
*****************************************************************

By specifying the '/if', I choose to install an assembly into the global assembly cache. If an assembly with the same name already exists in the global assembly cache, the tool overwrites it.
(other possible parameters can be found here).

ATTENTION: the path to the gacutil.exe may be different on your pc. I've installed it on a 64-bit machine. you should verify the actual path of your gacutil.exe (may occur multiple times on your pc).

Source: http://biztalkia.blogspot.com/2005/12/easier-way-to-add-dll-to-gac.html

vrijdag 7 januari 2011

Inserting / updating SQL from C#

Creating an SQL string in C# is a poor way to do it. It might give problems with some special characters, even the data retrieved from SQL to C# might not be the exact same anymore.

You really want to bind your parameters. It prevents SQL attacks, and makes the query run faster.

Here's a simple piece of code as an example:


   1:  // we'll assume there's a connection set up in the instance
   2:   
   3:  // pass a value
   4:  public void updateLog(string msg) {
   5:   // create your command, not the @msg where the parameter will go
   6:   SqlCommand cmd = 
   7:                new SqlCommand("insert into log(msg) values (@msg)", this.conn);
   8:   
   9:   // bind the value to the parameter reference
  10:   cmd.Parameters.AddWithValue("@msg", msg);
  11:   
  12:   // fire
  13:   try {
  14:    cmd.Connection.Open();
  15:    cmd.ExecuteNonQuery();
  16:   } finally {
  17:    cmd.Connection.Close();
  18:   }
  19:  }

Converting DatetTime from and to Custom String format

Converting from String to DateTime

Using Convert.ToDateTime method might give you the error 'String was not recognized as a valid DateTime'.
Following code for example will result in an error:

DateTime dt = Convert.ToDateTime("20100228");

You can simply do the conversion by defining the dateTime format yourself as follows:

DateTime dt = DateTime.ParseExact("20100228", "yyyyMMdd", new System.Globalization.DateTimeFormatInfo())

Converting from DateTime to String

To convert a DateTime to String is even easier, just use following code:

dt.ToString("yyyy-MM-dd HH:mm:ss.ss");



NOTE:

for an overview of all possible DateTime formats, I'll refer to http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

donderdag 6 januari 2011

Not able to add a BizTalk Project in Visual Studio 2008

It's possible that you're not able to add a biztalk project in your visual studio 2008.
The likely cause is a new visual studio 2008 installation, or it's updated in any way.

The solution to this problem is quite simple.You could just do a repair of your BizTalk with the setup, this should do the trick.

Or another way to accomplish this is by changing the registry key [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Projects\{FAE04EC0-301F-11d3-BF4B-00C04F79EFBC}] from ‘csproj’ to ‘csproj;btproj’.
For a 64 bit version of visual studio this would be as follows:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\Projects\{FAE04EC0-301F-11d3-BF4B-00C04F79EFBC}
Somehow this key gets reset every time the Visual Studio installation is changed...

RESOURCES:
http://ronaldlokers.blogspot.com/2010/12/visual-studio-2008-doesnt-know-biztalk.html
http://blogs.msdn.com/b/biztalkcrt/archive/2009/08/21/visual-studio-2008-fails-to-create-open-biztalk-projects.aspx

The BizTalk Orchestration Debugger

I found a neat blog on the use of the BizTalk Orchestration Debugger.
The blog provides a very extended guide to use the Orchestration Debugger.
I even discovered some capabilities that I wasn't aware of.

link: http://biztalkthoughts.blogspot.com/2010/12/orchestration-debugger.html

dinsdag 4 januari 2011

Compress and Decompress Strings with C#

To reduce the data size in our SQL tables we compress the full received message for all messages older than 90 days.
To accomplish this I use a compression and a decompression function in C#.

I've come up with 2 different solutions.
One will use the System.IO.Compression namespace, while the other uses an external referenced library, namely SharpZipLib.

First I'll show you how you can do this by just using the build in .NET functions.
the function to decompress the String looks like this:


    public static string Zip(string value)
    {
        //Transform string into byte[]
        byte[] byteArray = new byte[value.Length];
        int indexBA = 0;
        foreach (char item in value.ToCharArray())
        {
            byteArray[indexBA++] = (byte)item;
        }

        //Prepare for compress
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.IO.Compression.GZipStream sw = new         System.IO.Compression.GZipStream(ms,
        System.IO.Compression.CompressionMode.Compress);

        //Compress
        sw.Write(byteArray, 0, byteArray.Length);
        //Close, DO NOT FLUSH cause bytes will go missing...
        sw.Close();

        //Transform byte[] zip data to string
        byteArray = ms.ToArray();
        System.Text.StringBuilder sB = new         System.Text.StringBuilder(byteArray.Length);
        foreach (byte item in byteArray)
        {
            sB.Append((char)item);
        }
        ms.Close();
        sw.Dispose();
        ms.Dispose();

        return sB.ToString();
    }

The Decompression will be done by following funtion:


    public static string DecompressData(string sData)
    {
        byte[] byteArray = new byte[sData.Length];

        int indexBa = 0;
        foreach (char item in sData)
            byteArray[indexBa++] = (byte)item;

        MemoryStream memoryStream = new MemoryStream(byteArray);
        GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);

        byteArray = new byte[1024];

        StringBuilder stringBuilder = new StringBuilder();

        ;int readBytes;
        while ((readBytes = gZipStream.Read(byteArray, 0,byteArray.Length)) != 0)
        {
           for (int i = 0; i < readBytes; i++)             stringBuilder.Append((char)byteArray[i]);             }         gZipStream.Close();         memoryStream.Close();         gZipStream.Dispose();         memoryStream.Dispose();          return stringBuilder.ToString();     }


Another way to solve this is by using the SharpZipLib Library, which can be found here.
This will result in following methods:


    private static string Compress(string strInput)
    {
    try
    {
    byte[] bytData = System.Text.Encoding.UTF8.GetBytes(strInput);
    MemoryStream ms = new MemoryStream();
    Stream s = new DeflaterOutputStream(ms);
    s.Write(bytData, 0, bytData.Length);
    s.Close();
    byte[] compressedData = (byte[])ms.ToArray();
    return ConvertByteToString(compressedData);
    }
    catch (Exception e)
    {
    Console.WriteLine(e.ToString());
    return null;
    }
    }
    
    private static string DeCompress(string strInput)
    {
    byte[] bytInput = ConvertStringToByte(strInput);
    string strResult = "";
    int totalLength = 0;
    byte[] writeData = new byte[4096];
    Stream s2 = new InflaterInputStream(new MemoryStream(bytInput));
    
    try
    {
    while (true)
    {
    int size = s2.Read(writeData, 0, writeData.Length);
    if (size > 0)
    {
    totalLength += size;
    strResult += System.Text.Encoding.ASCII.GetString(writeData, 0,
    size);
    }
    else
    {
    break;
    }
    }
    s2.Close();
    return strResult;
    }
    catch (Exception e)
    {
    Console.WriteLine(e.ToString());
    return null;
    }
    }
    
    private static byte[] ConvertStringToByte(string strInput)
    {
        byte[] byteArray = new byte[strInput.Length];
    
        int indexBa = 0;
        foreach (char item in strInput)
            byteArray[indexBa++] = (byte)item;
        return byteArray;
    }
    private static string ConvertByteToString(byte[] compressedData)
    {
        System.Text.StringBuilder sB = new System.Text.StringBuilder(compressedData.Length);
        foreach (byte item in compressedData)
        {
            sB.Append((char)item);
        }
        return sB.ToString();
    }


Conclusion:
The second method, with the SharpZipLib library, results in a better compression than the first solution. So by using SharpZipLib you'll get the lowest data size for the resulting compressed string.

Microsoft SQL Server 2005 SP4 is released

Service Pack 4 (SP4) for Microsoft SQL Server 2005 is now available for download. SQL Server 2005 service packs are cumulative, and this service pack upgrades all service levels of SQL Server 2005 to SP4 . You can use these packages to upgrade any of the following SQL Server 2005 editions:
- Enterprise
- Enterprise Evaluation
- Developer
- Standard
- Workgroup

SQL Server 2005 SP4 includes SQL Server 2005 SP3 cumulative update 1 to 11, customer requested fixes, along with instances of the SQL Server 2005 SP4 database Engine support for DAC operations.

link: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b953e84f-9307-405e-bceb-47bd345baece&displaylang=en
link for the express edition: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=26435597-b28e-4568-9d16-017bdf47abdc&displaylang=en

There is also a SP4 released for the SQL 2005 feature pack.
The Feature Pack is a collection of standalone install packages that provide additional value for SQL Server 2005. It includes:
- Latest versions of redistributable components for SQL Server 2005
- Latest versions of add-on providers for SQL Server 2005
- Latest versions of backward compatibility components for SQL Server 2005

link: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=7768393b-71fa-4281-83eb-cab08be4fb76&displaylang=en