Wednesday, March 28, 2007

Getting correct object type

Following code snippet demonstrates how can we use GetType function to get correct object type in inheritance hierarchy

public interface A
{
void print();

}

public class B : A
{
void A.print()
{
Console.WriteLine("B");
}
}

public class C : A
{
void A.print()
{
Console.WriteLine("C");
}
}


public class MyClass
{
public static void Main()
{
A a = new C();
Console.WriteLine("#: " + a.GetType().Name);
a.print();
RL();
}

}

Tuesday, March 27, 2007

SMTP Managed Event Sink for scanning incoming emails

I want to scan all incoming mails using Managed Event sink. I am implementing IMailTransportSubmission Interface.

void IMailTransportSubmission.OnMessageSubmission(
MailMsg message,
IMailTransportNotify notify,
IntPtr context)
{
try
{
// Fill details of the Message in database
FillData(message);
}
catch (Exception ex)
{
EventLog.WriteEntry(
Assembly.GetExecutingAssembly().FullName,
ex.Message + "\n" + ex.StackTrace.ToString(),
EventLogEntryType.Error);
}
finally
{
if (null != message)
Marshal.ReleaseComObject(message);

}
}


I am using following command to bind the event sink:

cscript smtpreg.vbs /add 1 OnTransportSubmission "MyEventSink.ManagedSink" MyEventSink.ManagedSink.Sink "MAIL FROM=*" 28001

I have a development exchange server. When I put a message in “pickup” directory, a entry for this message is inserted into database. That means my event sink working fine, but I am not sure how to test it for incoming message as this development exchange doesn’t have dns entries and I have to check event sink functionality for incoming mail from same development server somehow. First thing I want to know that whether I have implemented correct interface for scanning incoming mails. Two, whether I have registered correct event for incoming mails.

Tuesday, March 06, 2007

SharePoint Versioning Algorithm

In my current project I need to implement versioning of the document stored in the SQL Server in the same way as SharePoint. I wrote following C# code to simulate versioning of SharePoint:

    int majorVersion = 0;
int maxMajorVersion = 2;
int maxMinorVersion = 20; // default 511;
string version = string.Empty;
int counter = 1;
int id = 1;

int maxId = 512 * maxMajorVersion;

List<int> lstNxtVersionIds = new List<int>();
for (int i = 1; i <= maxMajorVersion; i++)
lstNxtVersionIds.Add(i*512);

for (int i = 1; i<=maxId & id<=maxId; i++)
{
if (counter == maxMinorVersion)
{
version = majorVersion.ToString() + "." + counter.ToString();
Console.WriteLine ("id {0}, version {1}", id, version);
if (majorVersion < maxMajorVersion)
{
majorVersion++;

foreach (int nxtVersionId in lstNxtVersionIds)
{
if (id < nxtVersionId)
{
id = nxtVersionId;
break;
}
}
}
else if (majorVersion == maxMajorVersion)
break;
counter = 0;
}
version = majorVersion.ToString() + "." + counter.ToString();
Console.WriteLine ("id {0}, version {1}", id, version);

id++;
counter++;