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!

Code metrics in reflector for C#

Tuesday, June 9th, 2009

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

Session state in generic handlers

Thursday, February 14th, 2008

http://techleppie.wordpress.com/2006/10/01/session-state-and-generic-handlers/

The solution to my problem : IRequiresSessionState. I had never come across this interface before but it was the magic key!

According to MSDN :- Specifies that the target HTTP handler requires read and write access to session-state values. This is a marker interface and has no methods.

However, if you only require readonly access then alternatively it is better to make use of IReadOnlySessionState. This will have a performance improvement and provide more scalability.

you can implement multiple interfaces using a ‘,’ see below:

public class process : IHttpHandler, System.Web.SessionState.IReadOnlySessionState {

C# Generics - interesting article

Thursday, February 14th, 2008

http://www.willasrari.com/blog/c-generics/000191.aspx

Strategy Pattern C# .NET 3.0

Saturday, January 5th, 2008

http://www.unboxedsolutions.com/sean/archive/2005/10/23/779.aspx

Intellisense for the NHibernate XML Schemas

Friday, December 14th, 2007

http://blog.benday.com/archive/2006/01/15/3646.aspx

  1. Download the NHibernate source and go to the “src\NHibernate“ directory
  2. Copy nhibernate-configuration-2.0.xsd, nhibernate-mapping-2.0.xsd, and nhibernate-generic.xsd to the Visual Studio “schemas“ directory.  By default, the schemas directory is “C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas“. or “C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas”
  3. Restart Visual Studio

Getting many-to-many mappings working in nhibernate

Friday, December 14th, 2007

this was really useful:

http://sdesmedt.wordpress.com/2006/10/21/nhibernate-part-5-mapping-techniques-for-aggregation-many-to-one-and-many-to-many-mapping/

Getting back to NHibernate

Monday, November 19th, 2007

Going to use NHibernate for a new project:

http://www.hibernate.org/362.html

Latest docs:

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/

Finally after a lot of fiddling I got it working. This was useful:

http://www.theserverside.net/tt/articles/showarticle.tss?id=NHibernate

c# and timers

Thursday, August 16th, 2007

The elapsed function has to be a delegate that conforms to an ElapseEventHandler

System.Timers

timer = new Timer(1000 * 60 * 15);

timer.AutoReset = true;

timer.Elapsed += functionCall;

timer.Start();

console example:

using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
Timer timer = new Timer(10000);
timer.AutoReset = true;
timer.Elapsed += printHello;
timer.Start();

Timer timer2 = new Timer(1000);
timer2.AutoReset = true;
timer2.Elapsed += printGoodbye;
timer2.Start();

Console.ReadLine();
}
public static void printHello(Object sender, ElapsedEventArgs e)
{
Console.WriteLine(”hello: ” + DateTime.Now);
}

public static void printGoodbye(Object sender, ElapsedEventArgs e)
{
Console.WriteLine(”BHYE: ” + e.SignalTime.ToString());
}
}
}

C# specification location

Monday, August 6th, 2007

C:\Program Files\Microsoft Visual Studio 9.0\VC#\Specifications\1033