views:

354

answers:

3

Howdy all. I am trying to solve a problem which is apparently not uncommon and I'm not sure how to find how this was resolved for folks. When I run StructureMap on my machine through IIS I get an exception and it looks like this:

**Description**: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
**Exception Details**: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

This question has come up here at SO (http://stackoverflow.com/questions/784666/), in the comments on this blog post and a year or so ago on the structuremap mailing list.

My problem is not running it in a foreign hosting environment. I can't even get it to run on my own box (IIS 7.5, Win7 RC, .NET 3.5). I have tried to configure the site to use a custom policy file and the FileIOPermission is marked to have unrestricted access...no dice. If anyone has some tips or a link it would be greatly appreciated.

Update So there is no way that this is the best way to solve the problem, but after digging around and looking into what Joshua mentioned, these are the things I had to do to get it working: StructureMap, Code Access Security and a Bad Solution to a Problem. A better solution would be appreciated.

A: 

This is a bug, and has been fixed in the trunk. It will be included in the 2.6+ releases. Some earlier versions of StructureMap would either attempt to unnecessarily write the dynamic assemblies to disk, or unnecessarily attempt to read from the filesystem.

If you are running in a restricted environment that does not allow access to full paths in the filesystem (ASP.NET), make sure to set IgnoreDefaultFile = true when you configure your container. Keep in mind this will disable the ability to load XML configuration from StructureMap.config.

Joshua Flanagan
I'm using version 2.5.3. I did not build it from the source.
Mallioch
+1  A: 

For what it's worth, I ran into this same issue where I had full control over the box and even set all the permissions to full trust. With IIS 7.5, I had to change the identity used for the specific application pool to NetworkService instead of ApplicationPoolIdentity. Once I restarted IIS, it worked.

Mike
This solved the problem for me.
Mike Stockdale
A: 

Using the official StructureMap 2.5.4 build on Windows 7 with IIS 7.5 I still encountered this problem. Mallioch's change

  ObjectFactory.Initialize(x =>
    {
      x.UseDefaultStructureMapConfigFile = false;
      x.IgnoreStructureMapConfig = true;

was necessary to resolve the FileIOPermission exception but I then received Request for the permission of type ‘System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed. which I resolved using Mike's solution ( for which I've created a step-by-step visualization ).

Handcraftsman