ごった煮

色々な事を書いてます

ASP.NET MVC に Application Insights を導入すると、App Service 上で動かない時がある

ASP.NET MVC (not dotnet core) に Application Insights を NuGet から入れた際にハマったので、備忘録を残します。

何が起きたのか

下記のステップで既存の ASP.NET MVC アプリに Application Insights を入れてみました。

  1. NuGet から Microsoft.ApplicationInsights.DependencyCollector をインストール
  2. NuGet から Microsoft.ApplicationInsights.Web をインストール

1 の状態では、App Service 上でも動きました。

問題は、2の状態で発生しました。

発生したエラー

An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.

This application defines configuration in the system.web/httpModules section.

はてさて

なぜこんなエラーが起きたのか

NuGet から、 Microsoft.ApplicationInsights.Web をインストールした際、Web.config の system.web/httpModules のセクションに下記の設定が自動で差し込まれていました。

    <httpModules>
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>

使っていた App Service は Managed pipeline versionIntegrated だった為、system.web/httpModules のセクションが使えず、エラーになっていました。

Managed pipeline version を、Classic に変更すれば、該当の Web.config でも問題なく動作します。

まとめ

App Service の初期設定は、Integrated なので、注意が必要です。

今は、.NET Framework で新規のコードを書くことは減ったかもしれませんが、既存のコードに導入するといったことは往々にしてあるかと思います。 その際はご注意を