1

I have created a small web service method using an asmx file. Here'a simplified version of it.

    <WebMethod()> _
Public function DeleteFile(Byval fileID As String) as boolean
    DeleteFileByID(fileID)
    return true
End Sub

It's working very well but I would like to make sure the data sent back to the client doesn't get lost in the process.

I know this could be done by setting a second web service method that would be call by the client to confirm he received some data. However, I would like to know if this could be done in a single web service method.

Here's an example of what I might be looking for:

        <WebMethod()> _
Public function DeleteFile(Byval fileID As String) as boolean
    return true

    clientAcknowledgement = 'This is what I'm loking for... How to make sure the client received the confiormation before deleting the file
    if clientAcknowledgement then
        DeleteFileByID(fileID)
    end if

End Sub
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
  • Do you have the option of using WCF? If so, you could look at the answer to [this question](http://stackoverflow.com/questions/1940889/wcf-webservice-is-there-a-way-to-determine-that-client-received-response). – Matt Jul 12 '13 at 20:40

2 Answers2

2

I would solve this by adding a parameter to the web method call that the caller can set.

<WebMethod()> _
Public Function DeleteFile(ByVal fileID As String, clientAcknowledgement As Boolean) As Boolean
    If clientAcknowledgement Then
        DeleteFileByID(fileID)
    End If
End Function

If the developer that calls this method is lazy and just sets it to true all of the time without prompting the end user, then it is their responsibility to answer to their users.

You could get pretty complex with scenarios to call one method then another, but at the end of the day, the developer consuming the web services can circumvent just about anything you put into place and you have no real guarantee that the end user has been asked unless you implement the code yourself.

UPDATE

After clarification that the desire is to know that the caller received the response prior to actually deleting the file, I have some additional ideas.

There is no easy way that I am aware of to ensure the web services response has been successfully delivered to the client before performing the actual deletion.

However, there are a couple of alternatives:

Option A: Add a DeleteRequested method that sends a token to the caller that they then have to return to the DeleteFile method. You would only perform the deletion if the token is valid. There are still timing issues here, but it is slightly better than the current implementation.

Option B: Implement an IHttpHandler that is exposed through an ASHX page. Because you are responsible for sending the response in this scenario, you will know if it completed transferring successfully or not (i.e. the client disconnected), so you could wait to perform your delete until the response completes. This does change how the client calls and responds to your service, but does give you the validation you are after.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • 2
    If you don't want to delete a file, why would you call a method called `DeleteFile` and pass that method the Id of the file you don't want to delete and a false boolean value that basically says 'Just kidding, I don't really want to delete it'. I think the question is more about confirming a client's receipt of a SOAP response. – Matt Jul 12 '13 at 21:06
  • Matt is right. The real issue here is about the client's receipt of a SOAP response. – The_Black_Smurf Jul 12 '13 at 22:43
  • @Matt: It wasn't clear what the OP was after and it still isn't entirely clear. I thought they were after accountability. By adding a flag, you can record in a transaction log that the caller verified that they checked with the end user first so that if anything goes wrong down the road, you can point to the log as proof that the caller verified with the user. This is very similar to checking the box on a license agreement that you agree to the terms. You may not ever read them, but you check the box anyway, which covers the company with the license agreement. – competent_tech Jul 12 '13 at 22:54
  • @The_Black_Smurf: Sorry, I did not understand that from the question. Please see the update to the answer. – competent_tech Jul 12 '13 at 23:10
1

Trying to read between the lines a little, it occurs to me that the poster's problem may really be that if something goes wrong in between any of a series of actions (invoked using web service calls), the database or a set of files (or both) might be left in an inconsistent state.

In which case, I would suggest the use of a transaction.

A good starting point on transactions is the MSDN article Distributed Transactions in Visual Basic .NET.

All sorts of things could go wrong with - to use the poster's example - deleting a file, not just the lack of a SOAP confirmation. The idea of a transaction is that if anything goes wrong, all changes made within the transaction are reversed. HTH

debater
  • 466
  • 6
  • 11