startup.cs上的IIS 8.5配置(Azure webrole)

时间:2016-04-19 07:47:44

标签: c# asp.net azure iis

using (ServerManager serverManager = new ServerManager())
{
    var config = serverManager.GetApplicationHostConfiguration();

    // Configure IIS to compress for requests coming through proxies (CDN).
    var httpCompressionSection = config.GetSection("system.webServer/httpCompression");
    httpCompressionSection["noCompressionForHttp10"] = false;
    httpCompressionSection["noCompressionForProxies"] = false;
    httpCompressionSection["staticCompressionIgnoreHitFrequency"] = true;

    // Configure IIS threshold and time-period for compression
    var serverRuntimeSection = config.GetSection("system.webServer/serverRuntime");
    serverRuntimeSection["frequentHitThreshold"] = 1;
    serverRuntimeSection["frequentHitTimePeriod"] = new TimeSpan(24, 0, 0); // 1 day

    // Configure IIS to compress files beforehand
    var urlCompressionSection = config.GetSection("system.webServer/urlCompression");
    urlCompressionSection["dynamicCompressionBeforeCache"] = true;

    serverManager.CommitChanges();
}

当我在本地运行此代码时,一切都按预期工作(本地意味着Windows 10 + IIS express)。 当我将此webrole部署到Azure时,我的计算机(Windows Server 2012 R2 + IIS)崩溃。

我无法在部署的计算机上调试此代码(至少我不知道),所以我试图了解这段代码的错误。

1 个答案:

答案 0 :(得分:1)

您应该使用错误日志记录技术(如log4net)为您创建正在抛出的错误的日志。

尝试类似的内容:

    try {
        using (ServerManager serverManager = new ServerManager())
        {
            var config = serverManager.GetApplicationHostConfiguration();

            // Configure IIS to compress for requests coming through proxies (CDN).
            var httpCompressionSection = config.GetSection("system.webServer/httpCompression");
            httpCompressionSection["noCompressionForHttp10"] = false;
            httpCompressionSection["noCompressionForProxies"] = false;
            httpCompressionSection["staticCompressionIgnoreHitFrequency"] = true;

            // Configure IIS threshold and time-period for compression
            var serverRuntimeSection = config.GetSection("system.webServer/serverRuntime");
            serverRuntimeSection["frequentHitThreshold"] = 1;
            serverRuntimeSection["frequentHitTimePeriod"] = new TimeSpan(24, 0, 0); // 1 day

            // Configure IIS to compress files beforehand
            var urlCompressionSection = config.GetSection("system.webServer/urlCompression");
            urlCompressionSection["dynamicCompressionBeforeCache"] = true;

            serverManager.CommitChanges();
        }
    } catch (Exception e)
    {
        _log.ErrorFormat("Exception thrown during configuration! : {0}", e.ToString());
    }