Category Archives: .NET

Trimming .NET 4.0

Here is two string extension methods, one to handle the change of functionality in the Trim() method from .NET 3.5 to 4.0. The other one removes null characters from a string.

 

public static class Extensions
    {
        //
http://www.blogfor.net/2010/08/01/string-trim-treats-unicode-whitespace-differently-in-net-4/
        public static string TrimWithUnicodeWhitespace(this string stringToTrim)
        {
            return stringToTrim.Trim().Trim(new char[] { ‘\uFEFF’, ‘\u200B’ });
        }

        public static string TrimWithNullChar(this string stringToTrim)
        {
            char nullChar = Convert.ToChar(0x0);
            int i = stringToTrim.IndexOf(nullChar);
            if (i > 0)
            {
                stringToTrim = stringToTrim.Substring(0, i);
            }
            return stringToTrim.Trim();
        }
    }

System.DirectoryServices slow after 15 seconds

Symptoms: When using .NET Framework System.DirectoryServices hosted in an WCF or Web Services the first LDAP query is really slow but the following are fast. Then, after beeing idle for 15 seconds or more the following LDAP query is really slow again. The queries work, the only problem is the initial overhead when creating a connection to the LDAP server. And after 15 seconds the WCF thread pool recycles.

Cause: The issue could be caused by a mismatch in server certificate if your not calling from the same domain.

Resolution: Use System.DirectoryServices.Protocol instead in order to ignore certificate errors and turn off ReferralChasing.

Code example:

Continue reading

TimeSpan formatting as extension method C#

Here’s a simple extenstion method to format a TimeSpan object as total number of hours and minutes (HH:MM) wich supports total hours above 24.

namespace YourProject.ExtensionMethods
{
    public static class Extensions
    {
        public static string ToHHMMString(this TimeSpan span)
        {
            int totalHours = (int) span.TotalHours; //truncate decimals
            return totalHours.ToString(“00”) + “:” + span.Minutes.ToString(“00”);
        }
    }  
}

CRM 2011: Fetch entity ID in process/workflow

Let’s say you need to fetch the GUID of an entity record in a process in order to build a link in an e-mail. The default process wizard does’nt give you this information. So you need to build your own custom workflow activitylibrary in Visual Studio.

Here’s the basic how to: http://msdn.microsoft.com/en-us/library/gg328515.aspx

And here’s the code for extracting the ID:

Continue reading

A simple class for EventLog writing in C# .NET

A simple class with a method for creating your own entries to the operating system’s event log by using the Microsoft .NET Framework. More info http://support.microsoft.com/kb/307024.

using System;
using System.Text;
using System.Diagnostics;

namespace Logs //your namespace here
{
    public class OSEventLog
    {
        public static void Write(string message, EventLogEntryType type)
        {
            string source = “SYSteam.NCA.CRM.Mesh.Import”;
            string log = “Application”;
           
            if (!EventLog.SourceExists(source))
                EventLog.CreateEventSource(source,log);
                       
            EventLog.WriteEntry(source, message, type);
        }
    }
}