2012-04-18 128 views
8

我试图使用HttpSelfHostServer自承载ASP.NET MVC 4 WebAPI。一切都很好util我尝试添加一个自定义的依赖解析器。 (最终这将使用StructureMap,但我还没有达到那一点)。“使用HttpSelfHostServer和IDependencyResolver时违反了”继承安全规则“

TypeLoadException:按类型违反继承安全规则: “System.Web.Mvc.CompareAttribute”如果我尝试实例化自定义解析,我启动服务器时出现以下情况例外。派生类型必须匹配基类型的安全性可访问性或不易访问的 。

的代码如下:

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); 

请注意,我没有跟解析器做任何事情 - 这是SIMP,Y实例它后来引发故障的行为。奇怪的是,这个异常只发生在调试(F5) - 如果我通过Ctrl + F5运行,它一切正常。

有关如何解决此问题的任何想法?

堆栈跟踪:

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] 
+0

什么是堆栈跟踪? – SLaks 2012-04-18 11:15:24

+0

更新:我甚至不需要创建自定义依赖关系解析器,只是执行'var type = typeof(IDependencyResolver);'触发这个错误。 – stusmith 2012-04-18 11:16:16

回答

5

终于找到了答案,因此回答了我自己的问题。

原来有两个IDependencyResolver:一个在System.Web.Http.Services,一个在System.Web.Mvc

都在非调试中编译和运行。

System.Web.Http.Services.IDependencyResolver是正确的一个

System.Web.Mvc.IDependencyResolver似乎会导致问题。

+0

这对我来说也是很大的惊喜:) – 2012-04-18 14:12:30

+0

+1。欢迎来到并行世界:) – Aliostad 2012-04-18 22:46:31

+1

当我尝试使用HttpSelfHostServer时,我正面临着这个问题。看起来像HttpConfiguration类使用的IDependencyResolver id相同。但我不创建我自己的自定义DependencyResolver。有任何想法吗? – 2015-05-21 17:06:20