A potentially dangerous Request - MVC 1 / 2 - .NET 4

Thursday, July 29th, 2010

If you are running MVC 1/2 and have just upgraded to .NET 4 you may probably see this error:

A potentially dangerous Request.Form value was detected from the client

even in you have the attribute of

[ ValidateInput( false ) ]

set. This is due to some breaking changes in .NET 4. See this article for more details.

Basically all yo have to do is open your Web.config and add:

<httpRuntime requestValidationMode="2.0" />

Inside the

<system.web>

section.

That is it and you are fixed. (you still need the

[ ValidateInput( false ) ]

attribute - it just makes it process as it was before ;))

using nunit to run tests built in .NET 4

Sunday, July 25th, 2010

This almost caught me out so I thought that I would share what I found. By Default NUnit will not run tests from an assembly that has been built against v4 of the CLR.

You can however tell it you want it to by adding a few lines to the config files. So to fix it all you have to do is open the nunit.exe.config file and add the following:

<startup>

<requiredRuntime version="v4.0.20506" />

</startup>

Just inside <configuration>

and then add

<loadFromRemoteSources enabled="true" />

just after <runtime>

Now if you are like me and are just using the nunit console to run tests with rake then you have to do the same as above in the nunit-console.exe.config

Simples!

NHibernate, Castle IOC and MVC.NET 2 running in medium trust on shared hosting

Saturday, November 28th, 2009

This has caused me a lot of pain today so I thought that I would share my findings!

If you intend to publish your site to shared hosting and they are restricting you to a Medium trust security policy (which is expected really on shared hosting) you need to have assemblies that work properly!

After finding the issue the first thing I did was to get a dev environment that replicated the problem. The easiest way to do this is to set the trust level in the web.config. You can do this by adding this within the system.web  element:

<system.web>
<trust level=”Medium”/>

…other tastey elements…..

</system.web>

In the future I will set this before I start programming anything - probably good practice when you know you will be deploying to a medium trust environment but I will put that down to a lesson learnt!

After that you need to build Castle from source with a few additional parameters to make it run in medium trust. To do this you need to check it out from SVN. I had a few problems here too with SVN externals. I ended up getting the latest version of tortoise and checking it out on a Windows 7 VM I had running.
Check it out from here: http://svn.castleproject.org:8080/svn/castle/trunk/

Once fully checked out, open a cmd promt and navigate to the the root of the checked out directory. run this command:
build.cmd -D:assembly.allow-partially-trusted-callers=true release quick build

Watch the window for any errors and please read the error messages if you get any. I got one in relation to not having the .NET 2.0 SDK installed, so guess what - I installed it and tried again and it worked!

OK great now we have some assemblies that will run under medium trust. Copy these over the old versions and make sure you perform a clean build. Thats Castle sorted right? WRONG! It still won’t work.

I then had an issue with Castle.Service.Transations. Read this for more information (http://stackoverflow.com/questions/1038914/using-castle-windsor-and-the-nhibernate-facility-on-shared-hosting). OK no probs got that sorted by registering it in my container:

container.Register(Component
.For(typeof (IActivityManager))
.ImplementedBy(typeof (TLSActivityManager)));

OK we are still not there - a couple more things to do in the web.config. You need to add the requirePermission=”false” attribute the the castel section:

<section name=”castle” requirePermission=”false” type=”Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor”/>

I also disabled the NHibernate reflection optimiser:

<item key=”reflection-optimizer”>false</item>

OK almost there - the app will now run. Next was the problem of URL’s and the fact I was not allowed a wild card mapping - fine - no probs, lets update the routing. I changed my routing to the following:

routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new {controller = "Home", action = "Index", id = ""},
new[] {”ANAMESPACE.Core.Controllers”});

Yay! That works! There is only one more problem. If I go to / then it can’t find a route regardless of the default file settings. So a bit of a workaround was to add another route after the usual default:

routes.MapRoute(
"",
new { controller = "Home", action = "Index", id = "" },
new[] { “ANAMESPACE.Core.Controllers” });

This feels very wrong and I don’t like it! There must be a better way.

Anyway - screw you shared hosting and medium trust!

Castle ISessionFactory can be resolved outside of a web request

Wednesday, July 15th, 2009

I found this very useful today. I have a Quatz Job running that requires access to a repository. This repository is usually resolved by castle like so:

var repository = Registry.Get<MyRepository>();

In the case I have I cannot do this as ‘MyRepository’ depends on having an ISessionManger injecting into it but in this context one does not exists. What I found out though (thanks to Thom) was that I can simply ask the container for an ISessionFactory like so:

var sessionFactory = Registry.Get<ISessionFactory>();

Now I can create a class that implements ISessionManager and instantiate my repository with this - top stuff!

Now I have found this I will be changing how my integration tests work!

testing sending emails .NET

Friday, July 10th, 2009

At first I thought this may be tricky but as it turns out I am not the only one ever to have this problem!

First of all you can try and use the SpecificPickupDirectory. This tells the smtp client to simply drop the message into a desegnated directory:

http://dotnettipoftheday.org/tips/smtp-delivery-method-SpecifiedPickupDirectory.aspx

<system.net>

<mailSettings>

<smtp deliveryMethod=”SpecifiedPickupDirectory”>

<specifiedPickupDirectory pickupDirectoryLocation=”c:\Test\” />

</smtp>

</mailSettings>

</system.net>


If that does not float your boat then you can take it a stage further:

http://haacked.com/archive/2006/05/30/ATestingMailServerForUnitTestingEmailFunctionality.aspx

Another useful tool is this: http://www.codeproject.com/KB/IP/despop3client.aspx

Code metrics in reflector for C#

Tuesday, June 9th, 2009

http://reflectoraddins.codeplex.com/Wiki/View.aspx?title=CodeMetrics

sqlite with nhibernate - a guide to getting it working

Wednesday, May 27th, 2009

I am currently working on a new project and I am going full on TDD and BDD (where appropriate). As I have been in a variety of different environments I want to be able to check out the project, run the tests and write some code. My integration tests rely on a database (currently proving that my nhibernate mappings are correct) and I did not want there to be a dependency on having a database running so I wanted to use sqlite.

Here is what I did:

Download sqlite:

http://sourceforge.net/projects/sqlite-dotnet2

Get an sqlite GUI admin tool:

http://sqliteadmin.orbmu2k.de/

Create a database to connect to.

Change you nhibernate config file:

<?xml version="1.0" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.SQLiteDialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SQLite20Driver
</property>

<property name="connection.connection_string">
Data Source=directory-in-your-test-assembly\your-database.s3db
</property>

<property name="query.substitutions">true=1;false=0</property>

<mapping assembly="YourAssembly" />

</session-factory>
</hibernate-configuration>

Notice that I have used the SQLite20Driver instead of SQLiteDriver (it won’t work otherwise!)

Don’t forget to make sure that your db is copied to your output directory.

I will expand upon this post tomorrow when I have more time!

New release of the ajax control toolkit

Thursday, May 14th, 2009

http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx

NAct - a .NET port of Jbehave

Wednesday, May 13th, 2009

NAct is now on google code. Built by Tim Wilde from TechnoPhobia (my place of work).

Super duper:

http://code.google.com/p/nact

Quartz.NET - open source job scheduling system

Wednesday, May 13th, 2009

How have I missed this for so long!

http://quartznet.sourceforge.net/