Wednesday, March 9, 2011

Some simple steps to convert ASP.NET application to Silverlight application

Sometime you may be asked to look at the possiblity of converting the ASP.NET application to Silverlight application. Trust me, there are no hard and fast rules to migrating ASP.NET to Silverlight due to various reasons;
  1. Silverlight runs in client-side; ASP.NET runs in the server-side. This means, you will not be able to use as much as functionalities available from server-side.
  2. Application Life Cycle is entirely different for Silverlight and ASP.NET. You need to familiarize yourselves first before starting to work on silverlight.

But, somehow I managed to find some steps which will ease the ASP.NET to Silverlight migration;
  1. Find the equivalent controls for ASP.NET controls in Silverlight
  2. Convert the .aspx pages to .xaml files
  3. Define the Page navigation in App.xaml.cs file (bit difficult to do)
  4. Silverlight support very few assembly references. Migrate the .aspx.cs code .xaml.cs code accordingly.
  5. If you encounter some assembly references breaking, convert those functionalities to WCF service. Good thing about silverlight is that, it supports communication with WCF service with some limitations (no WS-Security)
  6. Silverlight follows MVVM pattern, if you application built around MVVM or can be easily upgraded to MVVM pattern, you can migrate your ASP.NET application to Silverlight application at ease...

I will update this post, If I manage to find some other tweaks and tricks in converting ASP.NET to Siliverlight.

Tuesday, February 22, 2011

Code to capture the Message arrived event in Outlook

There may be times when we want to do some operation upon arrival of the message in the Outlook Inbox. Here is the simple piece of code which will help to capture the message arrived event in Outlook.

//Initialization code
protected override bool DoInitialise()
{
outlook = new ApplicationClass();
outlook.Session.Logon(null, null, false, false);
inbox = outlook.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Items;
inbox.ItemAdd += new ItemsEvents_ItemAddEventHandler(inbox_ItemAdd);
outlook.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(outlook_NewMailEx);
return true;
}


void outlook_NewMailEx(string EntryIDCollection)
{
//This event will be fired whenever a mail arrives in Inbox
}

Passing Default Credentials from Silverlight to WCF

While developing Silverlight and WCF apps you will have to share the default credentials from Silverlight to WCF service sometime.



This is how you can do;


1) Create the <basicHttpBinding> with the following information under the <bindings> section;

<basicHttpBinding>
<binding name="BasicBinding0">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>

2) Add the below piece of section under the <System.ServiceModel> section
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
</serviceHostingEnvironment>

3) Add the below piece of code above the OperationContract behavior;
[System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode=System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Allowed)]
public class StaticDataProcessing : ProcessTaskWCFBase

Please note that steps 2 and 3 are needed to avoid the HttpContext.Current coming as NULL issue.

This way you can pass the default credentials from Silverlight to WCF.

ProjectLinker extension for Visual Studio

Recently we were developing an application in silverlight which will consume WCF for doing some business operations and data access. WCF was designed using MessageContracts and DataContracts concept. Each operation will translate to input and output DataContracts. This WCF is not an tranditional one in which when you add reference you will get all the data contracts. So we will have to share all these DataContracts to Silverlight app. There arised the problem, since the Desktop/Web class library project cannot shared in silverlight app. So we thought of creating a separate silverlight class library replicating all the classes. This will be a serious maintenance nightmare!!!!
That time, I came across the concept of ProjectLinker synchronization tool. This is specially designed to address such scenarios. Basically Multi-targeting framework.
Bingo!!! Problem solved. Using this we can share the same class files between different platforms nothing but between the Desktop/Web class library and Silverlight class library.

You can find more about how to use this tool in the below link;
http://msdn.microsoft.com/en-us/library/ff921108(v=pandp.20).aspx

Download Project Linker for VS2008 here;
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=387c7a59-b217-4318-ad1b-cbc2ea453f40&displaylang=en

Download Project Linker for VS2010 here;
http://visualstudiogallery.msdn.microsoft.com/5e730577-d11c-4f2e-8e2b-cbb87f76c044/

Thursday, January 6, 2011

Issues With Programmatically Attaching Multiple Files To A List Item

Recently one of my team member was facing issue while adding multiple files to a list item programmatically. The issue was, about 10% files added programmatically were corrupt while uploading. So whenever the user tries to download the file, they were getting "HTTP 500 Internal Server". Below is the piece of code he was using;

HttpFileCollection uploads = HttpContext.Current.Request.Files;


for (int i = 0; i < uploads.Count; i++)

{


HttpPostedFile upload = uploads[i];


if (upload.ContentLength == 0)


continue;


Stream inputStream = upload.InputStream;


byte[] buffer = new byte[inputStream.Length];


inputStream.Read(buffer, 0, (int)inputStream.Length);


inputStream.Close();


eItem.Attachments.Add(upload.FileName, buffer);


}


eItem.Update();
 
On analysing we found out that, the issue was with Attachments.Add method. It was causing some corruptions in the bytes uploaded. So we tried with Attachments.AddNow..
Bingo!!! It started to work without any issues.
 
The only difference between "Attachments.Add" and "Attachments.AddNow" was, "Attachments.AddNow"; it will create new version whenever you attach.
Also, "Attachments.AddNow" internally calls the "Update" method of the List Item. Though its very weird, it is solving the problem of bytes corruption while uploading multiple attachments to the list item.