Tuesday, 9 November 2010

CS0234: The type or namespace name 'Html' does not exist in thenamespace 'System.Web.WebPages' (are you missing an assembly reference?)

So I have just updated from MVC 3 Preview 1 to MVC 3 RC. I missed out the beta as didn't have time to check it out. Here are the steps I followed to get everything to work correctly.

Updating Ninject


Previously I was using the IMvcServiceLocator interface with Ninject to provide dependency injection. MVC 3 no longer provides this interface, instead it uses a simpilar one called IDependencyResolver
public class NinjectDependencyResolver : IDependencyResolver
{
    private IKernel _kernel;

    private ILog _logger = LogManager.GetLogger(typeof(NinjectDependencyResolver));

    public NinjectDependencyResolver(IKernel kernel)
    {
        _kernel = kernel;
    }
    public object GetService(Type serviceType)
    {
        try
        {
            return _kernel.Get(serviceType);
        }
        catch (Exception exception)
        {
            // log the error
            _logger.Error("Error whilst trying to get ninject service", exception);
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _kernel.GetAll(serviceType);
        }
        catch (Exception exception)
        {
            // log the error
            _logger.Error("Error whilst trying to get ninject service", exception);
            return new List<object>();
        }
    }
}




Update Razor Tempaltes so LayoutPage => Layout
remove @inherits System.Web.Mvc.WebViewPage from _layout.cshtml

change @RenderSection("SectionName", optional:true) => @RenderSection("SectionName", optional:false)


set web.config in views to

<configuration>
  <configsections>
    <sectiongroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" requirepermission="false" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="pages" requirepermission="false" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    </section></section></sectiongroup>
  </configsections>

  <system .web.webpages.razor="">
    <host factorytype="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <pages pagebasetype="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc">
        <add namespace="System.Web.Mvc.Ajax">
        <add namespace="System.Web.Mvc.Html">
        <add namespace="System.Web.Routing">
      </add></add></add></add></namespaces>
    </pages>
  </host></system>

  <system .web="">
    <httphandlers>
      <add path="*" type="System.Web.HttpNotFoundHandler" verb="*">
    </add></httphandlers>

    <!--<br/>        Enabling request validation in view pages would cause validation to occur<br/>        after the input has already been processed by the controller. By default<br/>        MVC performs request validation before a controller processes the input.<br/>        To change this behavior apply the ValidateInputAttribute to a<br/>        controller or action.<br/>    -->
    <pages br="">        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
      <controls>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagprefix="mvc">
      </add></controls>
    </pages>
  </system>

  <system .webserver="">
    <validation validateintegratedmodeconfiguration="false">

    <handlers>
      <remove name="BlockViewHandler">
      <add name="BlockViewHandler" path="*" precondition="integratedMode" type="System.Web.HttpNotFoundHandler" verb="*">
    </add></remove></handlers>
  </validation></system></configuration>

Wednesday, 25 August 2010

Javascript string.format


String.prototype.format = function () {
formatted = this;
for (i = 0; i < arguments.length; i++) {
formatted = formatted.replace("{" + i + "}", arguments[i]);
}
return formatted;
}

Monday, 17 May 2010

Easy way to reinstall fedora machine

So at work we got in a new machine to act as our repository server. However in the haste to get the machine up-and-running I made the school boy error of configuring the raid as RAID 0 instead of RAID 1. As the machine is now configured correctly I wanted an easy way to reinstall the machine with the same settings. Here are the steps that I used to reinstall the machine.

1 Install etckeeper



yum install etckeeper etckeeper-bzr

2 Configure etckeeper to use bzr



vi /etc/etckeeper/etckeeper.conf

# The VCS to use.
# VCS="hg"
#VCS="git"
VCS="bzr"
# VCS="darcs"

# Options passed to git commit when run by etckeeper.
#GIT_COMMIT_OPTIONS=""

# Options passed to hg commit when run by etckeeper.
#HG_COMMIT_OPTIONS=""

# Options passed to bzr commit when run by etckeeper.
#BZR_COMMIT_OPTIONS=""

# Options passed to darcs commit when run by etckeeper.
#DARCS_COMMIT_OPTIONS=""

# Uncomment to avoid etckeeper committing existing changes
# to /etc automatically once per day.
#AVOID_DAILY_AUTOCOMMITS=1

# Uncomment to avoid etckeeper committing existing changes to
# /etc before installation. It will cancel the installation,
# so you can commit the changes by hand.
#AVOID_COMMIT_BEFORE_INSTALL=1

# The high-level package manager that's being used.
# (apt, pacman-g2, yum etc)
HIGHLEVEL_PACKAGE_MANAGER=yum

# The low-level package manager that's being used.
# (dpkg, rpm, pacman-g2, etc)
LOWLEVEL_PACKAGE_MANAGER=rpm


3 Initialize etckeeper



etckeeper init


4 Push your etckeeper to a save location



bzr push sftp://server/source/etckeeper
bzr bind sftp://server/source/etckeeper




X Create the list of packages



rpm -qa --qf "%{NAME}\n" | sort > installed_rpm_list.txt


X Reinstall fedora


Reinstall fedora.

Reload Packages


Copy the install_rpm_list.txt back to your home directory and reinstall the packages using

yum install $(cat installed_rpm_list.txt)







Useful Links


Monday, 26 April 2010

