gpt4 book ai didi

c# - 使用 HttpClient 在 Azure Function 中给出 'Script compilation failed'

转载 作者:行者123 更新时间:2023-11-30 20:30:01 24 4
gpt4 key购买 nike

我在编写 Azure 函数时遇到了一个奇怪的问题。我使用 HttpClient 从 Web 服务获取数据,但是当我运行该函数时,我收到“脚本编译失败”的消息。为了隔离问题,我编写了一个新的 Azure 函数,在其中仅检索 HTML 响应:

using System;
using System.Net.Http;
using System.Threading.Tasks;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
CallHttpClient().Wait;
}

public static async Task CallHttpClient()
{
using (var httpClient = new HttpClient())
{
var str = await httpClient.GetStringAsync("https://www.google.com");
log.Info(str);
}
}

我正在使用 Azure 门户。调用日志给我这个错误:

执行函数时出现异常:Functions.TestFunctionAsync

Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.TestFunctionAsync ---> Microsoft.CodeAnalysis.Scripting.CompilationErrorException : Script compilation failed. at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.CreateFunctionTarget(CancellationToken cancellationToken) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 343 at async Microsoft.Azure.WebJobs.Script.Description.FunctionLoader`1.GetFunctionTargetAsync[T](Int32 attemptCount) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\FunctionLoader.cs : 0 at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.GetFunctionTargetAsync() at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 192 at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.InvokeCore(Object[] parameters,FunctionInvocationContext context) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 260 at async Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\FunctionInvokerBase.cs : 171 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.InvokeAsync[TReflected](Object[] arguments) at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance) at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,ParameterHelper parameterHelper,TraceWriter traceWriter,ILogger logger,CancellationTokenSou…

最佳答案

您的示例中有两个编译错误:Wait 之后缺少 () 且未将 log 作为参数传递给 CallHttpClient.

您根本不应该使用 Wait(),而应使 Run 异步。

这是一个正确的版本:

public static async Task Run(TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
await CallHttpClient(log);
}

public static async Task CallHttpClient(TraceWriter log)
{
using (var httpClient = new HttpClient())
{
var str = await httpClient.GetStringAsync("https://www.google.com");
log.Info(str);
}
}

关于c# - 使用 HttpClient 在 Azure Function 中给出 'Script compilation failed',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45436486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com