2012-04-18 29 views
8

Tôi đang cố gắng sử dụng HttpSelfHostServer để tự lưu trữ ASP.NET MVC 4 WebAPI. Tất cả mọi thứ là tốt util Tôi cố gắng để thêm một người phụ thuộc giải quyết phụ thuộc. (Cuối cùng điều này sẽ sử dụng StructureMap, nhưng tôi vẫn chưa đạt đến điểm đó). Nếu tôi cố gắng để nhanh chóng một resolver tùy chỉnh, tôi nhận được ngoại lệ sau khi tung ra máy chủ:"Quy tắc bảo mật thừa kế vi phạm" khi sử dụng HttpSelfHostServer và IDependencyResolver

TypeLoadException: quy tắc bảo mật Inheritance vi phạm theo loại: 'System.Web.Mvc.CompareAttribute'. Các loại có nguồn gốc phải phù hợp với khả năng truy cập bảo mật của loại cơ sở hoặc ít truy cập hơn.

Mã này là như sau:

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

Lưu ý rằng tôi không phải làm bất cứ điều gì với resolver - đó là Simp, y hành động instantiating nó mà gây nên sự thất bại sau đó.

Kỳ lạ, ngoại lệ này chỉ xảy ra trong gỡ lỗi (F5) - nếu tôi chạy qua Ctrl + F5, tất cả đều hoạt động tốt.

Bất kỳ suy nghĩ nào về cách giải quyết vấn đề này?

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

Dấu vết ngăn xếp là gì? – SLaks

+0

Cập nhật: Tôi thậm chí không cần tạo trình phân giải phụ thuộc tùy chỉnh, chỉ cần thực hiện 'var type = typeof (IDependencyResolver);' kích hoạt lỗi này. – stusmith

Trả lời

5

Cuối cùng tìm thấy câu trả lời; do đó trả lời câu hỏi của riêng tôi.

Hóa ra có hai số IDependencyResolver: một trong số System.Web.Http.Services và một trong số System.Web.Mvc.

Cả hai biên dịch và chạy trong không gỡ lỗi.

System.Web.Http.Services.IDependencyResolverlà đúng.

System.Web.Mvc.IDependencyResolver dường như gây ra sự cố.

+0

Đó cũng là một bất ngờ lớn đối với tôi :) –

+0

+1. Chào mừng bạn đến với thế giới song song :) – Aliostad

+1

Tôi đang đối mặt với vấn đề này khi tôi cố gắng sử dụng HttpSelfHostServer. Có vẻ như cùng một ID IdependencyResolver được sử dụng bởi lớp HttpConfiguration. Nhưng tôi không tạo DependencyResolver tùy chỉnh của riêng mình. Bất kỳ ý tưởng? –

Các vấn đề liên quan