Bazaar Plugin for OnTime Integration

I was playing around with bazaar, whilst reviewing distributed version control systems (DVCS), and created a simple plugin for use with axosoft's ontime. The plugin parses the commit message and links the files to the related task, defect or feature.

Install


Download the tar file from [download id="3"]
Extract and run the installer using the following:

tar -vxzf bzr-ontime-0.0.1.tar.gz
cd bzr-ontime-0.0.1
sudo python setup.py install


Configure


Setup the database connection information.
/etc/bzr-ontime.conf
[ontime]
server=your-mssql-server
username=sa
password=p4ssw0rd
database=ontime


Confirm Installation


bzr hooks

If the plugin is installed correct you should see the plugin listed under the post_commit section.

Related Reading








Saturday, 27 March 2010

Running NHibernate in Medium Trust

After deploying a NHibernate to a shared host I started running into a number of issues. Whilst developing we were running in full trust not in medium trust whilst the majority of shared hosts use. After hours and hours of searching I finally come across a post on the Castle Project mailing list which detailed how to get a nhibernate working under medium trust. Here is the steps I followed to get it working.

I am using NHibernate 2.1.2 and C# .NET 3.5 in Visual Studio 2008.

Step 1


Download Castle.Core from gitbhub
git://github.com/castleproject/Castle.Core.git



Step 2


Enable Castle to allow partially trusted callers.
Open buildscripts/CommonAssemblyInfo.cs and enable the library to enable partially trusted callers



Step 3


Disable generation of debug information for the projects Castle.Core and Castle.DynamicProxy.


Step 4


Build the projects

Step 5


Download Nhibernate source code from source forge.
http://sourceforge.net/projects/nhibernate/files/

Step 6


Open Nhibernate.Everything.sln and update the references for the NHibernate.ByteCode.Castle project so that the references for Castle.Core and Castle.DynamicProxy2 are the libraries build in step 4. Disable generation of debug information for the projects NHibernate, Iesi.Collections and NHibernate.ByteCode.Castle, similar to what was done in step 3.



Step 7



Copy the outputted library files from NHibernate.ByteCode/bin/Release to your shared libs folder of your project.



Step 8



Update the references in your project to point to the new libraries. Next turn off reflection optimization. This needs to be done in code before you create the configuration object. Configuring this in the hibernate.cfg.xml does not work. Update all your projects assemblies to set allow partially trusted callers, similar to step 2. Update your web.config to set requirePermission="false" in the nhibernate section delceration


NHibernate.Cfg.Environment.UseReflectionOptimizer = false;
Configuration = new Configuration();
Configuration.Configure();







You can now run your site in medium trust using lay loading.

Links



  • msysgit - http://code.google.com/p/msysgit/

  • TortoiseGit - http://code.google.com/p/tortoisegit/

  • NHibernate - http://nhforge.org/

  • Castle.Core Source - http://github.com/castleproject/

  • NHibernate Source - http://sourceforge.net/projects/nhibernate/files/


Related Reading














Sunday, 3 January 2010

Upload Image to Imgur.com

I was looking for an easy way to upload images from my laptop to imgur.com without having to open my browser up. I found a post explaining how to upload an image from nautilus. The script worked well enough however I didn't like the information it displayed to you at the end. So I rewrote the script in python so that it displays a dialog with link buttons for the available options for the image.

upload to imgur screenshot


The code is hosted at google.

Update

Thanks to the people over at reddit the script now supports automatically copies the url to the clipboard, and also correctly loads the glade file.

Friday, 1 January 2010

NAnt.ToDo a NAnt Plugin

This simple plugin for NAnt parses your source code and creates a report of all the TODO and comment tags in your source code. The code is hosted over at google code.

Comments


The task automatically identify comments in the following style

//TODO: First Test to do Item
//FIXME: First Fix me Item
//HACK: First Hack Item


Additional matches can be made by adding in Token elements to the NAnt task.

Sample



<ToDo source="path/to/source" output="report.xml" searchpattern="*.cs;*.txt">
<Tokens>
<Tokens Value="BUGFIX" />
</Tokens>
</ToDo>


Output



<ToDo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Item>
<Message>First Test to do Item</Message>
<File>NAnt.ToDo\ToDoTask.cs</File>
<Line>0</Line>
<Type>TODO</Type>
</Item>
<Item>
<Message>First Hack Item</Message>
<File>NAnt.ToDo\ToDoTask.cs</File>
<Line>0</Line>
<Type>HACK</Type>
</Item>
<Item>
<Message>First Fix me Item</Message>
<File>NAnt.ToDo\ToDoTask.cs</File>
<Line>0</Line>
<Type>FIXME</Type>
</Item>
</Items>
</ToDo>


Cruise Control Xsl


Below is a basic xsl style sheet that can be used with CruiseControl.NET to display the output from NAnt.ToDo.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:param name="applicationPath"/>
<xsl:template match="/">
<div id="ToDo">
<h1>ToDo List</h1>
<div id="Summary">
<h3>To Do</h3>
<table>
<tbody>
<xsl:for-each select="//ToDo/Items/Item">
<tr>
<td>
<xsl:value-of select="Type/text()"/>
</td>
<td>
<xsl:value-of select="Message/text()"/>
</td>
<td>
<xsl:value-of select="File/text()"/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>    
</div>
</xsl:template>
</xsl:stylesheet>