2
votes

I've got a strange problem writing an Azure Function. I use HttpClient to get data from a webservice but when I run the function, I get a 'Script compilation failed'. To isolate the problem, I've written an new Azure function in which I just retrieve a HTML response:

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);
    }
}

I'm using the Azure Portal. The Invocation log gives me this error:

Exception while executing function: 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…

1
try adding using System.Net;4c74356b41
using System.Net; doesn't make any difference.Robin Dijkstra
Try calling Wait by adding the parens: Wait()Crowcoder
maybe you can salvage something from this example: github.com/4c74356b41/tryh4rder/blob/master/sharpito/run.csx4c74356b41
If Run supports async as in the example linked by @4c74356b41 then you should definitely await instead of Wait() so as to not block on async.Crowcoder

1 Answers

4
votes

You have two compilation errors in your example: missing () after Wait and not passing log as parameter to CallHttpClient.

You shouldn't use Wait() at all, instead make Run async.

Here is a proper version:

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);
    }
}