- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 HttpSelfHostServer 自托管 ASP.NET MVC 4 WebAPI。一切都很好,我尝试添加自定义依赖解析器。 (最终这将使用 StructureMap,但我还没有达到这一点)。如果我尝试实例化自定义解析器,则在启动服务器时会出现以下异常:
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.
代码如下:
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 );
请注意,我不必对解析器执行任何操作 - 实例化它的行为很简单,稍后会触发失败。
奇怪的是,这个异常只发生在调试(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]
最佳答案
终于找到答案了;因此回答我自己的问题。
结果有两个 IDependencyResolver
:一个在 System.Web.Http.Services
中,另一个在 System.Web.Mvc
中。
均以非调试方式编译和运行。
System.Web.Http.Services.IDependencyResolver
是正确的。
System.Web.Mvc.IDependencyResolver
似乎会导致问题。
关于asp.net-mvc - 使用 HttpSelfHostServer 和 IDependencyResolver 时为 "Inheritance security rules violated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10208188/
我正在设计对遗留 ASP.NET 应用程序的一些架构更改。我为模仿 ASP.NET MVC 的 IDependencyResolver 的依赖关系解析制作了一些类的原型(prototype)。我不会发
我有一点误解:当我们实现 IDependencyResolver 时,这意味着我们可以在项目的每个部分使用 DI。因此,即使我不从 DefaultControllerFactory 继承,我也可以构建
为什么 IDependencyResolver 与 System.Web 程序集(Mvc 或 Http ) 在 .NET 框架中? DI 系统的目标不是应该提供一种不可知的方式来为客户提供依赖项吗?如
我正在开发一个 Web Api,我决定使用自定义 DependencyResolver。我引用 this [Dependency Injection for Web API Controllers]文
我在 MVC(基础书籍)中阅读了有关 IDependencyResolver 的内容,但我不知道 mvc 中的 DependencyResolver 到底是什么? 有人可以解释一下这些方法吗? 最佳答
我在 MVC(基础书籍)中阅读了有关 IDependencyResolver 的内容,但我不知道 mvc 中的 DependencyResolver 到底是什么? 有人可以解释一下这些方法吗? 最佳答
我们可以通过实现 IDependencyResolver 或扩展 DefaultControllerFactory 在 MVC 中进行 DI 我之前一直认为这两种不同的方式做起来没有太大的区别。 然而
各位! 我在使用 MVC3 IDependencyResolver 从 Unity 2.0 解析命名注册时遇到问题。 我将类型注册配置为 public class Implementations {
最近我遇到了这个讨论。 在 ASP.NET WebAPI 中,当我们需要集成自定义控制反转/依赖注入(inject)容器时,我们需要采用两种不同的策略: 实现 IHttpControllerActiv
在使用 Unity 进行依赖注入(inject)时,我对 MVC 的 IDependencyResolver.GetServices 的实现有疑问。 可以找到几个示例实现,其中大部分已经实现以解决 U
我正在尝试使用 HttpSelfHostServer 自托管 ASP.NET MVC 4 WebAPI。一切都很好,我尝试添加自定义依赖解析器。 (最终这将使用 StructureMap,但我还没有达
我读了 this article看到很多人评论说不要使用 CaSTLe Windsor 在 ASP.NET MVC3 中实现 IDependencyResolver 并坚持使用自定义 IControl
我是一名优秀的程序员,十分优秀!