2012-04-18 10 views
8

HttpSelfHostServerを使用して、ASP.NET MVC 4 WebAPIを自己ホストしようとしています。すべてがうまくいきます。カスタムの依存関係リゾルバを追加しようとしています。 (最終的にこれは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

+0

アップデート:カスタム依存リゾルバを作成する必要はなく、 'var type = typeof(IDependencyResolver);を実行するだけでこのエラーが発生します。 – stusmith

答えて

5

は最後に答えを見つけました。それで私自身の質問に答える。

IDependencyResolverの2つがあります.1つはSystem.Web.Http.Servicesにあり、1つはSystem.Web.Mvcにあります。

非デバッグでコンパイルして実行します。

System.Web.Http.Services.IDependencyResolverは、正しい1つですです。

System.Web.Mvc.IDependencyResolverは問題を引き起こすようです。

+0

それは私にとっても大きな驚きでした:) –

+0

+1。パラレルワールドへようこそ:) – Aliostad

+1

HttpSelfHostServerを使用しようとすると、この問題に直面しています。それは、HttpConfigurationクラスで使用されている同じIDependencyResolver IDです。しかし私は独自のカスタムDependencyResolverを作成していません。何か案は? –

関連する問題