当IIS作为CGI运行时,为什么C#配置系统无法初始化?

时间:2018-12-31 19:08:35

标签: c# iis cgi app-config application-pool

我有一个C#可执行文件,旨在作为CGI运行。

从命令行调用时的行为与作为CGI调用时的行为不同。

代码在这里:

using System;
using System.Configuration;

class Foo
{
    static void Main(string[] args)
    {
        //throw new Exception( "POINT-A: Seen through browser and at command line" );
        var appSettings = ConfigurationManager.AppSettings;
        throw new Exception("POINT-B:");
    }
}

当我从命令行运行它时,我得到了(预期):

Unhandled Exception: System.Exception: POINT-B:
   at Foo.Main(String[] args) in C:\Users\brian\My 
Stuff\MMG\Development\mra_query\src\simple.cs:line 10

当我以CGI浏览到它时,我得到了(这是意外的):

Unhandled Exception: System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.CheckIllegalCharacters(String[] str, Boolean onlyCheckExtras)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.AppDomainSetup.VerifyDir(String dir, Boolean normalize)
   at System.AppDomainSetup.get_ConfigurationFile()
   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
   at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
   at System.Configuration.ClientConfigurationSystem..ctor()
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at Foo.Main(String[] args) in C:\Users\brian\My Stuff\MMG\Development\mra_query\src\simple.cs:line 9

为什么从命令行调用时到达“ POINT-B”,但是作为CGI调用时出了点问题?

我有一个App.config文件,它与EXE驻留在同一文件夹中:

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
    <startup>   
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
    </startup>  
  <appSettings>  
    <add key="Setting1" value="May 5, 2014"/>  
    <add key="Setting2" value="May 6, 2014"/>  
  </appSettings>  
</configuration>

此配置文件是否需要放置在某些特殊位置以供IIS使用? IIS ApplicationPool是否在其中发挥了我没有考虑的作用?

该异常提到“路径中的非法字符”,但我很难理解其含义。似乎正在检查读取配置文件的权限。该文件位于以下所示的路径。路径中的那个空间有问题吗?

C:\Users\brian\My Stuff\MMG\Development\mra_query\cgiroot

非常感谢您的帮助。

0 个答案:

没有答案