In recent years, it becomes more and more common that enterprises are tranferring their websites to mobile apps. By transferring, I mean that they want both versions of their apps. That will give us a scenario that our users, especially for Microsoft customers, they will want a same database structure even a same set of Web API to use for both Website and mobile apps.
This blog is exactly the same situation here. I got one customer who already has a website and its Web API is based on .NET Core 2.0. However, he’s trying to use the Xamarin.Forms to build an app and also using the Azure Mobile Apps SDK to enable the offline sync feature in his app.
Of course, he wants the Web API to be the same for both Website and Mobile app.
If you use a mobile app, you will be handling data sync with the database. When the network is good on your mobile, you will never meet issue to query/add/update/delete from database and sync back with your mobile app.
However, you will never be confident that your mobile will always be under a good network situation. That’s where the offline sync fit in.
Offline sync allows users to interact with a mobile application, viewing, adding, or modifying data, even where there isn’t a network connection. Changes are stored in a local database, and once the device is online, the changes can be synced with the Azure Mobile Apps instance. This article explains how to add offline sync functionality to a Xamarin.Forms application.
It’s actually quite easy to do so. If you are only dealing with the mobile app. You can directly use the Quick Start template from our Azure portal. It’s a todoList app that can be directly run and can be easily enabled for offline sync feature.
Follow Create a Xamarin.Forms app with Azure to download the Xamarin.Forms app with Azure Mobile Apps SDK integrated.
Follow Enable offline sync for your Xamarin.Forms mobile app to enable the offline sync feature in the project you just downloaded.
Follow Synchronizing Offline Data with Azure Mobile Apps to understand the usage, troubleshooting and testing on offline sync feature.
If you followed these instructs and also if you are using the template that you downloaded from Azure portal, you will not see any problem at all. It’s working normally.
As I mentioned in the above, if you purely run from our template, it will work. However, my customer is trying to use the same Web API to do the offline sync with the backend database, that’s where the issue came in.
If you read in detail from the above document in step 3, you will find that the key code block for offline sync is the
PushAsync() method which is within the
Detail code as below:
public async Task SyncAsync()
And wala, my customer is getting error and he debugged the error in the foreach loop. The error is something like:
[0:] Error executing sync operation. Item: WorkDiary (fcddf2b4a5c24e8b8119be2fd3af993c). Operation discarded..
This is the main content I want to share in this blog. The way I troubleshoot the issue and how I found the root cause is really important. You will not see much debugging or something because customer already knows how to get the error, and in this case, it’s also customer’s debugging leads us to the solution and root cause.
I was really lost when I first troubleshoot on this issue. I reviewed all the customer’s code related to the offline sync, and nothing suspecious coming out. Customer is following our documents and the code has barely been changed.
However, my customer has implemented one
LoggingHandler() to capture logs for the web request he has made. And below is the content for his web request when calling
[0:] Request: POST https://xxxxxxxx.azurewebsites.net/tables/WorkDiary
We can learn from the above information that the data (json format) was there, the URL is clear, and the response status should be 404 Not Found.
After getting the information from customer, it suddenly hit me that since customer mentioned he’s trying to use the same Web API as his website, so is it really the same in these 2 scenarios?
One easy way to find out is: Fiddler to capture the network trace.
Below are the detail steps to troubleshoot the issue:
I simulate the same process using Fiddler’s
Composerfeature. I built the same data in json format, I use
POSTmethod to post the data to the Web API Url.
I got the same result as his Xamarin app, it returns 404 Not Found error:
I created a record in customer’s website, while doing this, I captured the fiddler as below:
And the data posted to the Web URL is:
From the above comparasion of the Fiddler trace, we can see at least 2 things:
- The Web URLs for the Web API that we are calling are totally different between customer’s website and Xamarin web request.
- In his website’s Web API, he’s using the Web Form data structure. However, in Azure Mobile Apps SDK, he’s using the json format.
So these are the root cause of why this issue is happening.
Knowing the root cause, I went for product team in Microsoft for help. As product team mentioned, to use the Azure Mobile App SDK, customer has to implement the Web API according to the way that our SDK is trying to do.
It’s really bad because customer already implemented one Web API based on .NET Core, and our Azure Mobile SDK only supports .NET4.6. That’s why I am calling it the pain point because customer has no other way to avoid this issue.
Finally customer agreed to implement another set of API for this usage and I helped him with another case, and now I can confirm after implementing the API, it’s working as normal again.
If you want to know details about developing Xamarin.Forms app using Azure Mobile SDK, here is the link you should definitely look into: Develop Cloud Connected Mobile Apps with Xamarin and Microsoft Azure