Friday, January 27, 2012

Removing Strong Name from .NET assembly

First in 10+ years of .net practice I had to solve removing the strong name from the assembly and recompiling it after the export with Reflector.

The need for that was caused by customer’s extra security/audit requirements which could not be satisfied through a standard extensibility in MS CRM 3.0. And let us say MS CRM 3.0 is a very old lady now, so one can hardly expect year 2005 code to meet year 2012 security standards.

I found two solutions over the web:

http://blog.fredrikhaglund.se/blog/2009/02/06/how-to-patchfixhack-someone-elses-assembly/

http://www.codeproject.com/Articles/15374/Removing-Strong-Signing-from-assemblies-at-file-le

I was happy I didn’t have to resort to a second one – quite heavier weight for what I needed. First one worked as a charm.

By the way  I tried side by side latest .NET reflector from Red Gate with the last not expired version of original Reflector and should say that I didn’t notice any value added in Red Gate’s version. Nothing to advocate a sudden price tag and 1+ year of ownership, SHAME!

As for decompile and recompile of a CRM assembly in question, just few issues I ran into:

Compiler limit exceeded: Line cannot exceed 2046 characters (CS1034) - C:\CRM\PROJECTS\components.core\Microsoft\Crm\SystemCustomization\Security.cs:31,2046

That was solved easily with splitting the line into several instructions.

Microsoft.Crm.CalendarUtil:

Operator '>=' cannot be applied to operands of type 'System.DayOfWeek' and 'int' (CS0019) - C:\CRM\PROJECTS\components.core\Microsoft\Crm\CalendarUtil.cs:157,17

Corrected version of a method would look like (trick is in casting the enums to int):

public static DateTime GetFirstDayInCalendar(DateTime date)
{
    date = new DateTime(date.Year, date.Month, 1, 0, 0, 0);
    int weekStartDayCode = OrganizationSettings.Current.WeekStartDayCode;
    if ((int)date.DayOfWeek >= weekStartDayCode)
    {
        return date.AddDays((double) (((int)date.DayOfWeek - weekStartDayCode) * ~(int)DayOfWeek.Sunday));
    }
    return date.AddDays((double) ((((int)date.DayOfWeek + 7) - weekStartDayCode) * ~(int)DayOfWeek.Sunday));
}

And again, always something to learn! I was really puzzled with that tilda operator in front of DayOfWeek.Sunday so was glad to learn about it: http://stackoverflow.com/questions/387424/what-is-the-tilde-in-a-c-sharp-enumeration

This is it. Hope my positive experience will ease your decisions once you stay in front of a similar task!

No comments: