torsdag den 15. oktober 2009

How to Binary serialize a MailMessage for use with MessageQueue

The MailMessage object is in itself not serializeable.
The MessageQueue needs a serializeable object in order to work so as is the MailMessage is no target for the MessageQueue.

This has bothered me for a long time but finally the right project arrived with the need of Queuing MailMessages.

Almost all examples on the internet describes how to XML serialize the MailMessage but most of the times only the simple types are serialized; like To, From, Subject and Body.
What about AlternateViews and Attachments? This has been left for your imagination.

I decided to start my own SerializeableMailMessage class that should be Binary seriablizeable. Its only hard work and 500 lines of code is the result of this effort.

There is no tricky stuff in it. It is only hard work figuring out how to serialize each object in the MailMessage object.

You use it by creating a new SerializeableMailMessage() feeding it with a MailMessage object. GetMailMessage() on the SeriablizeableMailMessage object gives you the MailMessage object back.

 using System;  
 using System.Collections.Generic;  
 using System.Text;  
 using System.Text.RegularExpressions;  
 using System.Net;  
 using System.Net.Mail;  
 using System.IO;  
 using System.Net.Mime;  
 using System.Collections.Specialized;  
 namespace Netmester.Mail.Serialization  
 {  
      [Serializable]  
      internal class SerializeableLinkedResource  
      {  
           String ContentId;  
           Uri ContentLink;  
           Stream ContentStream;  
           SerializeableContentType ContentType;  
           TransferEncoding TransferEncoding;  
           internal static SerializeableLinkedResource GetSerializeableLinkedResource(LinkedResource lr)  
           {  
                if (lr == null)  
                     return null;  
                SerializeableLinkedResource slr = new SerializeableLinkedResource();  
                slr.ContentId = lr.ContentId;  
                slr.ContentLink = lr.ContentLink;  
                if (lr.ContentStream != null)  
                {  
                     byte[] bytes = new byte[lr.ContentStream.Length];  
         lr.ContentStream.Position = 0;  
                     lr.ContentStream.Read(bytes, 0, bytes.Length);  
                     slr.ContentStream = new MemoryStream(bytes);  
                }  
                slr.ContentType = SerializeableContentType.GetSerializeableContentType(lr.ContentType);  
                slr.TransferEncoding = lr.TransferEncoding;  
                return slr;  
           }  
           internal LinkedResource GetLinkedResource()  
           {  
                LinkedResource slr = new LinkedResource(ContentStream);  
                slr.ContentId = ContentId;  
                slr.ContentLink = ContentLink;  
                slr.ContentType = ContentType.GetContentType();  
                slr.TransferEncoding = TransferEncoding;  
                return slr;  
           }  
      }  
      [Serializable]  
      internal class SerializeableAlternateView  
      {  
           Uri BaseUri;  
           String ContentId;  
           Stream ContentStream;  
           SerializeableContentType ContentType;  
           List<SerializeableLinkedResource> LinkedResources = new List<SerializeableLinkedResource>();  
           TransferEncoding TransferEncoding;  
           internal static SerializeableAlternateView GetSerializeableAlternateView(AlternateView av)  
           {  
                if (av == null)  
                     return null;  
                SerializeableAlternateView sav = new SerializeableAlternateView();  
                sav.BaseUri = av.BaseUri;  
                sav.ContentId = av.ContentId;  
                if (av.ContentStream != null)  
                {  
                     byte[] bytes = new byte[av.ContentStream.Length];  
         // Reset read position  
         av.ContentStream.Position = 0;  
         av.ContentStream.Read(bytes, 0, bytes.Length);  
         sav.ContentStream = new MemoryStream(bytes);  
                }  
                sav.ContentType = SerializeableContentType.GetSerializeableContentType(av.ContentType);  
                foreach (LinkedResource lr in av.LinkedResources)  
                     sav.LinkedResources.Add(SerializeableLinkedResource.GetSerializeableLinkedResource(lr));  
                sav.TransferEncoding = av.TransferEncoding;  
                return sav;  
           }  
           internal AlternateView GetAlternateView()  
           {  
                AlternateView sav = new AlternateView(ContentStream);  
                sav.BaseUri = BaseUri;  
                sav.ContentId = ContentId;  
                sav.ContentType = ContentType.GetContentType();  
                foreach (SerializeableLinkedResource lr in LinkedResources)  
                     sav.LinkedResources.Add(lr.GetLinkedResource());  
                sav.TransferEncoding = TransferEncoding;  
                return sav;  
           }  
      }  
      [Serializable]  
      internal class SerializeableMailAddress  
      {  
           String User;  
           String Host;  
           String Address;  
           String DisplayName;  
           internal static SerializeableMailAddress GetSerializeableMailAddress(MailAddress ma)  
           {  
                if (ma == null)  
                     return null;  
                SerializeableMailAddress sma = new SerializeableMailAddress();  
                sma.User = ma.User;  
                sma.Host = ma.Host;  
                sma.Address = ma.Address;  
                sma.DisplayName = ma.DisplayName;  
                return sma;  
           }  
           internal MailAddress GetMailAddress()  
           {  
                return new MailAddress(Address, DisplayName);  
           }  
      }  
      [Serializable]  
      internal class SerializeableContentDisposition  
      {  
           DateTime CreationDate;  
           String DispositionType;  
           String FileName;  
           Boolean Inline;  
           DateTime ModificationDate;  
           SerializeableCollection Parameters;  
           DateTime ReadDate;  
           long Size;  
           internal static SerializeableContentDisposition GetSerializeableContentDisposition(System.Net.Mime.ContentDisposition cd)  
           {  
                if (cd == null)  
                     return null;  
                SerializeableContentDisposition scd = new SerializeableContentDisposition();  
                scd.CreationDate = cd.CreationDate;  
                scd.DispositionType = cd.DispositionType;  
                scd.FileName = cd.FileName;  
                scd.Inline = cd.Inline;  
                scd.ModificationDate = cd.ModificationDate;  
                scd.Parameters = SerializeableCollection.GetSerializeableCollection(cd.Parameters);  
                scd.ReadDate = cd.ReadDate;  
                scd.Size = cd.Size;  
                return scd;  
           }  
           internal void SetContentDisposition(ContentDisposition scd)  
           {  
                scd.CreationDate = CreationDate;  
                scd.DispositionType = DispositionType;  
                scd.FileName = FileName;  
                scd.Inline = Inline;  
                scd.ModificationDate = ModificationDate;  
                Parameters.SetColletion(scd.Parameters);  
                scd.ReadDate = ReadDate;  
                scd.Size = Size;  
           }  
      }  
      [Serializable]  
      internal class SerializeableContentType  
      {  
           String Boundary;  
           String CharSet;  
           String MediaType;  
           String Name;  
           SerializeableCollection Parameters;  
           internal static SerializeableContentType GetSerializeableContentType(System.Net.Mime.ContentType ct)  
           {  
                if (ct == null)  
                     return null;  
                SerializeableContentType sct = new SerializeableContentType();  
                sct.Boundary = ct.Boundary;  
                sct.CharSet = ct.CharSet;  
                sct.MediaType = ct.MediaType;  
                sct.Name = ct.Name;  
                sct.Parameters = SerializeableCollection.GetSerializeableCollection(ct.Parameters);  
                return sct;  
           }  
           internal ContentType GetContentType()  
           {  
                ContentType sct = new ContentType();  
                sct.Boundary = Boundary;  
                sct.CharSet = CharSet;  
                sct.MediaType = MediaType;  
                sct.Name = Name;  
                Parameters.SetColletion(sct.Parameters);  
                return sct;  
           }  
      }  
      [Serializable]  
      internal class SerializeableAttachment  
      {  
           String ContentId;  
           SerializeableContentDisposition ContentDisposition;  
           SerializeableContentType ContentType;  
           Stream ContentStream;  
           System.Net.Mime.TransferEncoding TransferEncoding;  
           String Name;  
           Encoding NameEncoding;  
           internal static SerializeableAttachment GetSerializeableAttachment(Attachment att)  
           {  
                if (att == null)  
                     return null;  
                SerializeableAttachment saa = new SerializeableAttachment();  
                saa.ContentId = att.ContentId;  
                saa.ContentDisposition = SerializeableContentDisposition.GetSerializeableContentDisposition(att.ContentDisposition);  
                if (att.ContentStream != null)  
                {  
                     byte[] bytes = new byte[att.ContentStream.Length];  
                  att.ContentStream.Position = 0;   
         att.ContentStream.Read(bytes, 0, bytes.Length);  
                     saa.ContentStream = new MemoryStream(bytes);  
                }  
                saa.ContentType = SerializeableContentType.GetSerializeableContentType(att.ContentType);  
                saa.Name = att.Name;  
                saa.TransferEncoding = att.TransferEncoding;  
                saa.NameEncoding = att.NameEncoding;  
                return saa;  
           }  
           internal Attachment GetAttachment()  
           {  
                Attachment saa = new Attachment(ContentStream, Name);  
                saa.ContentId = ContentId;  
                this.ContentDisposition.SetContentDisposition(saa.ContentDisposition);  
                saa.ContentType = ContentType.GetContentType();  
                saa.Name = Name;  
                saa.TransferEncoding = TransferEncoding;  
                saa.NameEncoding = NameEncoding;  
                return saa;  
           }  
      }  
      [Serializable]  
      internal class SerializeableCollection  
      {  
           Dictionary<String, String> Collection = new Dictionary<string, string>();  
           internal SerializeableCollection()  
           {  
           }  
           internal static SerializeableCollection GetSerializeableCollection(NameValueCollection col)  
           {  
                if (col == null)  
                     return null;  
                SerializeableCollection scol = new SerializeableCollection();  
                foreach (String key in col.Keys)  
                     scol.Collection.Add(key, col[key]);  
                return scol;  
           }  
           internal static SerializeableCollection GetSerializeableCollection(StringDictionary col)  
           {  
                if (col == null)  
                     return null;  
                SerializeableCollection scol = new SerializeableCollection();  
                foreach (String key in col.Keys)  
                     scol.Collection.Add(key, col[key]);  
                return scol;  
           }  
           internal void SetColletion(NameValueCollection scol)  
           {  
                foreach (String key in Collection.Keys)  
                {  
                     scol.Add(key, this.Collection[key]);  
                }  
           }  
           internal void SetColletion(StringDictionary scol)  
           {  
                foreach (String key in Collection.Keys)  
                {  
                     if (scol.ContainsKey(key))  
                          scol[key] = Collection[key];  
                     else  
                          scol.Add(key, this.Collection[key]);  
                }  
           }  
      }  
      /// <summary>  
      /// Serializeable mailmessage object  
      /// </summary>  
      [Serializable]  
      public class SerializeableMailMessage  
      {  
           Boolean IsBodyHtml { get; set; }  
           String Body { get; set; }  
           SerializeableMailAddress From { get; set; }  
           List<SerializeableMailAddress> To = new List<SerializeableMailAddress>();  
           List<SerializeableMailAddress> CC = new List<SerializeableMailAddress>();  
           List<SerializeableMailAddress> Bcc = new List<SerializeableMailAddress>();  
           SerializeableMailAddress ReplyTo { get; set; }  
           SerializeableMailAddress Sender { get; set; }  
           String Subject { get; set; }  
           List<SerializeableAttachment> Attachments = new List<SerializeableAttachment>();  
           Encoding BodyEncoding;  
           Encoding SubjectEncoding;  
           DeliveryNotificationOptions DeliveryNotificationOptions;  
           SerializeableCollection Headers;  
           MailPriority Priority;  
           List<SerializeableAlternateView> AlternateViews = new List<SerializeableAlternateView>();  
           /// <summary>  
           /// Creates a new serializeable mailmessage based on a MailMessage object  
           /// </summary>  
           /// <param name="mm"></param>  
           public SerializeableMailMessage(MailMessage mm)  
           {  
                IsBodyHtml = mm.IsBodyHtml;  
                Body = mm.Body;  
                Subject = mm.Subject;  
                From = SerializeableMailAddress.GetSerializeableMailAddress(mm.From);  
                To = new List<SerializeableMailAddress>();  
                foreach (MailAddress ma in mm.To)  
                {  
                     To.Add(SerializeableMailAddress.GetSerializeableMailAddress(ma));  
                }  
                CC = new List<SerializeableMailAddress>();  
                foreach (MailAddress ma in mm.CC)  
                {  
                     CC.Add(SerializeableMailAddress.GetSerializeableMailAddress(ma));  
                }  
                Bcc = new List<SerializeableMailAddress>();  
                foreach (MailAddress ma in mm.Bcc)  
                {  
                     Bcc.Add(SerializeableMailAddress.GetSerializeableMailAddress(ma));  
                }  
                Attachments = new List<SerializeableAttachment>();  
                foreach (Attachment att in mm.Attachments)  
                {  
                     Attachments.Add(SerializeableAttachment.GetSerializeableAttachment(att));  
                }  
                BodyEncoding = mm.BodyEncoding;  
                DeliveryNotificationOptions = mm.DeliveryNotificationOptions;  
                Headers = SerializeableCollection.GetSerializeableCollection(mm.Headers);  
                Priority = mm.Priority;  
                ReplyTo = SerializeableMailAddress.GetSerializeableMailAddress(mm.ReplyTo);  
                Sender = SerializeableMailAddress.GetSerializeableMailAddress(mm.Sender);  
                SubjectEncoding = mm.SubjectEncoding;  
                foreach (AlternateView av in mm.AlternateViews)  
                     AlternateViews.Add(SerializeableAlternateView.GetSerializeableAlternateView(av));  
           }  
           /// <summary>  
           /// Returns the MailMessge object from the serializeable object  
           /// </summary>  
           /// <returns></returns>  
           public MailMessage GetMailMessage()  
           {  
                MailMessage mm = new MailMessage();  
                mm.IsBodyHtml = IsBodyHtml;  
                mm.Body = Body;  
                mm.Subject = Subject;  
                if( From != null )  
                     mm.From = From.GetMailAddress();  
                foreach (SerializeableMailAddress ma in To)  
                {  
                     mm.To.Add(ma.GetMailAddress());  
                }  
                foreach (SerializeableMailAddress ma in CC)  
                {  
                     mm.CC.Add(ma.GetMailAddress());  
                }  
                foreach (SerializeableMailAddress ma in Bcc)  
                {  
                     mm.Bcc.Add(ma.GetMailAddress());  
                }  
                foreach (SerializeableAttachment att in Attachments)  
                {  
                     mm.Attachments.Add(att.GetAttachment());  
                }  
                mm.BodyEncoding = BodyEncoding;  
                mm.DeliveryNotificationOptions = DeliveryNotificationOptions;  
                Headers.SetColletion(mm.Headers);  
                mm.Priority = Priority;  
                if (ReplyTo != null)  
                     mm.ReplyTo = ReplyTo.GetMailAddress();  
                if (Sender != null)  
                     mm.Sender = Sender.GetMailAddress();  
                mm.SubjectEncoding = SubjectEncoding;  
                foreach (SerializeableAlternateView av in AlternateViews)  
                     mm.AlternateViews.Add(av.GetAlternateView());  
                return mm;  
           }  
      }  
 }  

onsdag den 14. oktober 2009

Third party webservices

Every time I have to make integration with third party webservices I always try to detach the application from the webservice due to the constant errors/problems with webservices.

I am still looking for an easy way to make a test page that easily test and validates a given webservice and method.

I always spent a lot of time assuring that it isnt some bug I have introduced in my application that has broken the webservice integration.

Once in a while it is some bug introduced by myself but in 8 out of 10 times it is data that the webservice collect that are invalid or the webservice itself that fails.

I will write my experiences with this issue here and try to create an easy way of testing a webservice.

But please share if you have any ideas.

Me and sitecore

This blog exists because I have a love/hate relationship with the CMS created by Sitecore which is a big part of my job as a web developer in Netmester A/S in Denmark.

I am always challeging Sitecore and Sitecore unfornunately often breaks to all the weird stuff Im doing to it every day.

Ill create blogs about all the weird stuff I meet and struggles to solve every day.

And this is the first in many.

Im at the moment developing a solution in Sitecore 6.1.0 which is currently the latest Sitecore release (not recommended version).

One of the new things in Sitecore 6 is that all security (rights, roles and other membership related stuff) are implemented using standard asp.net security rather than using Sitecores own security.
This should in itself be an approvement but sitecore has for one thing decided to implement the Sitecore domain functionality as part of the username which I personally think is pretty stupid.

