探索 Windows Azure 中的命令行与非 .NET 语言运行
1. 运行命令行程序的问题与解决
在尝试使用代码启动 cmd.exe
实例并传递参数、读取输出时,可能会遇到问题。例如,在 Dev Fabric 下运行网站,输入命令(如 dir
)并回车,输出的不是文件列表,而是安全异常。这是因为在沙盒环境中,Windows Azure 里的托管代码默认以中等信任级别运行,会阻止以编程方式创建进程。
要解决这个问题,需要启用完全信任/本机代码执行,以放宽安全限制。具体操作如下:
打开 ServiceDefinition.csdef
文件,将 enableNativeCodeExecution
属性设置为 true
,示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="NativeCodeCmd" xmlns=
"http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole1" enableNativeCodeExecution="true">
<InputEndpoints>
<InputEndpoint name="HttpIn" protocol="http" por