Archive for May 2010
Here are some instructions to redirect all requests to http://www.hostname.com to just http://hostname.com. There are a few reasons why you would want to do this, here is a good post explaining some reasons why, and how to redirect from http://hostname.com to http://www.hostname.com. Doing the opposite is just as simple, add this to your web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to remove WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.hostname.com$" />
</conditions>
<action type="Redirect" url="http://hostname.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
16
SharpArchitecture 1.5 in medium trust
2 Comments · Posted by Paul in ASP.NET, ASP.NET MVC, SharpArchitecture
UPDATE: The instructions for building S#arp were updated shortly after I published this post. See below for more.
My previous post on running S#arpArchitecture 1.0 in medium trust seemed to be helpful to a few, so here are instructions on getting the latest build of S#arp (1.5.2 at the time of writing) running in medium trust as well. The platforms that S#arp depends upon have matured a bit since last time and some of the assemblies already have the AllowPartiallyTrustedCallers attribute, so running in medium trust is much easier now. See the 1.0 post for a little more background info.
These instructions are organized as follows:
- The dependency project/component that we must rebuild
- Where to get the source
- Which previously built assemblies to copy to this project’s lib/dependency folder, if any
- Any changes you must make to the code, solution/project files, or build files
- How to build the component
- Where the build output is located, and the specific assemblies we are interested in. You may want to create a common lib directory to copy all of these to because you will need to keep track of them. I’ll only mention the assemblies here, but you may also want to copy the xml documentation files (which are named the same as the assemblies but with the .xml extension) as well.
Castle Core 1.2
- Get the source from: http://github.com/castleproject/Castle.Core/tree/1-2-stable
- Build the project with the following parameter:
-D:assembly.allow-partially-trusted-callers=true. Here is the command line I use:nant -D:assembly.allow-partially-trusted-callers=true -D:common.testrunner.enabled=false -D:project.config=release - The output is located in build\net-3.5\release. Copy these assemblies to your lib directory:
- Castle.Core.dll
Castle InversionOfControl 2.1
- Get the source from: http://github.com/castleproject/Castle.InversionOfControl/tree/2-1-stable
- Copy these assemblies to lib\net-3.5:
- Castle.Core.dll
- Build with the same command as for Castle Core:
nant -D:assembly.allow-partially-trusted-callers=true -D:common.testrunner.enabled=false -D:project.config=release - The output is located in build\net-3.5\release. Copy these assemblies to your lib directory:
- Castle.MicroKernel.dll
- Castle.Windsor.dll
Common Service Locator Windsor Adapter
- Get the source from: http://commonservicelocator.codeplex.com/wikipage?title=Castle%20Windsor%20Adapter
- Copy these assemblies to sharedlibs:
- Castle.Core.dll
- Castle.DynamicProxy2.dll (from your SharpArch bin directory)
- Castle.MicroKernel.dll
- Castle.Windsor.dll
- Add the following lines to CommonServiceLocator.WindsorAdapter\Properties\AssemblyInfo.cs:
using System.Security; [assembly: AllowPartiallyTrustedCallers]
- Build the project, either from Visual Studio or with the following command line:
msbuild commonservicelocator.windsoradapter.csproj /t:Rebuild /p:Configuration=Release
- The output is located in CommonServiceLocator.WindsorAdapter\bin\release. Copy these assemblies to your lib directory:
- CommonServiceLocator.WindsorAdapter.dll
MvcFutures (Microsoft.Web.Mvc.dll)
- Get the ASP.NET MVC 2 source from: http://aspnet.codeplex.com/releases/view/41742
- Open the MvcDev.sln solution in Visual Studio, and in the MvcFutures project:
- Delete the existing System.Web.Mvc reference (which is to the local System.Web.Mvc project) and add a reference to the System.Web.Mvc version 2 assembly in your GAC
- Add the following lines to Propertes\AssemblyInfo.cs:
using System.Security; [assembly: AllowPartiallyTrustedCallers] - Build the MvcFutures project. You can also build from the command line:
msbuild MvcFutures.csproj /t:Rebuild /p:Configuration=Release
- The output is located in bin\release. Copy these assemblies to your lib directory:
- Microsoft.Web.Mvc.dll
MvcContrib
- Get the source from: http://mvccontrib.codeplex.com/SourceControl/list/changesets
- Copy these assemblies to bin\castle:
- Castle.Core.dll
- Castle.MicroKernel.dll
- Castle.Windsor.dll
- Copy Microsoft.Web.Mvc.dll to bin\aspnetmvc
- Open MVCContrib.sln in Visual Studio and in the MvcContrib.Castle and MvcContrib.FluentHtml projects, add an AssemblyInfo.cs file with the following two lines:
using System.Security; [assembly: AllowPartiallyTrustedCallers] - Build the solution in Visual Studio or with the following command line:
nant.exe -buildfile:nant.build -D:project.config=release -D:runtests=false
- The output is located in build\net-3.5.win32-MVCContrib-release. Copy these assemblies to your lib directory:
- MvcContrib.dll
- MvcContrib.FluentHtml.dll
- MvcContrib.Castle.dll
Inflector.Net
- Get the source from: http://andrewpeters.net/inflectornet/. This site appears to be down at the moment so I’ve uploaded Inflector.Net to my site, you can download it from http://paulwideman.com/inflectornet.zip.
- Open Inflector.Net.sln and add the usual two lines to Propertes\AssemblyInfo.cs:
using System.Security; [assembly: AllowPartiallyTrustedCallers]Then build the solution in release config.
- The output is located in Inflector.Net/bin/release. Copy Inflector.Net.dll to your lib directory.
S#arp
- Get the latest source from: http://github.com/codai/Sharp-Architecture
- Copy all assemblies from your lib directory to bin.
- Edit the SharpArch.build file so that it adds the
AllowPartiallyTrustedCallersattribute to the automatically generated CommonAssemblyInfo.cs file, by adding the two highlighted lines below:<target name="version" description="Generate AssemblyInfo"> <property name="version.build" value="${build.number}" if="${property::exists('build.number')}"/> <property name="version.revision" value="${build.vcs.number.1}" if="${property::exists('build.vcs.number.1')}" /> <echo message="Marking build with version ${project.fullversion}" /> <delete file="${solution.dir}/CommonAssemblyInfo.cs" failonerror="false"/> <asminfo output="${solution.dir}/CommonAssemblyInfo.cs" language="CSharp"> <imports> <import namespace="System.Reflection" /> <import namespace="System.Runtime.InteropServices" /> <import namespace="System.Security" /> </imports> <attributes> <attribute type="ComVisibleAttribute" value="false" /> <attribute type="AssemblyVersionAttribute" value="${project.fullversion}" /> <attribute type="AssemblyFileVersionAttribute" value="${project.fullversion}" /> <attribute type="AssemblyInformationalVersionAttribute" value="${project.fullversion}" /> <attribute type="AssemblyCopyrightAttribute" value="Copyright © ${company.name} ${datetime::get-year(datetime::now())}" /> <attribute type="AssemblyCompanyAttribute" value="${company.name}" /> <attribute type="AssemblyConfigurationAttribute" value="${project.config}" /> <attribute type="AssemblyTrademarkAttribute" value="" /> <attribute type="AssemblyCultureAttribute" value="" /> <attribute asis="true" type="AllowPartiallyTrustedCallersAttribute" /> </attributes> <references> <include name="System.dll" /> </references> </asminfo> </target> - UPDATE: There seems to be a bug in the release build configuration in the latest S#arp source. The merged SharpArch.dll assembly does not contain the content from all of the SharpArch projects. To fix this, open SharpArch.sln in Visual Studio and update the properties for each project to change the output path for the release configuration to “bin\Release\”.
- Build S#arp with this command line:
NAnt.exe -buildfile:SharpArch.build -D:project.config=release
- The output is located in build. Copy SharpArch.dll to your lib directory.
From here, the steps are the same as with 1.0:
Now we’ve got a version of SharpArch and all its dependencies that will run in medium trust. Even if you have no interest in running the Northwind sample in medium trust, there are a few things we must do to a web app built with the SharpArch project template to enable it to run in medium trust. So I believe these instructions are applicable to any web app built with the SharpArch project template.
Northwind Sample
- Copy all of the newly build assemblies from your lib directory to the SharpArch\bin directory. The Northwind project references these dlls from there.
- Edit src\NorthwindSample\CommonAssemblyInfo.cs to add the
AllowPartiallyTrustedCallersattribute:using System.Security; [assembly: AllowPartiallyTrustedCallers]
- Open Northwind.sln in VS2008
- We have to make a few changes to the NHibernate configuration so that NHibernate will run properly in medium trust. One of these (
reflection-optimizer) requires us to move the NH config out of its own file and into web.config. To move the config into web.config, in the Northwind.Web project:- Add a config section declaration for the NH config section, I put mine right after the log4net section declaration:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false" />
- Copy the
<hibernate-configuration>section and all of its contents from NHibernate.config to somewhere in web.config, I put mine right above the log4net section. - Open Global.asax.cs, go to the
InitializeNHibernateSession()method, and remove the last parameter in the call toNHibernateSession.Init()to tell NHibernate to get its config from web.config.
- Add a config section declaration for the NH config section, I put mine right after the log4net section declaration:
- Update the NH configuration info in web.config for use in medium trust:
- Remove the
adonet.batch_sizeproperty, NHibernate update batching does not work in medium trust - Add
<reflection-optimizer use="false" />as a child of<hibernate-configuration>. The reflection optimizer requires some reflection permissions that aren’t available in medium trust. Turning the reflection optimizer off will cause a performance decrease overall, although you may see a performance boost during app startup. See the NHibernate documentation here for more info. - As per the SharpArch docs, add the
current_session_context_classproperty with valuemanaged_web. In my testing I didn’t find this necessary, but assuming the docs are up to date then it is their guidance to add it. You can see the NHibernate documentation here for more on this property.
My final config section in web.config is as follows:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <reflection-optimizer use="false" /> <session-factory> <property name="connection.connection_string">Data Source=your_server;Database=Northwind;UID=username;pwd=password;</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="show_sql">false</property> <property name="connection.release_mode">auto</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> <property name="current_session_context_class">managed_web</property> <!-- Mapping assemblies --> <!-- Can't map it for Fluent NHibernate here; instead, load the mapping assembly in Global.asax.cs. If you're still using HBMs, you can use the mapping here or pass the assembly via Global.asax.cs as well, just like you can do with the Fluent NHibernate assembly(s). --> <!-- mapping assembly="Northwind.Data" --> </session-factory> </hibernate-configuration> - Remove the
- Set the app to use medium trust by adding
<trust level="Medium" />to the system.web section. - Don’t forget to update the NH config with your Northwind database info.
- If you want to make use of the WCF part of the Northwind sample app, you’ll need to manually add the
AllowPartiallyTrustedCallersattribute to the Northwind.Wcf\Properties\AssemblyInfo.cs file. - Set Northwind.Web as the startup project, rebuild and run.
That’s it, the Northwind sample app should now run successfully in medium trust.
