2

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…

9
  • try adding using System.Net; Commented Aug 1, 2017 at 11:28
  • using System.Net; doesn't make any difference. Commented Aug 1, 2017 at 12:21
  • Try calling Wait by adding the parens: Wait() Commented Aug 1, 2017 at 12:26
  • maybe you can salvage something from this example: github.com/4c74356b41/tryh4rder/blob/master/sharpito/run.csx Commented Aug 1, 2017 at 12:34
  • 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. Commented Aug 1, 2017 at 12:40

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, not passing log was the problem. The function compiles now. I knew it should be something obvious...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.