Sep 30 2011

Getting C# Source Code From a COM Interop Assembly With ILSpy

Category: .NET | Tips & Tricks | COMDavid @ 15:43

Many of you were no doubt ticked when RedGate decided to stop providing Reflector as a free tool.  Well, this post is not designed to harp on that or talk about how Redgate backtracked on that decision.  Instead, let’s talk about a great replacement for it that is free and open source! 

This great new tool is called ILSpy.  It has a great feature that will let you output the disassembly code to actual C# files.  This comes in super handy if you have a COM Interop assembly that you need to modify. I ran into this situation when working on a project leveraging the VirtualBox API.

The great thing is that it is supper easy to do.  Once you open the interop assembly in ILSpy simply select the assembly node in the treeview and then on the File menu select Save Code…  You will then be prompted to save a C# project file.  Once you do that it will generate the project file and will create a .cs file per object  in the assembly.  And that is it!

Tags: , ,

Sep 13 2011

Windows Server 2008 R2 Installation Problem: “Setup was unable to create a new system partition…”

Category: General Tech | Hardware | Tips & Tricks | Windows 2008David @ 02:32

Recently I decided to virtualize all three of the primary servers that I have running on my network.  To do this I decided to install Windows Server 2008 R2 on two new servers.  To save space I chose smaller cases and to save money I left out the optical drives since I have a USB DVD drive that would work for my installations.  In addition I downloaded the latest drivers onto a large flash drive in preparation for the install.

The motherboard that I chose is the MSI 890FXA-GD65 which supports USB boot as well as RAID 0, 1, 5, 10, and JBOD.  So, I chose to run a RAID 1 setup with two disks. After setting the array up in the BIOS I inserted the USB DVD drive, a flash drive with the RAID drivers, and cranked up the machine.  Things moved pretty fast and I landed on the screen for selecting the drive to which I wanted to install Windows.  It was blank and so I simply loaded the drivers from the flash drive and the RAID drive appeared with full capacity.  Normally the next thing you should do is select the drive and hit the next button, but when I did that I got this wonderful error message:

“Setup was unable to create a new system partition or locate an existing system partition. See the Setup log files for more information.”

To make a long story short I had a heck of a time figuring out the cause of my problem.  In the end what worked for me was leaving the USB Flash drive out of the system until I was ready to install the drivers.  I inserted the flash drive, selected “Load Drivers” and when through the browsing, selecting and loading.  Right after the drivers were finished loading I pulled the flash drive and then hit “Next”.  And the rest, as they say, is history…  Windows installed fine from that point on and within about 30 minutes my new server was up and running.

Tags: , , ,

Mar 22 2011

An Old Trick for Speeding up .NET Compact Framework Builds: PlatformVerificationTask

Category: .NET | Tips & Tricks | Compact FrameworkDavid @ 11:48

OK! I know that most new mobile development is occurring on Windows Phone 7, iPhone or Android, but I have no other choice than Windows Mobile 6 for the application I’m currently working on due to customer requirements.  So, here I am stuck back in VS 2008 developing a mobile application for Law Enforcement.  In the past I played around with mobile development in my spare time, but never had to do any long term development for it.  Since starting on this current project I noticed how long it took for my builds to deploy to the emulator and test devices for debugging.  Deployment was taking from a few minutes to over 5 minutes at times.  Well, you know as well as I do what that can do to developer productivity!

Now,  I’m the type of developer that codes a chunk of code, builds, tests, and repeats the process in rapid succession so I was really not having any fun. So to figure out what was going on I set the Build output to “Diagnostic” in my Build options and hit F5.  Well, I noticed that it was hanging on a build task called “PlatformVerificationTask”.  Well, after digging around a little on Bing. I found this old blog post.  The short of it is that the Platform Verification Task does a check on your assemblies at build time to make sure there are no unsupported properties, methods or events in your code.  This process can really slow down the build process on large applications that have multiple DLLs. (Like my project does!)

So, what the solution?  It’s a good ‘ole file editing trick.  All you need to do is edit the file located at %windir%\Microsoft.NET\Framework\v2.0.50727\Microsoft.CompactFramework.Common.Targets and find the node:

<Target
    Name="PlatformVerificationTask">
    <PlatformVerificationTask
        PlatformFamilyName="$(PlatformFamilyName)"
        PlatformID="$(PlatformID)"
        SourceAssembly="@(IntermediateAssembly)"
        ReferencePath="@(ReferencePath)"
        TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
        PlatformVersion="$(TargetFrameworkVersion)"/>
</Target>

 

Add the following attribute to the <PlatformVerificationTask"> node:

Condition="'$(DoPlatformVerificationTask)'=='true'"

Now restart Visual Studio 2008 and check out how much faster your build is!

Tags: , ,

Nov 16 2009

Got ViewState?

Category: .NET | ASP.NET | Best Practices | Tips & TricksDavid @ 10:58

