Patrick's SharePoint Blog

SharePoint's Booming world

Archive for September, 2010

Native VHD boot from external harddisk using Win7

Posted by Patrick Boom on September 22, 2010

Like many of us out there, I like the use of virtual servers for testing, research and developement. It keeps my host OS clean, but also gives me the opportunity to leverage the hardware of my machine to the full extend, except for the disk virtualization.

The possibility of Windows 7 to native boot VHD images is great, if not a blessing. I never want to move back to VPC or VMWare. The only downside was that it was hard to boot an image from an external harddisk (USB). I say hard, because I simply did not know how to make that work. But it should be possible.

Cut a long story short, using the following article described here by Mark Wilson, you can modify the windows installationin the VHD to allow booting from USB. It takes a lot of command line commands, but in the end, it works! Thanks Mark!

Posted in Uncategorized | Tagged: , , | 4 Comments »

Using PowerShell to register SPGuidance diagnostic areas

Posted by Patrick Boom on September 16, 2010

The SPGuidance package delivers a set of components to ease certain administration tasks with SP 2010, for example logging.

It hooks in to the built-in SharePoint logging functionality and exposes a set of classes to easily log your own events in the event log, ULS logs or any custom log sources, like a database.

If one would look into this model, you will find that a diagnostic area corresponds to an event source in the event logs. For instance, if I register a diagnostic area called Boom.MyCustomApplication, the event source mentioned in the logs would read the same. This allows support engineers to easily track the source of the event and act accordingly.

However, although the diagnostic area corresponds to the event source, they are not the same. You can register diagnostic areas using the logging component of the SPGuidance set, but this does not mean a corresponding event source is also created. In fact, event sources need to be registered on each front-end separately with sufficient permissions, before the logging component can write to the event log using the diagnostic area as the event source. So how would you create the event sources on each front-end? Well, there are a couple of possible approaches:

  1. Use PowerShell to create the sources on each front-end remotely
  2. Use a Timer Job to run custom code on each front-end that registers the event source.

In this post, I will show how to do that from PowerShell. These are only two lines of code and the script can be run from a remote location on each front-end.

# First load object models to use
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Practices.SharePoint.Common”)

# call the registration method to ensure all registered diagnostic areas are also registered as event sources
[Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsAreaEventSource]::EnsureConfiguredAreasRegistered()

In this script, we first load the assembly Microsoft.Practices.SharePoint.Common.dll. We assume here that the assembly is available from the GAC. In my next post, the solution provided will ensure these assemblies are deployed to the GAC for use. Once we have done that, we can call the static EnsureConfiguredAreasRegistered method of the DiagnosticsAreaEventSource class that will iterate through all the configured and registered diagnostic areas and ensures that an event source is registered for that. The event sources will be registered on the Application event log. During the next couple of days, I will also investigate the possibility of registering the sources to different logs.

In the next post, I will show a custom solution on how to create additional diagnostic areas and use a timer job to ensure the event sources are created at the front end. Till next time!

Posted in PowerShell, SharePoint 2010 | Tagged: , , , , | 9 Comments »

XMLFormatter provider for serialization

Posted by Patrick Boom on September 2, 2010

Introduction

(This article was published by myself on CodePlex in 2006. Moved it here)

During the development process of our project, the need arose to serialize objects to XML. For this purpose, the .NET provides the XmlSerializer class to serialize objects. This class has some disadvantages, however.

First, the creation of the class is expensive in performance. This is caused by the fact the class creates an in-memory assembly to quickly read and write from the objects to serialize. It does this by using reflection to inspect the object. This leads to the second disadvantage; only public properties and fields are serialized. These properties also have to be writable so that the XmlSerializer can set the properties during deserialization. This leads to problems encapsulating your code. Finally, but not less important, it does not support the ISerializable interface.

Alternatives

To get around these problems, you can use the SoapFormatter class to serialize the object to SOAP. This class does support the ISerializable interface, and does not use reflection. The resulting XML, however, is less accessible than the plain XML the XmlSerializer produces, and has a lot of overhead. The only thing left is to create your own formatter by implementing the IFormatter interface.

