つばろぐ

主に C#, .NET, Azure の備忘録です。たまに日記。

Visual Studio Tools for Azure Functionsでデバッグできない現象を解決する方法

仕事や趣味でAzure Functionsのアプリケーションを作ることがちょくちょくあります。
サーバを作るほどでも無いちょっとしたアプリケーションをホストするにはとても使い勝手がよいです。

Visual Studio 2015を使えば、Azure Functionsのローカル開発を行うことができます。詳しくはこちらをご覧ください。

Azure Functions のローカル開発 | ブチザッキ

通常、デバッグを実行するとlocalhostのWebサーバが起動し、Functionsの関数へのエンドポイントが実行されます。
しかし、いつからかデバッグできない状態に陥っていました。(Visual Studio 2017をインストールしてからかも?)

具体的にはlocalhostのWebサーバは起動するが、関数のエンドポイントが繋がらず 503 Service Unavailable となってしまいます。

f:id:tech-tsubaki:20170701205119j:plain

Warning: The filename 'appsettings.json' is deprecated. Rename it to local.settings.json

                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

Listening on http://localhost:7071/
Hit CTRL-C to exit...
[2017/07/01 11:30:42] Reading host configuration file 'D:\Functions\host.json'
[2017/07/01 11:30:42] Host configuration file read:
[2017/07/01 11:30:42] {
[2017/07/01 11:30:42]
[2017/07/01 11:30:42] }
[2017/07/01 11:30:42] Loaded custom extension: BotFrameworkConfiguration from ''
[2017/07/01 11:30:42] Loaded custom extension: SendGridConfiguration from ''
[2017/07/01 11:30:46] Generating 1 job function(s)
[2017/07/01 11:30:46] Starting Host (HostId=matvaio-1730734029, Version=1.0.11002.0, ProcessId=9884, Debug=False, Attempt=0)
[2017/07/01 11:30:46] Found the following functions:
[2017/07/01 11:30:46] Host.Functions.Hoge
[2017/07/01 11:30:46]
[2017/07/01 11:30:46] Job host started
The host is taking longer than expected to start.
The host is taking longer than expected to start.
The host is taking longer than expected to start.

The host is taking longer than expected to start.という出力が延々続いて、何もできない状態となります。
Visual Studio 2015の修復や、Visual Studio Tools for Azure Functionsの修復を何度か試しましたが、改善されず放置していました。
まぁVisual Studio 2017に開発ツールが出てくるのを待てばいいや、というスタンスでした。

しかしやはり不便だったので解決方法を調べてみると、Azure Functions CLIリポジトリにIssueがあがっていました。

github.com

このIssueで述べられていた内容としては、デバッグ時に使用するポート(既定では7071)が既にシステムに登録されているよ、という内容でした。
コマンドプロンプトを管理者権限で起動し、次のコマンドを入力しましょう。

netsh http delete urlacl url=http://+:7071/

私もこのコマンドを使用することでデバッグできない問題が解消されました。
これでエンドポイントにアクセスすることができ、デバッグ実行を行うことができるようになりました。

f:id:tech-tsubaki:20170701205401j:plain

Azureの開発ツールもGitHubで管理・公開されるようになり、不具合に関する情報が見えやすくなってきていますね。
もし同じ現象で困っている方の参考になればいいなーと思います。