Pergunta

I am running a .NET 4.0 web application locally using the Visual Studio Development Server (built in web server with VS2010), and for the last couple months, my StructureMap bootstrapper file has worked perfectly.

I'm using StructureMap 2.6.1

I have not changed the Bootstrapper file or the Web.config file, and suddenly, I'm getting this strange error when trying to start up my web application.

Here is the error being thrown from the website:

error

it's a bit tough to read. It says:

Security Exception
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 Failed.

here is the code in my bootstrapper file:

public class BootStrapper
{
    public static void ConfigureStructureMap()
    {            
        ObjectFactory.Initialize(x =>
        {
            x.AddRegistry<ProductRegistry>();
        });
    }   
}

public class ProductRegistry : Registry
{
    public ProductRegistry()
    {

        For<IFirmRepository>()
            .Use<FirmRepository>().Ctor<string>("connectionString").Is(ConfigurationManager.ConnectionStrings["FeesAndFlows"].ConnectionString);

        For<ICryptographyService>()
            .Use<Rijndael>();

        For<IUserRepository>()
            .Use<UserRepository>().Ctor<string>("connectionString").Is(ConfigurationManager.ConnectionStrings["FeesAndFlows"].ConnectionString);

        For<IAuthenticationService>()
            .Use<AuthenticationService>();

        For<ILogger>()
            .Use<DatabaseLogger>();

    }
}

The error is being thrown on this line:

x.AddRegistry<ProductRegistry>();

I've already tried adding each of these lines to my Web.config file, one at a time, and they didn't fix the problem:

<trust level="Full" />

and

<securityPolicy>
    <trustLevel name="Full" policyFile="internal"/>
</securityPolicy>

Does anyone have any ideas or might have heard of a problem like this? It's pretty important, b/c without StructureMap starting up correctly, my entire application will not run.

Thanks, Mike

UPDATE:

Ok, so it appears my problem is local. Other developers here can download the code, and runs it just fine on their local machines. Weird. They bootstrap StructurMap just fine and all instances are resolved...

Any ideas on why just my machine can't bootstrap StructureMap when running in debug mode locally for my web project?

Foi útil?

Solução 2

Okay, so this was the problem. The StructureMap.dll was blocked by Win 7. I don't know how is become blocked or where it became blocked, but apparently, when I downloaded the StructureMap.zip file onto my system, the .zip file was blocked, which in turn, led to all the items extracted from the .zip file being blocked as well.

Every time I unblocked it, it went back to blocked when I tried to run the web app.

The way I fixed it was to go back to the original .zip file, unblock it, extract it, and then replace my StruectureMap.dll reference with one that was not blocked.

Insane.

I don't even know WHAT causes file to suddenly become blocked or what process in Windows 7 determines what file(s) should be blocked, but this strange operating system "feature" cost me a day's worth of work.

Outras dicas

Are you running the code from a network drive by any chance (ie is your documents folder redirected onto a network drive)? Are you in an enterprise environment running on a domain?

If the former it's likely that the code is running in the intranet security context. If the former isn't true but the later is, then its quite possible that a network administrator has changed the enterprise wide CAS policy.

I had the exact same issue at the exact same place, ObjectFactory.Initialize:

Server Error in '/X.ServiceHost' Application.

Inheritance security rules violated while overriding member: 'StructureMap.StructureMapException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.TypeLoadException: Inheritance security rules violated while overriding member: 'StructureMap.StructureMapException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

Getting the latest StructureMap package from NuGet (2.6.4.1) fixed the issue.

StructureMap NuGet Package

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top