Poor mans composite UI or separating out controllers in MVC3

I was looking to create a simple composite UI style application in Microsoft MVC3. What I want is a host MVC application that will compose its UI from other Controllers and Views packaged in other DLLs. The concept comes from autonomous components and SOA where a service owns not only its traditional business services but all the data and almost-all of the UI.

I thought I would be overriding controller builders, adding configuration, etc, etc, but actually this “poor-mans” variant is very easy. Here are the steps I followed;

  1. Create your Host MVC3 web application (Host.proj), for the sake of this example choose the Internet application so you get the default HomeController
  2. Create your satellite MVC3 controller by creating a standard class library project, called Satellite.proj
  3. Add a reference in Satellite to System.Web.Mvc
  4. Create a controller for your satellite, say MyController. Add an ActionResult such as;
    public class MyController : Controller 
      public ActionResult MyInformation() 
        return View("MyInformation2");
  6. Create the View folders for your controller in the Satellite project, e.g. Views\My\
  7. Now for linking them together. I just added a project reference from Host to Satellite in order to grab the DLL. The Views are slightly harder, I used a post build event in the Satellite such as; xcopy “$(ProjectDir)Views” “$(SolutionDir)Host\Views” /E /R /Y
  8. That’s it. Just add some code to use the controller, such as placing the following in the index view;
  9. @Html.Action("MyInformation","My")

    Edit: Note you get errors complaining that ‘there is no build provider registered for the extension’ you need add a dummy web.config to the project, follow these instructions;
    Using Razor in a class library

Using NuGet with Visual Studio Express

A quick “tip”. Currently Visual Studio C# 2010 Express cannot install the NuGet extension. To workaround the problem you must have Visual Studio Web Developer Express. The NuGet extensions has been signed to work with VWD. Then to install a NuGet package for your VCE project do the following (it’s not nice);
1. Create your project in VCE, save and close
2. Open the project in VWD, it will probably complain but just ignore it
3. Run your NuGet command, e.g. Install-Package
4. Save and close VWD
5. Reopen VCE, add reference and browse to the packages folder that is now in your solution.
There, it’s not nice but it does work

Workaround for problems Launching Performance Analyzer

Over the past couple of weeks I have been trying to use the Visual Studio 2010 Performance Analyizer on a web site but kept on getting;

VSP 7008: ASP.net exception: “The web site could not be configured correctly; getting ASP.NET process information failed. Requesting ‘http://localhost/VSEnterpriseHelper.axd’ returned an error: The remote server returned an error: (404)

The site is a relatively complex IIS6 site with a number of smoke and mirror redirects so I guessed this complexity was confusing the tools. I contacted Microsoft Support to help. During a shared-desktop session with support it started working! Ah the curse of the support person making it work just by looking at it. What actually happened is that when I launched the Performance Wizard I incorrectly gave it a file URL (\\localhost) rather than a web URL. The wizard prompted to say there were no DLLs to profile but I said to continue (not realising my mistake). The profile finished with no results. I corrected the problem (//localhost) by editing the resulting profile NOT by creating via a new wizard. It works?! So I had created a workaround Confused smile. I then manually added the missing target DLLs and re-ran the profile. No problems, everything worked.

There you go, if you are running IIS6 and having problems getting the Profiler to connect, why not try this ridiculous workaround.

How to add a link to a project folder in Visual Studio

I’ve had a quick play with adding the contents of a folder from another project, i.e. I want to add links to all the files in another project’s folder. So for example; I have a project called ConsoleApplication1 and I want to ensure it always has links to the images in another project called ClassLibrary1 (yes nice names eh). So first create a folder with the same name, e.g. images, in ConsoleApplication1. Add a link to one of the files in ClassLibrary1\Images (Add existing item->add as link). Unload ConsoleApplication1 and Edit the project file. Locate the Content Include and replace it as follows;

  <Content Include="..\ClassLibrary1\images\*.*">

So whatever changes you make to ClassLibrary1\Images will automatically be reflected in ConsoleApplication\Images. It’s early days but it seems to run ok.

Getting a Logitech mouse to work with Visual Studio

Recently my trusty Microsoft Explorer mouse broke, so I decided to invest in a Logitech Performance Mouse MX. The idea was that I could use some of the extra button to help me do stuff in Visual Studio. However, once installed it didn’t seem to work with VS. The left/right buttons and the vertical scroll worked but nothing else. The solution is to go to the All Programs->Startup folder and change the Set Point program to run as Administrator. Sure you’ve opened up another security hole but I want a mouse that works.

Visual Studio no longer opens files from Windows Explorer?

“There was a problem sending the command to the program.”

This has been frustrating me for a couple of days now so I decided to see what the issue was. Apparently it appears that the problem is due to couple of issues;

  1. I’d marked Visual Studio 2008 to ‘run as Administrator’
  2. I have multiple versions of Visual Studio

Apparently when you have multiple copies of VS the shell launches the Visual Studio selector program to work out which version it should launch. However, since the selector is running under the normal UAC state it gets a little confused. So rather than set the selector to Admin I returned Visual Studio back to it normal state and I can now open the files.

Problems with locked DLLs when compiling in Visual Studio

For some, as yet to be understood, reason after a few debug sessions some of the dependant DLLs become locked by Visual Studio and the build stage cannot overwrite the DLLs. Today I finally decided to see if anyone else was have the same problem and found this in Gary Farr’s blog (copied in case the blog becomes unavailable);
Final solution:
VS basically locks the file and you cannot use third party resources to unlock it.  Therefore, just use VS!  In the Properties of a project in your IDE you have Buld Events.  Basically, you can write scripts during pre and post builds of a project.  I added these two lines in the pre-build event command line, which basically unlocks the dll within Visual Studio.
IF EXIST $(TargetPath).LOCKED  (del $(TargetPath).LOCKED) ELSE (IF EXIST $(TargetPath) (move $(TargetPath) $(TargetPath).LOCKED))

Using IIS in Vista to access your Visual Studio web site project

Developing in Vista certainly presents the odd challenge. Recently I’ve started to develop applications using Live Mesh and I wanted to test out Delegated Authentication which involves running a web site under IIS. Now I’m not used to IIS 7 so after the initial shock of a new UI I setup the required virtual directory and ran my web site. However, I kept getting a rather strange error message;

The requested page cannot be accessed because the related configuration data for the page is invalid.

At first I took that to mean I’d corrupted my web.config file but no that all seemed fine. I eventually tracked down a Test Settings button in IIS. This showed a big warning triangle saying that it couldn’t ensure the site identity would be authorised to access the disk folder. Once I’d set an appropriate user to access the folder everything worked fine.

To access the Test Settings button;

Open IIS->Navigate to your site->Basic Settings (in the right pane)->Test Settings->Click on any line with a warning triangle and read the explanation.

Visual Studio freezing/crashing with Team System Foundation Server

Finally made the switch from Visual Source Safe to Team System Foundation Server today but as luck would have my machine didn’t enjoy the experience. My colleagues machine was running fine, the speed difference in opening a solution or checking in/out was impressive. My machine would just sit there endlessly waiting for the check-in to work until finally I killed it off. It then occurred to me that I hadn’t connected to the Foundation Server in my Team Explorer. Once connected everything seems to be running fine. So my tip is, ensure you’ve connected via Team Explorer before you attempt to use the Source Control.