0

I'm trying to create an Azure Function with Docker. When I create a Function with func new, it works fine and when I go to http://localhost:8182/api/HttpTriggerCSharp?name=John I get the message

Hello, John

Now I'm trying to run the same project but I changed the code. The previous code was this:

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

Now this is my new code:

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

I build the container then I run it and I get this message: enter image description here

I've already checked my code with VS Code (I did it in Sublime Text so I didn't have code checking) and all the problems it finds are the same error: enter image description here

And my code has "errors" everywhere: enter image description here

How can I solve this?

4
  • Are all the required packages included? Commented May 14, 2018 at 5:32
  • How can I tell if I have included all the packages? VS Code says that some assembly references are missing but I see no missing "usings" Commented May 14, 2018 at 12:06
  • Check out this link: learn.microsoft.com/en-us/nuget/quickstart/… Commented May 14, 2018 at 12:15
  • I'm not using Visual Studio. However, it worked following the instructions that Jerry gave me. Thanks! Commented May 14, 2018 at 19:55

1 Answer 1

1

You are using v2 function core tools(based on .net core), while the code you changed is targeted at v1(.net framework).

So you have two choices:

  1. Uninstall v2 and use function core tool v1.
  2. Modify your code to make it work in v2 runtime.

Here the code for you to refer. GenerateFibonacciTerms method needs no change.

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

2 Comments

Hi Jerry, Thanks for answering again! I'll try this when I get home and I'll tell you.
I made the modifications to my code and it worked, thank you so much!!

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.