April, 2010

26
Apr 10

ASP.NET MVC2 Plugin Architecture Tutorial

This is part 1 of a multi-part tutorial:

I recently started a new project with a requirement that the software be able support plugin modules. I assumed that this sort of functionality would be well documented and demonstrated on the internet, but had a surprisingly hard time finding information.   Throw Visual Studio 2010 and ASP.NET MVC2 into the mix and all I could really find were some proof-of-concept examples, the best being J Wynia’s example which ended up forming the basis for my solution. I took Wynia’s example (and some of the subsequent discussion in the comments) and expanded it to meet my requirements:

  • I want to use Visual Studio 2010, .NET 4.0,  ASP.NET MVC2.
  • Plugins are not a PITA to develop and test.
  • Plugins live in separate Visual Studio solutions.
  • Plugins can stand alone as applications (primarily for ease of development)
  • Plugins can be loaded without restarting/redeploying the main application.
  • Plugins can provide information to the hosting application.

In this series of posts, I will walk through creating a basic plugin framework from scratch, starting with describing Wynia’s VirtualPathProvider concept and then building on it to form a solution that can be used in the real world (at least my twisted definition of real world!).

Continue reading →

22
Apr 10

Snippet: Use CURL with NTLM authentication on an Active Directory domain

C:\>curl --ntlm -u domain\user:password http://localhost/somesite/
22
Apr 10

Conditionally embed ASP.NET MVC2 Views as resources during build in Visual Studio 2010

This is adapted from a recent StackOverflow post of mine:

I have a ASP.NET MVC2 project in VS2010 that can be deployed in two modes: standalone or plugin. In standalone mode, the views should live outside the compiled assembly as .aspx files (the default setup). In plugin mode, the views are switched (currently by hand) to embedded resources and the entire assembly is dropped into a host project folder.

Initially, this required the developer to go through each view and switch it from Build Action: “Content” to “Embedded Resource” and vice versa.  I wanted to create a new solution configuration named “Plugin” to automatically grab all .aspx files and build them as resources.  The solution turned out to be quite simple.

Unload the project:

Right click on the project and click “Edit Project.csproj”:

Find the section where all your .aspx files are included as

<ItemGroup>
    <Content Include="Global.asax" />
    <Content Include="Web.config">
      <SubType>Designer</SubType>
    </Content>
    <Content Include="Web.Debug.config">
      <DependentUpon>Web.config</DependentUpon>
    </Content>
    <Content Include="Web.Release.config">
      <DependentUpon>Web.config</DependentUpon>
    </Content>
    <Content Include="Views\Comment\Create.aspx" />
    <Content Include="Content\Site.css" />
    <Content Include="Scripts\jquery-1.3.2.js" />
    <SNIPPED>
</ItemGroup>

Add the following code right below:

<Choose>
    <When Condition=" '$(Configuration)'=='EmbedViews' ">
        <ItemGroup>
            <EmbeddedResource Include="Views\*\*.aspx" />
            
        </ItemGroup>
    </When>
</Choose>

Right click on the project and select “Reload Project”

Create a new project configuration named “EmbedViews”:

13
Apr 10

404 2 1260 Error For .NET 4.0 Application on IIS 6

If you just installed the .NET 4.0 runtime on your server and are banging your head against the wall trying to figure out why it is displaying 404 errors for pages that exist, make sure that ASP.NET v4.0 web service extension is enabled in IIS Manager:

Enable ASP.NET v4.0 Web Service Extension

Update

You should see 404 2 1260 in the IIS logs if you have substatus logging enabled.  From MSDN404.2 – Web service extension lockdown policy prevents this request.

09
Apr 10

Quick Tip

Yesterday, I was using jQuery to add some functionality to an old custom user control. I found that the relative path for including jQuery was incorrect once the user control was rendered on a page:

On the .ascx markup page, the path was:

<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>

Once the control was rendered on a page, the path should have been:

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

This mixup manifested itself as an error: Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function

Of course, fixing the include path on the user control .ascx page breaks Visual Studio’s awesome jQuery intellicrack.

ScottGu to the rescue.

The solution is put the correct include path (as noted above) on the MasterPage or the page the control will be used on, then put a fake reference on the user control page wrapped in an if statement:

<% if (false) { %>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<% } %>

This tricks Visual Studio Intellisense into working while preventing a duplicate jQuery inclusion.

03
Apr 10

Running Shoe Review: Vibram Five Fingers KSO

I broke my right foot on a motorcycle when I was young.  I shattered three metatarsals in my right foot and had surgery to rebuild it.  I spent the next twelve years using my gimpy foot as an excuse for why I could not run (not to mention my flat feet and bad knees!).

Two years ago I started running.  I was trying to lose weight and started jogging a slow mile on the treadmill.  You know, for cardio!  Soon, one mile became three and the next thing I know, I’m outside in the middle of winter running five miles at a time, three times a week.

My journey to this point has not been easy.  When I started having knee pain, I bought a pair of Brook’s Beast Motion Control shoes.  When I started having Iliotibial band syndrome, I saw a podiatrist and was cast for orthodics.  When the orthodics failed to help, I turned to barefoot running.

I really enjoyed running barefoot… but my feet did not.  I spent months running one mile at a time, on a smooth indoor track–my feet were blistered and destroyed after every run.  I just could not to get my mileage up.  That is when a buddy of mine pointed me at Vibram Five Fingers.  I bought a pair of KSOs and immediately took them outside.

