by Kirk Patrick Junsay
What solution should you use? Well, the answer for that is to leverage Azure functions. Nowadays you can commonly hear or maybe read the term Serverless computing. If you are a developer, well good news instead of trying to set up some servers you can just concentrate more on your codes. In Today’s article, I would like to show to you how easy it is to setup Azure Function and integrate with a simple Xamarin Forms Application.
What are Azure Functions?
Azure Functions basically is a FaaS offering of Azure that will let you concentrate more on the code itself rather than the complexity of designing your Infrastructure. A good thing also is that you can scale on demand and only pay for what you have just used. Another highlight of using Azure Functions is that you can seamlessly integrate with other Azure services like Storages, Event Hubs and many more. In this tutorial will just use the HTTPTrigger but you can check some other Azure functions as shown in the Image below:
Setting up an Azure Function
The first thing to do is to log in on your Microsoft Azure Account. You can also activate your Azure access on your My Visual Studio Benefits for free credits. You will be greeted by the landing page as shown in Figure 1.
Next thing to do is to create the Azure function using the Create a Resource button. Then under the Compute Category you will see the Function App service as shown in Figure 2.
After clicking the Azure Function App, you will be greeted by some settings you need to complete as shown in Figure 3.
- App Name: The unique application you want to call for your Function App.
- Subscription: Choose the subscription e.g. (V.S. Professional, Enterprise) you want to use for your Function App.
- Resource Group: Choose to create a new or either put it in an existing group the Function App that you want to create.
- OS: The OS that you want your server to be, could be Linux or either Windows.
- Hosting Plan: Basically, you can either choose a consumption plan (Pay as you execute) or App service plan (Predefined capacity allocation).
- Location: Region wherein you’re Function App should run.
- Runtime Stack: Choose what runtime for your preferred language you want to use on your Function app.
- Storage: Choose a new or created storage you want your Function app to utilize.
- Application Insights: You can use this to further monitor your Function App.
Once you’ve successfully created your Function App you will be greeted by the Main Page of your Azure Function App as shown in Figure 4.
So let’s try to add a simple function by clicking the Functions item as shown in Figure 5.
In this example we will use the HTTP Trigger Function App wherein the triggering of our code will be activated through calling the HTTP. Will name our HTTP Trigger Function app as xamarinhelloworld shown in Figure 6.
Creating this successfully will have a default generated code as shown in Figure 7. For the simplicity of this tutorial, we will not modify or make changes on our generated code.
One thing to note here is that you can either use the Azure Web IDE to manage or make changes on your Function App or you can also use the Visual Studio IDE. Now let’s try to test our function App using the Web IDE. Just click the Run button and you will see the output on the lower right part of your screen as shown in Figure 8. You can also try to edit the parameters for Azure Function by editing the Request Body.
Let’s try to test our function app using our web browser just to validate that it’s working outside the Azure portal. The first thing to do is to get the complete URL by clicking the Get Function URL code as shown in Figure 9. Just be mindful of your Azure Function Key for security purposes. You can check the Manage menu to view, renew or revoke each individual Function key.
Next thing to do is to copy and paste in your web browser but adding the parameter &name=Juan so that it will process and in return will display Hello Juan as shown in Figure 10.
Integrating Function App with Xamarin
For our Xamarin integration will be using a simple Android App wherein the user can enter their name and will be processed by our Function app and will be returned to our Android App as shown in Figure 11.
Our sample application will just have three lines of codes using mainly the HttpClient class. Will use the GetStringAsync() method with our URL as our parameter as shown in Figure 12. Try to run the Android application using either your emulator or actual device and you should see the same output on Figure 11.
Here is the URL for the sample App: https://github.com/KirkPatrickJunsay/XamarinAzureFunctions
To wrap things up, we created a simple Azure Function App in our Azure Portal, then we’ve learned how to execute it and also test it in our web browser. We also used the HTTPClient class in order to test in our simple Xamarin Forms Application. With that in mind Azure Functions are good for small APIs, microservices or for POCs, if you want to explore more you can check other Serverless Computing offerings of Microsoft.