Search code examples
xamarinxamarin.androidodatavisual-studio-lightswitch

Using Simple.OData.Client in Xamarin - unable to update/delete


I am using the Simple.OData.Client within a Xamarin android project. I am trying to access data from a Lightswitch odata service with user authorisation.

Both the get & insert work: ie FindEntriesAsync() and InsertEntryAsync().

However the update/delete methods do NOT work, i.e. UpdateEntryAsync() and DeleteEntryAsync(). The code simply hangs on the await command.

            // DELETE NOT WORKING
        RunOnUiThread(() => phoneNumberText.Text = "Trying to delete existing record.");
        await client
            .For("JobTypes")
            .Key(7)
            .DeleteEntryAsync();

        returnValue = "Deleted OK";
        RunOnUiThread(() => phoneNumberText.Text = returnValue);



        // UPDATE - NOT WORKING
        RunOnUiThread(() => phoneNumberText.Text = "Trying to update existing record.");
        String newJobTypeName = "xxNewJobType2changed";
        await client
            .For("JobTypes")
            .Key(7)
            .Set(new { JobType1 = newJobTypeName })
            .UpdateEntryAsync();

        returnValue = "Updated OK";
        RunOnUiThread(() => phoneNumberText.Text = returnValue);

        // INSERT - working
        Boolean isActive = true;
        newJobTypeName = "xxNewJobType2";
        RunOnUiThread(() => phoneNumberText.Text = "Trying to insert new record.");
        var newJobType = await client
            .For("JobTypes")
            .Set(new { JobType1 = newJobTypeName, IsActive = isActive })
            .InsertEntryAsync();

        returnValue = "New JobTypeID = " + newJobType["JobTypeId"];

        // GET - working
        var terms = await client.FindEntriesAsync("JobTypes");
        foreach (var term in terms)
        {
            returnValue += term["JobType1"];
        }

I have put my code for all four routines here. Has anyone seen this or can anyone give any pointers?


Solution

  • This was a bug in Simple.OData.Client 3.0 that ignored If-Match etag. According to OData protocol, when issuing a PUT, MERGE or DELETE request, clients need to indicate an ETag in the If-Match HTTP request header.

    • If for a given client it is acceptable to overwrite any version of the Entry in the server, then the value “*” may be used instead.
    • If a given Entry has an ETag and a client attempts to modify or delete the Entry without an If-Match header servers should fail the request with a 412 response code.

    Some OData services don't check If-Match headers, but others (Lightswitch) do, and since Simple.OData.Client didn't set them, so update and delete operations failed on these services. The error is now fixed in the forthcoming version of Simple.OData.Client (3.1).