XmlFormatter

The code accompanied by this article contains our XmlFormatter class that has the following features:

  • Produces more clean XML than the SOAP structure of the SoapFormatter
  • Supports the ISerializable interface
  • Does not impact performance by creating in-memory assemblies

The following example shows the use of the formatter, by serializing a class called MyObject:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace XmlFormatterSample
{
[Serializable]
public class MyObject : ISerializable
{
   public string MyPublicProperty
  
{
      get { return _myPublicProperty; }

      set { _myPublicProperty = value; }
  
}
   private string _myPublicProperty;

   private string MyPrivateProperty
  
{
      get { return _myPrivateProperty; }

      set { _myPrivateProperty = value; }
  
}
   private string _myPrivateProperty;

  
   public
MyObject()
  
{
      this._myPublicProperty = “This is my public property”;

      this._myPrivateProperty = “This is my private property”;
  
}

   public
string GetProperties()
  
{
      string properties = “MyPublicProperty = “ +
MyPublicProperty + “\r\n”;
     
properties += “MyPrivateProperty = “ + MyPrivateProperty;
      return properties;
  
}
#region ISerializable Members

   <summary>

   /// Special serialization constructor.

   </summary>

   <param name=”info”></param>

   <param name=”context”></param>

   public MyObject(SerializationInfo info,
StreamingContext context)
   {
     
_myPublicProperty = info.GetString(“MyPublicProperty”);
     
_myPrivateProperty = info.GetString(“MyPrivateProperty”);
   }
   <summary>

   /// Interface method to place the properties

   /// in the serialization queue

   ///
   </summary>

   ///
   <param name=”info”></param>

   <param name=”context”></param>

   public void GetObjectData(SerializationInfo info,
StreamingContext context)
   {
     
info.AddValue(“MyPublicProperty”, MyPublicProperty);
     
info.AddValue(“MyPrivateProperty”, MyPrivateProperty);
   }
#endregion
   }
}

The following code serializes and deserializes the object from and to a MemoryStream using our XmlFormatter. Please note that both the public and the private properties are being serialized.

using DotNetMagazine.September.Examples;
using System.IO;
using System.Xml;
using System.Data.SqlTypes;
namespace XmlFormatterSample
{
   class Program
  
{
      static void Main(string[] args)
     
{
        
MyObject object1 = new MyObject();
        
MyObject object2;
         // write the properties to the console
        
Console.WriteLine(“The properties of object1 are:”);
         
Console.WriteLine(object1.GetProperties());
        
Console.WriteLine();
         // serialize the object

         // ***************************************
        
MemoryStream stream = new MemoryStream();
        
XmlFormatter serializer = new XmlFormatter(typeof(MyObject));
        
serializer.Serialize(stream, object1);
         // reset the stream to the beginning
         
stream.Position = 0;
        
SqlXml xml = new SqlXml(stream);
         // ***************************************

         // write the XML value to the console
        
Console.WriteLine(“Xml value of object 1:”);
        
Console.WriteLine(xml.Value);
        
Console.WriteLine();
         // recreate the object in object 2

         using (MemoryStream stream2 =
new MemoryStream(Encoding.UTF8.GetBytes(xml.Value)))
        
{
           
object2 = (MyObject)serializer.Deserialize(stream2);
        
}

         // write the properties to the console
        
Console.WriteLine(“The properties of object2 are:”);
        
Console.WriteLine(object2.GetProperties());
        
Console.WriteLine();
        
Console.WriteLine(“Press any key to continue”);
         
Console.ReadKey();
      
}
  
}
}

The code is free to use in your projects. This code is also part of an article published in the Dutch version of the .NET (MSDN) magazine, September issue 2006: “Objecten en Sql Server 2005, XML als intermediair”.

This article was also published on the CodeProject in 2006, but copied to my blog for centralization.

I hope this formatter will be of use to you! You can find the source here and the demo version here.

Posted in .NET Framework | Tagged: , , , | Leave a Comment »

 
%d bloggers like this: