Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Inheritance security rules violated" when using HttpSelfHostServer and IDependencyResolver

I'm trying to use the HttpSelfHostServer to self-host an ASP.NET MVC 4 WebAPI. Everything is fine util I try to add a custom dependency resolver. (Ultimately this will use StructureMap, but I haven't even reached that point yet). If I try to instantiate a custom resolver, I get the following exception when launching the server:

TypeLoadException: Inheritance security rules violated by type: 'System.Web.Mvc.CompareAttribute'. Derived types must either match the security accessibility of the base type or be less accessible.

The code is as follows:

public class CustomDependencyResolver : IDependencyResolver
{
    public object GetService( Type serviceType )
    {
        return null;
    }

    public IEnumerable<object> GetServices( Type serviceType )
    {
        return null;
    }
}

...

// To trigger the exception, all I need to do is instantiate the custom resolver.
var dependencyResolver = new CustomDependencyResolver();

// Exception is thrown when I create the server:
var server = new HttpSelfHostServer( _config );

Note that I don't have to do anything with the resolver - it is simp,y the act of instantiating it which triggers the failure later.

Bizarrely enough, this exception only happens in debug (F5) - if I run via Ctrl+F5, it all works fine.

Any thoughts on how to resolve this issue?

Stacktrace:

mscorlib.dll!System.Reflection.RuntimeAssembly.GetExportedTypes() + 0x27 bytes  
System.Web.Http.dll!System.Web.Http.Dispatcher.HttpControllerTypeCacheUtil.FilterTypesInAssemblies(System.Web.Http.Dispatcher.IBuildManager buildManager, System.Predicate<System.Type> predicate) + 0x104 bytes    
System.Web.Http.dll!System.Web.Http.Dispatcher.HttpControllerTypeCacheUtil.GetFilteredTypesFromAssemblies(string cacheName, System.Predicate<System.Type> predicate, System.Web.Http.Dispatcher.IBuildManager buildManager) + 0x76 bytes    
System.Web.Http.dll!System.Web.Http.Dispatcher.HttpControllerTypeCache.InitializeCache() + 0x58 bytes   
System.Web.Http.dll!System.Web.Http.Dispatcher.HttpControllerTypeCache.HttpControllerTypeCache(System.Web.Http.HttpConfiguration configuration) + 0x96 bytes    
System.Web.Http.dll!System.Web.Http.Dispatcher.DefaultHttpControllerFactory.DefaultHttpControllerFactory(System.Web.Http.HttpConfiguration configuration) + 0x96 bytes  
System.Web.Http.dll!System.Web.Http.Services.DefaultServiceResolver..ctor.AnonymousMethod__0(System.Web.Http.HttpConfiguration config) + 0x30 bytes 
System.Web.Http.dll!System.Web.Http.Services.DefaultServiceResolver.GetService(System.Type t) + 0x57 bytes  
System.Web.Http.dll!System.Web.Http.Services.DependencyResolver.GetService(System.Type serviceType) + 0xd3 bytes    
System.Web.Http.dll!System.Web.Http.DependencyResolverExtensions.GetService<System.Web.Http.Dispatcher.IHttpControllerFactory>(System.Web.Http.Services.DependencyResolver resolver) + 0x6a bytes   
System.Web.Http.dll!System.Web.Http.DependencyResolverExtensions.GetServiceOrThrow<System.Web.Http.Dispatcher.IHttpControllerFactory>(System.Web.Http.Services.DependencyResolver resolver) + 0x5b bytes    
System.Web.Http.dll!System.Web.Http.DependencyResolverExtensions.GetHttpControllerFactory(System.Web.Http.Services.DependencyResolver resolver) + 0x25 bytes    
System.Web.Http.dll!System.Web.Http.Dispatcher.HttpControllerDispatcher.HttpControllerDispatcher(System.Web.Http.HttpConfiguration configuration) + 0x77 bytes  
System.Web.Http.SelfHost.dll!System.Web.Http.SelfHost.HttpSelfHostServer.HttpSelfHostServer(System.Web.Http.SelfHost.HttpSelfHostConfiguration configuration) + 0x62 bytes  
WebApi.Host.dll!My.WebApi.Host.Server.Listen() Line 33 + 0x1b bytes C#
Services.TrialBalance.TestHarness.exe!Digita.AccountsPro.Services.TrialBalance.TestHarness.Program.Main() Line 21 + 0xa bytes   C#
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6d bytes    
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2a bytes  
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes    
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
[Native to Managed Transition]  
like image 445
stusmith Avatar asked Apr 18 '12 11:04

stusmith


1 Answers

Finally found the answer; hence answering my own question.

Turns out there are two IDependencyResolver: one in System.Web.Http.Services, and one in System.Web.Mvc.

Both compile and run in non-debug.

System.Web.Http.Services.IDependencyResolver is the correct one.

System.Web.Mvc.IDependencyResolver seems to cause problems.

like image 98
stusmith Avatar answered Sep 16 '22 18:09

stusmith