无法调试 Azure 函数 - Visual Studio 2017

Can't Debug Azure Function - Visual Studio 2017

我使用带有 Azure Functions V1 的 Azure Functions 工具创建了一个全新的项目:

然后我按 F5 并得到黄色闪电,然后是一条错误消息:

"An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = WebScriptHostManager (DelegateActivator), Services = [Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned ---> The type initializer for 'Microsoft.Azure.WebJobs.Script.Utility' threw an exception. (See inner exception for details.)"

我的设置是:

Visual Studio 2017 - 15.6.7

Azure SDK 功能 1.0.12

Azure 函数 V1

Azure Web 作业工具 - 15.0.40215.0

Azure CLI 1.0.12

这是我的 VS 控制台的输出:

func.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\func.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Autofac.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.IO.Abstractions.dll'. Cannot find or open the PDB file.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Newtonsoft.Json.0.0.0__30ad4fe6b2a6aeed\Newtonsoft.Json.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\ARMClient.Authentication.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\ARMClient.Library.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Colors.Net.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Azure.WebJobs.Script.dll'. Cannot find or open the PDB file.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Extensions.Logging.Abstractions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Azure.WebJobs.Host.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Azure.WebJobs.Script.WebHost.dll'. Cannot find or open the PDB file.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\FluentCommandLineParser.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'Anonymously Hosted DynamicMethods Assembly'. 
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Web.Http.SelfHost.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Web.Http.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Net.Http.Formatting.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Web.Http.Cors.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Ignite.SharpNetSH.dll'. Cannot find or open the PDB file.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.IdentityModel\v4.0_4.0.0.0__b77a5c561934e089\System.IdentityModel.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\SMDiagnostics\v4.0_4.0.0.0__b77a5c561934e089\SMDiagnostics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Internals\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Internals.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Autofac.Integration.WebApi.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Generic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Azure.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Pusher.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Stripe.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Trello.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.DynamicsCRM.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.MailChimp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Slack.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Bitbucket.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Dropbox.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.WordPress.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.GitHub.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.Salesforce.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Azure.AppService.Proxy.Common.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Azure.WebJobs.Extensions.Http.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Receivers.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.Azure.WebJobs.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.WindowsAzure.Storage.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\Microsoft.AspNet.WebHooks.Common.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Reactive.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Reactive.Interfaces.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'func.exe' (CLR v4.0.30319: func.exe): Loaded 'C:\Users\Stanley\AppData\Local\Azure.Functions.Cli.0.12\System.Reactive.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[15204] func.exe' has exited with code -1073741510 (0xc000013a).

我试过: 卸载并重新安装 Azure Dev 工作负载以及 CLI 工具 我还没有尝试过 unistalling/reinstaling VS,因为这至少需要半天时间。

我怀疑问题出在 Azure CLI

我对 Azure Functions + VS 感到非常沮丧。 让他妈的东西正常工作绝对是一场噩梦!

我遇到了同样的错误,并将其追溯到 Newtonsoft.Json.dll 版本中的差异。我通过从 GAC 卸载 Newtonsoft.Json 版本 10.0 解决了这个问题。 (Azure Functions 需要 9.0。)

因此,在 Visual Studio 命令提示符 运行 gacutil /l | findstr Newtonsoft 中,然后 gacutil /u 在您发现高于 9.0 的每个 DLL 版本上。

如果这不起作用,我建议 运行从源代码中使用 Azure Functions 工具 (func.exe)。从 https://github.com/Azure/azure-functions-core-tools/ 克隆它,检查 v1.x 分支 ,运行 Azure.Functions.Cli 项目,使用 host start --pause-on-error 作为命令-line 参数和 Azure 函数的输出文件夹作为工作目录。如果您在调试器中 运行 设置为在所有 CLR 异常时中断,您应该能够很快找到您的问题。

问题已解决!

感谢 Jerry Liu,我成功调试了 Azure Function Core Tools 的源代码。

错误信息原来是Newtonsoft相关的:

"Could not load type 'Newtonsoft.Json.Converters.ExpandoObjectConverter' from assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'."

所以我只是 运行 将源代码更新到最新版本的 Newtonsoft。 然后我将重建的源代码复制到:

C:\Users[我的用户名]\AppData\Local\AzureFunctionsTools\Releases.0.12.1\cli

现在可以使用了。

感谢所有参与帮助我解决这个问题的人!

适用于 v2 而不是 v1。我必须进入 .csproj 文件并将 AzureFunctionsVersion 添加到 PropertyGroup

<PropertyGroup>
  <TargetFramework>net461</TargetFramework>
  <AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>