Reputation: 29
I have to set up a function app in Azure portal and need to access via my .NET Core application (API).
Mainly I need a function in which have to pass 3 parameters to function app (from C# code) and accept return value which should be in datatable format.
Since I am very new to this, I don't know much about the feasibility and implementation techniques. If someone explains with detailed examples it will be really helpful.
Thanks in advance.
Upvotes: 2
Views: 2883
Reputation: 22523
Here is the example how you could call your azure function into the .net core API controller.
I have a simple azure function which return a name and email once its called. Let's see the below example:
public class InvokeAzureFunctionController : ApiController
{
// GET api/<controller>
public async System.Threading.Tasks.Task<IEnumerable<object>> GetAsync()
{
HttpClient _client = new HttpClient();
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, "http://localhost:7071/api/FunctionForController");
HttpResponseMessage response = await _client.SendAsync(newRequest);
dynamic responseResutls = await response.Content.ReadAsAsync<dynamic>();
return responseResutls;
}
}
Note: Just replace your local host and put azure portal
API URL
Test Function For Controller Invocation:
public static class FunctionForController
{
[FunctionName("FunctionForController")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
if (name == null)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
}
ContactInformation objContact = new ContactInformation();
objContact.Name = "From Azure Function";
objContact.Email = "[email protected]";
return req.CreateResponse(HttpStatusCode.OK, objContact);
}
}
Simple ContactInformation Class I have Used:
public class ContactInformation
{
public string Name { get; set; }
public string Email { get; set; }
}
PostMan Test:
I have called the controller action
from Post Man and its successfully return data from my local azure function through the local controller action
. See the screen shot below:
Hope you understand. Just plug and play now.
Upvotes: 3
Reputation: 5634
You can create HTTP Triggered Azure Function. HTTP Triggered Azure Function has a public URL which can be used by your application for calling the Azure Function.
Then you can send the parameters in either GET or POST request as per need of your application.
The Azure Function will return an HTTP response.
Refer this documentation page for more details.
Upvotes: 0