Wednesday, December 14, 2011

Serialize and Deserialize Objects in C# Revised

I wrote a blog entry a few years back about this topic, and I have since been asked to rewrite the Serialize and Deserialize Functions to support any type of object. Here is how one can Serialize/Deserialize objects using boxing in C#:

Sample Object:

    [Serializable]
    public class Person
    {
        public string FirstName { getset; }
        public string LastName { getset; }
    }
Here are our methods to perform the operations:




    public class Serializer
    {
        public static string GetString(MemoryStream psMemStream)
        {
            string lsReturn = string.Empty;
 
            psMemStream.Position = 0;
            using (StreamReader loReader = new StreamReader(psMemStream))
            {
                lsReturn = loReader.ReadToEnd();
            }
            return lsReturn;
        }
 
        public static string SerializeMessage(Object poObject)
        {
            string lsResponse = "";
 
            try
            {
                MemoryStream loStream = new MemoryStream();
                XmlSerializer loMessageSerialize = new XmlSerializer(poObject.GetType());
                loMessageSerialize.Serialize(loStream, poObject);
 
                lsResponse = GetString(loStream);
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            return lsResponse;
        }
 
        public static object DeserializeMessage(string psObjectXML, Object poObject)
        {
            string loResponse = string.Empty;
            object loObject = new object();
 
            try
            {
                XmlSerializer loMessage = new XmlSerializer(poObject.GetType());
                loObject = loMessage.Deserialize(new StringReader(psObjectXML));
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            return loObject;
        }
    }

When you want to call the above methods, here is how you do it:



            Person loPerson = new Person();
            loPerson.FirstName = "John";
            loPerson.LastName = "Doe";
 
            string lsXMLString = Serializer.SerializeMessage(loPerson);
 
            MessageBox.Show(lsXMLString);
 
            Person loPerson2 = new Person();
            loPerson2 = (Person)Serializer.DeserializeMessage(lsXMLString, loPerson2);
That's it! Enjoy!

Pete Soheil
DigiOz Multimedia
www.digioz.com

Thursday, December 1, 2011

Configuring SQL Server Instance for Windows Azure Storage Emulator

Let's say you have downloaded the Windows Azure SDK which installs several Add-on to Visual Studio 2010 and IIS, and includes some Windows Azure Solutions Templates. Now suppose you have created a Windows Azure Solution based on one of these templates, and are trying to debug the solution. The first time you try to debug the solution, you may come across the following message:
Windows Azure Tools: Failed to initialize Windows Azure storage emulator. Unable to start Development Storage. Failed to start Storage Emulator: the SQL Server instance ‘localhost\SQLExpress’ could not be found.   Please configure the SQL Server instance for Storage Emulator using the ‘DSInit’ utility in the Windows Azure SDK.
You will get this message if either of the items below applies to you:
  1. You don't have any SQL Server Installed on your workstation. 
  2. You don't have SQL Express installed and have the full version of SQL Installed
  3. You have an older version of SQL Express installed which Azure SDK does not support. 
If item 3 applies to you there is no choice but to install the latest SQL Express version.  But if you have the full version of SQL Installed as Default Instance, then you can do the following to fix the issue:
  1. Go to "Start > All Programs > Accessories", right click on "Command Prompt" and select "Run as Administrator".
  2. Navigate to "C:\Program Files\Windows Azure Emulator\emulator\devstore\" directory by entering "cd [Directory Path]" and hitting Enter. 
  3. Enter "DsInit /sqlInstance:." and hit Enter. 
Here is the Window you will see (or a slight variation from it which accomplishes the same thing):


That's it! You should now be able to rebuild your Azure Solution in Visual Studio 2010 and run it to debug it locally using the Azure Emulators.

Pete Soheil
DigiOz Multimedia, Inc.
www.digioz.com

Monday, November 28, 2011

Fetching Remote Data in Salesforce.com using APEX

Suppose you need to fetch some data from a remote website inside a Salesforce.com APEX Code. This can be any type of text ranging from text to XML content, which you want to use to perform additional tasks based on. This comes in particularly handy if you need to do some sort of an integration with a Legacy System which cannot produce a structured WSDL Web Service.

Here is how you can accomplish this:

1. Create an APEX Class called "HttpCalloutSample", with the following code:

public class HttpCalloutSample {

// Pass in the endpoint to be used using the string url
  public String getContent(String url) {

// Instantiate a new http object
    Http h = new Http();

// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');

// Send the request, and return a response
    HttpResponse res = h.send(req);
    return res.getBody();
  }
 
  public String testContent()
  {
      return 'Testing ... 1... 2... 3...';
  }
}


Now let's call this APEX Class using Eclipse Force.com IDE "Execute Anonymous" code block feature (or if you prefer to call it from within an APEX Trigger you can do that too). Your calling code would look something like this:

HttpCalloutSample loCS = new HttpCalloutSample();
String lsReturnValue = loCS.testContent();
System.debug (lsReturnValue);
lsReturnValue = loCS.getContent('http://www.digioz.com/downloads/vbnet/DigiOz_Distribution_Agreement.txt');
System.debug (lsReturnValue);

The URL is basically the URL of the content you wish to fetch from the Remote Server. In this case I am using the URL of the Distribution Agreement of one of my open source projects, which is a simple text file containing some text. The first time you execute the code, you will see this error:

This is because by default Salesforce.com blocks accessing remote websites. We can fix this by going to "Setup > Security Controls > Remote Site Settings" in Salesforce, and add an Exception for our Remote site:

Now when you call that same site URL again, you will be able to access the remote content and fetch the data from it. If you look in your debug log, you will see an entry like this:

10:15:31.238 (238577000)|USER_DEBUG|[5]|DEBUG|Definition of Freeware

Freeware distribution gives users a chance to try software and continue to us ... (the rest of the content)


I hope others find this blog entry helpful.

Thanks,
Pete Soheil
DigiOz Multimedia, Inc.
www.digioz.com

Tuesday, September 27, 2011

Retrieving the COM class factory for component failed due to the following error: 80040154

Despite the fact that COM Components are considered legacy technology now, because Microsoft never made a clear cut decision to retire COM Components in newer versions of their Operating System these Libraries still linger around in many Applications, and have made their way into many .NET based Applications through DLL Referencing. Here is an error I have come across from time to time that doesn't tell you a whole lot unfortunately:

Retrieving the COM class factory for component with CLSID {XXXXXXXX-XX...} failed due to the following error: 80040154

There are several ways to resolve this issue. Here is a short list:

  1. Modify your project's platform from 'Any CPU' to 'X86' in Project Properties, Build/Platform's Target in Visual Studio
  2. If your Application is a Web-Based Application, try setting IIS to run in 32-bit mode. You can do this by going to IIS Manager, selecting the Application Pool, then choosing "Advanced Settings". The second option from the top is "Enable 32-bit Applications".
  3. If your Application is Web-Based, it could be that the com object was not configured to allow launch and access permissions for the aspnet user identity. Under administrative tools > Component services under the tree view, go to Component Services > Computers > My Computer > DCOM Config and find the registered com object. Right click for properties. Under the security tag, customize the Permissions to allow asp.net user
Usually one of the above 3 methods solves this problem.


Pete Soheil
DigiOz Multimedia
www.digioz.com




Saturday, September 10, 2011

Blogger finally got an iPhone App

It was only a matter of time I suppose, but Blogger finally got an iPhone App on the App Store. Some features worth mentioning are location detection, upload of pictures from your photo library of your phone or you can take a picture and upload it instantly. Overall it does make it a lot easier to Blog on the Go, so I give it two Thumbs Up!

Pete Soheil
DigiOz Multimedia
www.digioz.com

Wednesday, August 17, 2011

Disable SharePoint 2010 Mobile Site

SharePoint 2010 has made a huge improvement over the 2007 version, but there are still times when you may want to disable the Mobile version of the site, for example if you wish to have your SharePoint Publishing Site be External Facing. There is no out of box feature for disabling SharePoint 2010 Mobile Site, however there is a quick fix for this. Follow these steps to disable SharePoint 2010 Mobile Site and redirect all traffic to the standard version:

  1. Locate the IIS Directory for the SharePoint Site in Question (for example inetpub/wwwroot/wss/VirtualDirectories/80).
  2. Open the folder "App_Browsers", and open the file "compat.browser" using notepad. 
  3. Find all instances of the "isMobileDevice" attribute, and change its value from "true" to "false". So find this:


Replace it with this:



That's all you need. No IISRESET is required.

Thanks,
Pete Soheil
DigiOz Multimedia
www.digioz.com






Wednesday, April 27, 2011

Troubleshooting SharePoint 2010 Errors Using Correlation ID

SharePoint 2010 has made tracking down error messages somewhat cryptic. If you ever get an error message, usually it looks something like this:


Assuming that this is a production environment, the error message above tells you pretty much nothing! So how can we make sense of what is going on behind the scene you may ask. To answer that, let's navigate to the LOGS directory of the SharePoint Server in question and find the most recent log file there (which should be at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS\):


Right-Click on the file in question, and open it using notepad. Copy the Correlation ID you obtained from the error message and perform a search for it. You will have multiple entries in the file which will specify what page the error occurred on, what the Scope was when the error happened, what event was triggered when the error happend, and finally; what the actual cause of the error was, which depending on what you did to upset SharePoint will be different.

Pete Soheil
DigiOz Multimedia, Inc.
www.digioz.com




Twitter Updates