Web Service Requests Processed Out of Order

M

mgdev

I've got a problem where my web service requests are not always processed in
the same order they are received.

Both the web service and the windows application are based on the .net
framework 1.1.

Background:
At the server the database is designed with a parent/child relationship.
The parent is a list of switches (switch A, B, C, ...) and the children are
the transactions for when an [on] or [off] command has been received. To
improve query speed I've included a column called [transUID] in the parent
table to store the [UID] of the newest child record. So when an [on] or
[off] command is received a new record is placed in the child table and an
update is made to the parent table to indicate the [UID] of the new child
record.

The client application is designed to send requests to the web service to
turn a switch [on] or [off]. For the most part this works normally.
However, if the client sends two or more requests quickly (less than a couple
of seconds in between) the server will sometimes process the requests out of
order.

For example: Lets start with the switch being currently set to [off]. The
[transUID] for the switch is pointing to the last record in the child table
that represents a status of [off]. Then the client quickly sends an [on]
request imediately followed by an [off] request (this is a toggle button
programmed to switch between [on] and [off]). Although the database may show
the exact same date and time for the two records, the [transUID] should show
the [UID] of the last transactional record. Unfortunately, this is not
always the case. Sometimes the second request will get processed first.
Then in my history I will see the transactions going from [on] to [on] to
[off]. And the [transUID] is no longer pointing to the correct transaction
and the data is now inaccurate.

Any suggestions?
 
J

John Saunders

mgdev said:
I've got a problem where my web service requests are not always processed
in
the same order they are received.

Why does this surprise you? Did you ever see a guarantee that requests would
be processed in order?

If the order of several requests is important, then you should turn them
into a single request, which can process its pieces in whatever order it
likes:

Before:

[WebMethod]
public void PartOne(int a)
{
}

[WebMethod]
public void PartTwo(int b)
{
}

[WebMethod]
public void PartThree(int c)
{
}

After:

[WebMethod]
public void AllThreeInOrder(int a, int b, int c)
{
PartOne(a);
PartTwo(b);
PartThree(c);
}

private void PartOne(int a)
{
}

private void PartTwo(int b)
{
}

private void PartThree(int c)
{
}

John
 
M

mgdev

You're right. I have never seen a guarantee that anything should work as
expected. But I would like to understand the problem so I can at least build
a work around.

Unfortunately, your solution doesn't solve my situation.

The issue is not that a user calling three different functions. The issue
is that the same function is being called more than once.

<WebMethod>
Public Sub SwitchStatus(SwitchID as Integer, Status As Boolean)
If Status = True Then
"Turn the switch on"
Else
"Turn the switch off"
End If
End Sub

This function is normally called once or twice an hour per switch (Turn the
switch on or turn the switch off). But when a quick on and off is sent (like
someone clicking the button twice in the same second), the web service will
sometimes process the off request first. Then instead of the switch going
from off to on to off again. It runs the sequence of off to off to on
leaving it in the wrong position.
 
J

John Saunders

mgdev said:
You're right. I have never seen a guarantee that anything should work as
expected. But I would like to understand the problem so I can at least
build
a work around.

Unfortunately, your solution doesn't solve my situation.

The issue is not that a user calling three different functions. The issue
is that the same function is being called more than once.

<WebMethod>
Public Sub SwitchStatus(SwitchID as Integer, Status As Boolean)
If Status = True Then
"Turn the switch on"
Else
"Turn the switch off"
End If
End Sub

This function is normally called once or twice an hour per switch (Turn
the
switch on or turn the switch off). But when a quick on and off is sent
(like
someone clicking the button twice in the same second), the web service
will
sometimes process the off request first. Then instead of the switch going
from off to on to off again. It runs the sequence of off to off to on
leaving it in the wrong position.

Why not create a web method which toggles the state instead of explicitly
setting it on or off. That way, if there's a "double-click", it won't matter
which happens first.

John
 
M

mgdev

I guess my example is oversimplified. The entire application is much more
complicated and I'd rather not try to explain the whole thing.

In reality I have more than two states for my switch. Because of this the
toggle won't work.

There are actually about 12 different states that a switch can be in. And
whether I make them one function call with a parameter or 12 different
functions the root problem remains the same. The order that the server
receives the requests is not always the order in which they are processed.
Even though they are being sent by the same instance of the client
application in the appropriate consecutive order.
 
J

John Saunders

mgdev said:
I guess my example is oversimplified. The entire application is much more
complicated and I'd rather not try to explain the whole thing.

In reality I have more than two states for my switch. Because of this the
toggle won't work.

There are actually about 12 different states that a switch can be in. And
whether I make them one function call with a parameter or 12 different
functions the root problem remains the same. The order that the server
receives the requests is not always the order in which they are processed.
Even though they are being sent by the same instance of the client
application in the appropriate consecutive order.

The web services infrastructure makes no guarantees of sequence. If you need
such a guarantee, you need to impose it yourself. You'll need to have your
client increment a counter and include it in each message. You'll have to
have your server keep track of the sequence number for each client instance.
It will have to queue up requests if they arrive out of order, and will have
to operate asynchronously (see this month's MSDN magazine for a good
article).

Sorry, but you seem to have created an architecture based on incorrect
assumptions. If you can't re-architect now that you understand that your
assumptions are false, you'll have to create an infrastructure which makes
your assumptions true.

John
 
M

mgdev

OK. I get the issue with the server not processing requests in the order
they are received/sent. Now I need to explore a couple of questions
regarding the solution.

Is there any difference between using a sub vs. a function with the web
service?

I'm currently using sub routines in my web service and based on what we've
talked about the clint calls the routine and continues on without waiting for
the web service to complete the call. This leads to the client being able to
send another call without knowing whether or not the first one has completed.

If I use a function in my web service and coded the client to evaluate the
return value, would that cause the client code to stop and wait for a
response before it continues? This would insure that the client can't send a
second request until the first one has returned it's value.
 
J

John Saunders

mgdev said:
OK. I get the issue with the server not processing requests in the order
they are received/sent. Now I need to explore a couple of questions
regarding the solution.

Is there any difference between using a sub vs. a function with the web
service?

No. They are the same thing.
I'm currently using sub routines in my web service and based on what we've
talked about the clint calls the routine and continues on without waiting
for
the web service to complete the call. This leads to the client being able
to
send another call without knowing whether or not the first one has
completed.

There is no difference between a subroutine and function. The client is
behaving asynchronously for its own reasons, having nothing to do with the
server.
If I use a function in my web service and coded the client to evaluate the
return value, would that cause the client code to stop and wait for a
response before it continues? This would insure that the client can't
send a
second request until the first one has returned it's value.

There is no difference between a subroutine and a function.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top