Im using a third party member system that supplies users to the website (username, email, name etc).

In order to be able to log out this user, after login, using FormsAuthentication.SignOut() the user NEEDS to be added to the asp.net
Membership and therefore is visible in Sitecore security editor. With more than 5000 users this makes the security editor a bit crowded.
In order to put the users in a customer specific domain I need to prefix the user with the domain name followed by a backslash (\)
This makes it hard to map the sitecore user with the third pary member system or at least makes it more complicated because I now need to remove
the domain name from the sitecore user or append the domain name to the third party username.



I can live with this but the latest issue I have found is not easy to solve.
When redirecting from a page in my website (to login page due to user not logged in) sitecore throws an error looking like this

 System.InvalidOperationException: user at Sitecore.Caching.UserProfile.UserProfileCache.GetRecord(String userName, String propertyName)  
 at Sitecore.Security.UserProfile.GetPropertyValueCore(String propertyName)  
 at Sitecore.Security.UserProfile.get_SerializedData()  
 at Sitecore.Security.UserProfile.get_CustomProperties()  
 at Sitecore.Security.UserProfile.SerializeCustomProperties()  
 at Sitecore.Security.UserProfile.Save()  
 at System.Web.Profile.ProfileModule.OnLeave(Object source, EventArgs eventArgs)  
 at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()  
 at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)  

This exception is thrown in another thread than the request is executing in because no Session is available when I pick up the exception in Global.asax.

No user is logged in when the exception is thrown and it makes sense that the GetRecord() failes due to username is empty.

I have added a profile provider and a profile property that makes it possible to get a DB instance of the user logged in. This might be the trigger of the exception but I havnt been able to solve the bug.

This is the profile section of the web.config


 <profile defaultProvider="sql" enabled="true" inherits="Sitecore.Security.UserProfile, Sitecore.Kernel">  
  <providers>  
   <clear/>  
   <add name="sql" type="System.Web.Profile.SqlProfileProvider" connectionStringName="core" applicationName="sitecore"/>  
   <add name="switcher" type="Sitecore.Security.SwitchingProfileProvider, Sitecore.Kernel" applicationName="sitecore" mappings="switchingProviders/profile"/>  
   <add name="CommunityProfileProvider" applicationName="Community" type="Netmester.Profile.GenericProfileProvider" connectionStringName="Community"/>  
  </providers>  
  <properties>  
   <clear/>  
   <add type="System.String" name="SC_UserData"/>  
   <add name="CommunityProfile" provider="CommunityProfileProvider" type="Netmester.Framework.Community.DomainObjects.User"/>  
  </properties>  
 </profile>