c# - 如何在 C# Interactive 窗口中设置通常在 app.config 中定义的开关?

标签 c# .net exception roslyn

我正在尝试使用 C# Interactive REPL(来自 Roslyn)加载 DLL。它抛出以下异常:

"An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information."

DLL 不在网络位置,所以我不知道为什么会抛出这个异常。无论如何,没有我可以修改的 app.config 文件(据我所知),所以有人知道我该如何解决这个错误吗?

更新:

我已经 unblocked程序集,这使得这个异常消失了。但我仍然想回答更一般的问题,即如何在 C# Interactive 窗口中设置通常在 app.config 中定义的开关?

最佳答案

有问题的程序集在您尝试加载时不需要位于网络位置。您看到的消息警告您,您尝试加载的程序集是(在某个时候)从 Internet 下载的,并且当前标记为不受信任。

对于您的元问题,大多数配置设置映射到一个或多个代码结构(API、属性等),允许您根据需要以编程方式调整设置。如何修改特定设置取决于设置及其影响的 API。在某些情况下,您可能需要编写代码来配置运行时,而在其他情况下,您可能必须配置 IIS、日志记录和跟踪、WCF 端点等。

如果您阅读上面链接的文章,您会发现以下内容:

If an application has been copied from the web, it is flagged by Windows as being a web application, even if it resides on the local computer. You can change that designation by changing the file properties, or you can use the element to grant the assembly full trust. As an alternative, you can use the UnsafeLoadFrom method to load a local assembly that the operating system has flagged as having been loaded from the web.

使用 UnsafeLoadFrom 通常比让您的应用从不受信任的来源下载任意程序集并让它在您的应用内以完全信任的方式运行更安全。

文档继续说:

This element is typically used in the application configuration file, but can be used in other configuration files depending upon the context. For more information, see the article More Implicit Uses of CAS Policy: loadFromRemoteSources in the .NET Security blog.

请仔细阅读后面的文档 - 它说明了您需要编写代码才能实现与设置 loadFromRemoteSources 配置标志相同的结果:但是,请不要低估将任意代码加载到您的应用程序中的危险!

关于c# - 如何在 C# Interactive 窗口中设置通常在 app.config 中定义的开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15119750/

相关文章:

c# - 为什么DataGrid调用集合的IndexOf传递类型ItemcControl.ItemInfo的对象?

c# - WPF 媒体格式

java.net.URISyntaxException

.net - Try Catch 错误处理的最佳实践

c# - 如何使用 LINQ 获取值(value)最低的项目?

c# - Asp.net 在 GridView.AllowPaging ="true"时生成错误的 SQL 请求

c# - 如何使用内存映射文件进行进程间通信?

c# - 如何在 C# 中返回 HTTP 调用响应

c# - BinaryFormatter 异常

c# - 在 Form 上画多个圆