2012-04-18 83 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

相關問題