Azure Functions 脚本编译失败
Azure Functions Script Compilation Failed
我正在尝试使用 Docker 创建一个 Azure 函数。当我使用 func new
创建函数时,它工作正常,当我转到 http://localhost:8182/api/HttpTriggerCSharp?name=John 时,我收到消息
Hello, John
现在我正在尝试 运行 同一个项目,但我更改了代码。之前的代码是这样的:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
现在这是我的新代码:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// Parsing query parameters
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
log.Info("name = " + name);
string numberOfTerms = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "numberOfTerms", true) == 0)
.Value;
log.Info("name = " + numberOfTerms);
// Validating the parameters received
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(numberOfTerms))
{
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest,
"Please pass a name and the number of digits on the query string.");
return errorResponse;
}
int termsToShow;
try
{
termsToShow = Int32.Parse(numberOfTerms);
}
catch (FormatException e)
{
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest,
"The numberOfTerms parameter must be an integer!");
return errorResponse;
}
if (termsToShow < 0 || termsToShow > 100) {
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest,
"Please pass a numberOfTerms parameter between 0 and 100.");
return errorResponse;
}
// Building the response
string incompleteResponse = "Hello, " + name + ", you requested the first " + numberOfTerms + " terms of the Fibonacci series. Here they are: ";
string completeResponse = GenerateFibonacciTerms(incompleteResponse, termsToShow);
var response = req.CreateResponse(HttpStatusCode.OK, completeResponse);
// Returning the HTTP response with the string we created
log.Info("response = " + response);
return response;
}
public static string GenerateFibonacciTerms(string incompleteResponse, int termsToShow)
{
int a = 0;
int b = 1;
string temporalString = "";
for (int i = 0; i < termsToShow; i++)
{
int temp = a;
a = b;
b = temp + b;
temporalString = temporalString + temp.ToString() + " ";
}
string result = incompleteResponse + temporalString + "- That's it, have an awesome day!";
return result;
}
我构建容器,然后 运行 它并收到此消息:
我已经用 VS Code 检查了我的代码(我是在 Sublime Text 中检查的,所以我没有进行代码检查),它发现的所有问题都是同一个错误:
我的代码到处都是 "errors":
我该如何解决这个问题?
您使用的是v2功能核心工具(基于.net core),而您更改的代码是针对v1(.net framework)的。
所以你有两个选择:
- 卸载 v2 并使用功能核心工具 v1。
- 修改您的代码以使其在 v2 运行时中运行。
这里是代码供你参考。 GenerateFibonacciTerms
方法不需要改变。
log.Info("C# HTTP trigger function processed a request.");
// Parsing query parameters
string name = req.Query["name"];
log.Info("name = " + name);
string numberOfTerms = req.Query["numberOfTerms"];
log.Info("numberOfTerms = " + numberOfTerms);
// Validating the parameters received
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(numberOfTerms))
{
return new BadRequestObjectResult("Please pass a name and the number of digits on the query string.");
}
int termsToShow;
try
{
termsToShow = Int32.Parse(numberOfTerms);
}
catch (FormatException e)
{
return new BadRequestObjectResult("The numberOfTerms parameter must be an integer!");
}
if (termsToShow < 0 || termsToShow > 100) {
return new BadRequestObjectResult("Please pass a numberOfTerms parameter between 0 and 100.");
}
// Building the response
string incompleteResponse = "Hello, " + name + ", you requested the first " + numberOfTerms + " terms of the Fibonacci series. Here they are: ";
string completeResponse = GenerateFibonacciTerms(incompleteResponse, termsToShow);
var response = new OkObjectResult(completeResponse);
// Returning the HTTP response with the string we created
log.Info("response = " + response);
return response;
我正在尝试使用 Docker 创建一个 Azure 函数。当我使用 func new
创建函数时,它工作正常,当我转到 http://localhost:8182/api/HttpTriggerCSharp?name=John 时,我收到消息
Hello, John
现在我正在尝试 运行 同一个项目,但我更改了代码。之前的代码是这样的:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
现在这是我的新代码:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// Parsing query parameters
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
log.Info("name = " + name);
string numberOfTerms = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "numberOfTerms", true) == 0)
.Value;
log.Info("name = " + numberOfTerms);
// Validating the parameters received
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(numberOfTerms))
{
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest,
"Please pass a name and the number of digits on the query string.");
return errorResponse;
}
int termsToShow;
try
{
termsToShow = Int32.Parse(numberOfTerms);
}
catch (FormatException e)
{
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest,
"The numberOfTerms parameter must be an integer!");
return errorResponse;
}
if (termsToShow < 0 || termsToShow > 100) {
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest,
"Please pass a numberOfTerms parameter between 0 and 100.");
return errorResponse;
}
// Building the response
string incompleteResponse = "Hello, " + name + ", you requested the first " + numberOfTerms + " terms of the Fibonacci series. Here they are: ";
string completeResponse = GenerateFibonacciTerms(incompleteResponse, termsToShow);
var response = req.CreateResponse(HttpStatusCode.OK, completeResponse);
// Returning the HTTP response with the string we created
log.Info("response = " + response);
return response;
}
public static string GenerateFibonacciTerms(string incompleteResponse, int termsToShow)
{
int a = 0;
int b = 1;
string temporalString = "";
for (int i = 0; i < termsToShow; i++)
{
int temp = a;
a = b;
b = temp + b;
temporalString = temporalString + temp.ToString() + " ";
}
string result = incompleteResponse + temporalString + "- That's it, have an awesome day!";
return result;
}
我构建容器,然后 运行 它并收到此消息:
我已经用 VS Code 检查了我的代码(我是在 Sublime Text 中检查的,所以我没有进行代码检查),它发现的所有问题都是同一个错误:
我的代码到处都是 "errors":
我该如何解决这个问题?
您使用的是v2功能核心工具(基于.net core),而您更改的代码是针对v1(.net framework)的。
所以你有两个选择:
- 卸载 v2 并使用功能核心工具 v1。
- 修改您的代码以使其在 v2 运行时中运行。
这里是代码供你参考。 GenerateFibonacciTerms
方法不需要改变。
log.Info("C# HTTP trigger function processed a request.");
// Parsing query parameters
string name = req.Query["name"];
log.Info("name = " + name);
string numberOfTerms = req.Query["numberOfTerms"];
log.Info("numberOfTerms = " + numberOfTerms);
// Validating the parameters received
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(numberOfTerms))
{
return new BadRequestObjectResult("Please pass a name and the number of digits on the query string.");
}
int termsToShow;
try
{
termsToShow = Int32.Parse(numberOfTerms);
}
catch (FormatException e)
{
return new BadRequestObjectResult("The numberOfTerms parameter must be an integer!");
}
if (termsToShow < 0 || termsToShow > 100) {
return new BadRequestObjectResult("Please pass a numberOfTerms parameter between 0 and 100.");
}
// Building the response
string incompleteResponse = "Hello, " + name + ", you requested the first " + numberOfTerms + " terms of the Fibonacci series. Here they are: ";
string completeResponse = GenerateFibonacciTerms(incompleteResponse, termsToShow);
var response = new OkObjectResult(completeResponse);
// Returning the HTTP response with the string we created
log.Info("response = " + response);
return response;