A few months ago I wrote a couple of posts on Managing Session Variables in ASP.NET using a Proxy. Since then I have thought about the need to write a post addressing the use of the ViewState as well.  As with SessionState,  ViewState is essential just a Dictionary of String, Object pairs.  This means that at design time you have to do a fair amount of type casting to use the objects you store in ViewState in a strongly typed manner.  It also introduces many of the same issues as using the native SessionState, such as:

  • the inability to know where you are using a given ViewState variable in your code.
  • the potential for mistyping a ViewState key when putting an object in ViewState or retrieving.
  • the increase in code to check for null values returned from ViewState.

Each of these issues is a pain to deal with on their own, but when the issues are compounded over a complex page then it really can make for a bad experience when developing.  The solution to this problem that I propose is to use custom properties on the page to access the various objects that are stored in ViewState.  Below is an example of a code behind page that illustrates how to do this.  This example assumes you have a class Person that defines properties of FirstName, LastName and ID, as well as a web page with two textboxes and a button.

   1: Partial Public Class _Default
   2:    Inherits System.Web.UI.Page
   3:  
   4:  
   5: #Region " Fields "
   6:  
   7:    Private Const _personViewState As String = "MyPerson"
   8:    Public Const QueryStringParameterPersonID As String = "pid"
   9:  
  10: #End Region
  11:  
  12: #Region " Properties "
  13:  
  14:    Public Property Person() As Person
  15:       Get
  16:          If ViewState(_personViewState) Is Nothing Then ViewState(_personViewState) = New Person
  17:          Return CType(ViewState(_personViewState), Person)
  18:       End Get
  19:       Set(ByVal value As Person)
  20:          ViewState(_personViewState) = value
  21:       End Set
  22:    End Property
  23:  
  24: #End Region
  25:  
  26: #Region " Methods "
  27:  
  28:    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  29:       If Not String.IsNullOrEmpty(Page.Request.QueryString(QueryStringParameterPersonID)) Then
  30:          'Get your user from your datastore and populate the Person Property with the returned user.
  31:          Person = PersonDAO.GetPersonByID(CInt(Page.Request.QueryString(QueryStringParameterPersonID)))
  32:       End If
  33:  
  34:       FirstNameTextbox.Text = Person.FirstName
  35:       LastNameTextBox.Text = Person.LastName
  36:  
  37:    End Sub
  38:  
  39:    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
  40:       Person.FirstName = FirstNameTextbox.Text.Trim
  41:       Person.LastName = LastNameTextBox.Text.Trim
  42:  
  43:       'Persist your Person to your datastore, etc.
  44:    End Sub
  45:  
  46: #End Region
  47:  
  48: End Class

In this example you can see how I have abstracted the access to my ViewState Person object into a property that always returns an instance of a Person. You can of course have this property return null (Nothing) if you need or prefer that for your own development.  Either way you are still eliminating the direct reference to the ViewState allowing you to program against a strongly typed object.  As I did in my SessionProxy posts, I am using a private constant to hold the string value of my key to eliminate the chance of mistyping this value when using it in multiple places.

Another technique that I would like to point out in this post is the use of public constants to represent the string value of the query parameter names for this page.  By exposing a constant representing this value I then make it possible for other developers to easily see the parameters that my page accepts.  If they utilize the constant it will also allow me to change the actual string value of my parameter name at any point in time without affecting the functionality of my page. (I realize this is a big “If” that is reliant on team standards.)

Tags: , , , , ,

Aug 5 2009

Managing Session Variables in ASP.NET using a Proxy – Part 2

Category: .NET | Architecture | ASP.NET | Best Practices | Tips & TricksDavid @ 15:54

In my last post I gave an example of using the Proxy Design Pattern to manage your ASP.NET Session.  In this post I wanted to refine that proxy class through some refactoring and add some additional functionality to it that remove Session variables and also allow us to use the proxy to clear or abandon the Session as a whole. We will also create a mechanism for grouping Session variables into related objects in the SessionProxy.

First I want to add a private property that will allow me to eliminate some of the verbosity in my code.  I’ll add a private static property that encapsulates the current HTTPSessionState object.  We can then use that property to refactor our other properties so they are more readable.

   1: private static HttpSessionState Session { get { return HttpContext.Current.Session; } }

Next we’ll add some code into our properties that will check for the value being set and remove the Session variable from the Session object if necessary.  As an example I’ll use a new Property of type String.

   1: public static String CategoryName
   2: {
   3:    get 
   4:    {
   5:       if (Session[CATEGORYNAME] == null) { return String.Empty; }
   6:       return (String)Session[CATEGORYNAME];
   7:    }
   8:  
   9:    set 
  10:    {
  11:       if (String.IsNullOrEmpty(value)) { Session.Remove(CATEGORYNAME); }
  12:       else { Session[CATEGORYNAME] = value; }
  13:    }
  14: }

Now I want to add a few methods to allow me to use the SessionProxy to Clear the current Session or Abandon it.

   1: #region Methods
   2:  
   3: public static void Clear() { Session.Clear(); }
   4:      
   5: public static void Abandon() { Session.Abandon(); }
   6:      
   7: #endregion

At this point I feel that I have a pretty good proxy class that allows me to leverage my ASP.NET Session in a strongly type manner and to easily see what I am storing in the Session and what their types are.

However, in using this solution in a production application I have found that my list of properties has grown longer than I would like. There were close to two dozen at last count and all the Session variables have not been migrated to use the proxy.  I found myself increasingly tired of seeing all those properties each time I accessed the class.  On top of that some of the names were getting rather long in an effort to give them meaningful names.  To resolve this I implemented several properties on the SessionProxy that were child proxy objects.  To make things a bit easier I created a base class called SessionProxyChildProxyBase.  Here’s the code in full for that base class.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.SessionState;
   6:  
   7: public abstract class SessionProxyChildProxyBase
   8: {
   9:  
  10:   #region Properties
  11:  
  12:   protected HttpSessionState Session { get { return HttpContext.Current.Session; } }
  13:  
  14:   #endregion
  15:  
  16: }

You will note a few points on this class.  Unlike the SessionProxy class the SessionProxyChildProxyBase class is an abstract class.  We don’t want to allow others to implement this base class outside of our library so that the SessionProxy class now becomes a proxy for this class as well as for the ASP.NET Session.  It also contains a protected Session property that will facilitate a decrease in code verbosity.

Now we will leverage this base class and create derived classes that will contain related properties.  In your application you may provide a central location for entering search criteria but you pass those criteria via the Session to a page that actually displays the search results.  Let’s create a child proxy class to organize the various values being passed.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5:  
   6: public class SearchSessionProxy: SessionProxyChildProxyBase
   7: {
   8:  
   9:   #region Fields
  10:  
  11:   private const String SEARCHTEXT = "SearchText";
  12:   private const String SEARCHTYPENAME = "SearchTypeName";
  13:   
  14:   #endregion
  15:  
  16:   #region Properties 
  17:  
  18:   public String Text
  19:   {
  20:      get
  21:      {
  22:         if (Session[SEARCHTEXT] == null) { return String.Empty; }
  23:         return (String)Session[SEARCHTEXT];
  24:      }
  25:  
  26:      set
  27:      {
  28:         if (String.IsNullOrEmpty(value)) { Session.Remove(SEARCHTEXT); }
  29:         else { Session[SEARCHTEXT] = value; }
  30:      }
  31:   }
  32:  
  33:   public String TypeName
  34:   {
  35:      get
  36:      {
  37:         if (Session[SEARCHTYPENAME] == null) { return String.Empty; }
  38:         return (String)Session[SEARCHTYPENAME];
  39:      }
  40:  
  41:      set
  42:      {
  43:         if (String.IsNullOrEmpty(value)) { Session.Remove(SEARCHTYPENAME); }
  44:         else { Session[SEARCHTYPENAME] = value; }
  45:      }
  46:   }
  47:  
  48:   #endregion
  49:  
  50:   #region Constructors
  51:  
  52:   internal SearchSessionProxy() { }
  53:  
  54:   #endregion
  55:  
  56: }

Now we need to add some code to our SessionProxy class to leverage this new class.  The first task is to add a private static field of type SearchSessionProxy.

   1: #region Fields
   2:  
   3: private static SearchSessionProxy _searchProxy = new SearchSessionProxy();
   4:  
   5: #endregion

Next we will create the property to access the field.

   1: public static SearchSessionProxy Search { get { return _searchProxy; } }

And that’s it!  Now we call utilize our SessionProxy with our new child proxy classes for organization and grouping so that we can doing something like this in our web app code…

   1: public void DoSomething() 
   2: {
   3:  SessionProxy.Search.Text = "Search Text entered by user";
   4:  SessionProxy.Search.TypeName = "Product Search";
   5:  SessionProxy.ProductID = 0;
   6:  Response.Redirect("SearchResults.aspx");
   7: }

Tags: , , , ,

May 29 2009

New Collaboration Tool

Category: Collaboration | Tips & TricksDavid @ 17:47

I have worked at several companies now that have used a number of different tools for online demos and collaboration such as WebEx and GoToMeeting.  Microsoft has now introduced a new product that provides the same functionality but is completely free!  The new product is called Microsoft SharedView.

SharedView, like the other brand collaboration tools, requires a small download and installation. When you open the program it places a menu bar at the top of the desktop that provides access to the application features. This new tools allows up to 15 members in a given session and provides for application specific sharing or full screen sharing.  The session host must have a valid Windows Live Account (Hotmail and Microsoft Passport accounts work as well) to start a session.  However, the other participants do not need to have an account.  Once the session is started the other participants simply enter the user id of the session host and the password for the session that is generated when the session is initiated.

One of the more interesting features the SharedView provides is the presence of multiple mouse cursors on the screen labeled with the name of the participant that is represents.  This is a great feature that allows the other users to be able to point to areas on the screen without having full control of the shared application.

If your looking for a great free product for real-time collaboration I really encourage you to check out Microsoft SharedView.

Tags: ,