One hundred steps away from my door and I had a silly grin on my face.  It was like instantly developing thick I’m-a-kenyan-who-has-run-barefoot-since-birth callouses.  I ran for six miles that first day.  Yeah, I did get some blisters on my big toes, but SIX MILES!

So the review.  I ordered the shoes online for about $80 shipped from TravelCountry.com.  My first impression, upon un-boxing the shoes, was that they looked like a goofy pair of aquasox.  The best way to describe them is “gloves for your feet.”  There really is not much there.  A few millimeters of rubber, some cloth, and a velcro strap.  The construction seemed solid, though seams were glued, not stitched, which I found concerning.  The rubber on the bottom of the shoe has some texture and is siped.  Each toe has its own rubber pad that wraps around the top of the toe.

Running in the KSOs is a whole new experience.  I have logged three 3-5 mile runs every week for two months with the Vibrams.  My first runs left my calves in massive pain.  Running barefoot entails landing with a midfoot or forefoot strike.  Your calves become shock absorbers and work much harder than they are used to.  So I spent a few weeks walking around funny while my legs caught up.

The soles are three millimeters thick.  I can feel every pebble, root, and squishy slug.  The Vibrams make it enjoyable to run on different surfaces and I find myself looking for new textures to step on.  Instincts seem to take over when running like this.  If I step on a sharp rock, I find my body automatically compensating and rebalancing my stride to avoid putting my weight on the object.  After a few runs, I stopped looking at my feet and began looking farther ahead, trusting my body to handle any obstacles underfoot.  The interactive nature of the shoes has pushed me more towards trail running, though I have no issues running on asphalt.  Running feels more pure and more efficient with the shoes.  Not to mention, my ITBS and knee issues have vanished and my runs are pain free.

I was worried that the shoes would wear quickly.  After nearly 100 miles, my worries have proven to be unfounded.  The rubber on the toes shows some minimal wear but looks like it will be good for another 200 miles more.  The glue and fabric seams have held up great and show no signs of separation or wear.

The shoes do have some flaws.  If I step in a puddle or some mucky dirt, they seem to slurp the moisture up through the toes and it goes straight through the fabric sidewalls.  This makes the run less comfortable and I try to avoid it.  I would like to see a wet weather version of the shoe that is a bit more waterproof for winter running in Seattle.

My other complaint is that my toes get cold.  Having the toes separated from each other means less insulation, less insulation means numb toes.  I am sure this will not be an issue in the summer, but it has been an annoyance.

To sum it up: I am a convert.  I am already planning my second and third pair of Vibrams.  I am thinking of trying to the Flows for cold wet/weather running and potentially some Treks for… well trekking.  I would recommend these to anyone who is looking lose the heel strike and give barefoot running a try.  These shoes will get you reasonably close to outdoor barefoot running without destroying your feet.

02
Apr 10

Poweshell Nested Folder ACL/Permissions Comparison Script

I have been learning and experimenting with Powershell for some daily admin tasks. I plan to post any helpful scripts I come up with. Here is my first one!

With credit to antize for the starting point, I created this powershell script to loop through a nested directory structure and compare permissions.  This was to support a migration of files from one server to another.

The directory structure was:

-Project
----Area
--------Task

The script recurses through a folder on the Source and Target paths and compares ACLs on the Task folders. Any folders that have non-matching permissions are printed to an output file. Feel free to take and modify this for yourself!

#CompareNetworkACL.ps1
[System.IO.FileInfo] $outputFile = "C:\temp\AclCompareDump.txt"
[System.IO.DirectoryInfo] $searchDirSource = Read-Host -Prompt "Enter source project folder path to search"
[System.IO.DirectoryInfo] $searchDirTarget = Read-Host -Prompt "Enter target project folder path to search"

if ($outputFile.Exists)
{
    $outputFile.Delete()
}

if ($searchDirSource.Exists -and $searchDirTarget.Exists)
{
   foreach ( $sourceProject in $( Get-ChildItem $searchDirSource | where { $_.psIsContainer -eq $true } ) ) {
        $targetProject = Get-Item $($searchDirTarget.FullName + "\" + $sourceProject.Name)

        foreach ( $sourceArea in $( Get-ChildItem $sourceProject.FullName | where { $_.psIsContainer -eq $true } ) ) {
            $targetArea = Get-Item $($targetProject.FullName + "\" + $sourceArea.Name)

            foreach ($sourceTaskFolder in $($sourceArea | Get-ChildItem | where { $_.psIsContainer -eq $true }) ){
                $targetTaskFolder = Get-Item $($targetArea.FullName + "\" + $sourceTaskFolder.Name)

                $currentAcl = Get-Acl $sourceTaskFolder.FullName
                $refacl = Get-Acl $targetTaskFolder.Fullname

                Write-Progress -Activity "Searching source for folder ACLs..." -Status ("Comparing " + $sourceTaskFolder.FullName + " to " + $targetTaskFolder.FullName)

                $comparison = compare-object $refacl $currentAcl -Property Access
                if ($comparison -ne $null)
                {
                    $sourceTaskFolder.Fullname | Out-File $outputFile.FullName -Append
                    [int] $hits += 1
                }
                $comparison = $null
            }
        }
    }

    if ($hits)
    {
        & notepad.exe $outputFile.Fullname
    }
    else
    {
        Write-Warning "All target ACLs matched the source ACLs."
    }
}