2

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.

2 Answers 2

3

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:

enter image description here

Hope you understand. Just plug and play now.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Is there a way to generate C# code for calling AzureFunctions -- like SwaggerGen for JSON APIs?
Currently I am not aware of any ! Will try to research and if I find anything, I will see if I can post something around it. Thanks.

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.