使用 MEF 加载 dll 时的 System.Security.Permissions.FileIOPermission
我正在尝试将一些 dll 加载到 ASP.NET MVC 应用程序中的 MEF DirectoryCatalog 中:
var catalog = new DirectoryCatalog(HttpRuntime.BinDirectory, "Toptable.Mobile.*.dll");
当我通过 Cassini Web 服务器(即 F5)运行应用程序时,一切运行正常,但是当托管在 IIS(7) 中时,我收到以下异常:
[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +54
System.IO.Path.GetFullPath(String path) +193
System.ComponentModel.Composition.Hosting.DirectoryCatalog.GetFullPath(String path) +267
System.ComponentModel.Composition.Hosting.DirectoryCatalog.Initialize(String path, String searchPattern) +144
System.ComponentModel.Composition.Hosting.DirectoryCatalog..ctor(String path, String searchPattern) +166
Toptable.Mobile.MvcApplication.Application_Start() in C:\Dev\Toptable\Toptable.Mobile\Toptable.Mobile.Web\Global.asax.cs:74
应用程序的 .NET 信任级别对于站点和全局均设置为“完全”,并且我已将 web.config (system.web/trust) 中的信任级别设置为“完全”。对于可能导致这种情况的原因已经没有想法了。有什么建议吗?
I am trying to load some dll's into a MEF DirectoryCatalog within an ASP.NET MVC application:
var catalog = new DirectoryCatalog(HttpRuntime.BinDirectory, "Toptable.Mobile.*.dll");
When I run the app through the Cassini web server (i.e. F5) everything runs fine however when hosted in IIS(7) I get the following exception:
[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +54
System.IO.Path.GetFullPath(String path) +193
System.ComponentModel.Composition.Hosting.DirectoryCatalog.GetFullPath(String path) +267
System.ComponentModel.Composition.Hosting.DirectoryCatalog.Initialize(String path, String searchPattern) +144
System.ComponentModel.Composition.Hosting.DirectoryCatalog..ctor(String path, String searchPattern) +166
Toptable.Mobile.MvcApplication.Application_Start() in C:\Dev\Toptable\Toptable.Mobile\Toptable.Mobile.Web\Global.asax.cs:74
The .NET trust levels for the application are set to "Full" both for the site and globally and I have set the trust level in web.config (system.web/trust) to Full. Running out of ideas about what could be causing it. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确定完全信任,则更有可能是路径/权限错误。您确定您所走的路可以通行吗?
if youre sure youre in full trust is more likely a path/permissions error. are you sure the path youre after is accessible?
该异常使得您看起来实际上并没有完全信任地运行。检查 AppDomain.IsFullyTrusted 属性以验证您是否是。如果是,则可能是 ASP.NET 在 IIS 下以操作系统用户身份运行且权限较低,并且无权调用 GetFullPath。作为解决方法,您可以自己扫描目录,为每个 DLL 创建一个 AssemblyCatalog,并将所有 AssemblyCatalog 添加到 AggregateCatalog 中。
The exception makes it look like you aren't actually running in full trust. Check the AppDomain.IsFullyTrusted property to verify that you are. If you are, then it may be the case that ASP.NET is running as an OS user with reduced privileges under IIS, and it doesn't have permission to call GetFullPath. As a workaround you could scan the directory yourself, create an AssemblyCatalog for each DLL, and add all of the AssemblyCatalogs into an AggregateCatalog.