views:

40

answers:

0
public string CreateOrder(string purchaseOrder)
{
    using (MyWebService.CreateService service = new MyWebService.CreateService())
    {
        string orderCode = service.CreateOrder(purchaseOrder); 
        return orderCode;
    }
}

I've added a web service reference to the domain layer of an ASP.NET web app.

This generates the two methods as expected - CreateOrder and CreateOrderAsync.

Using what I thought should be a synchronous call above though, it still seems to be calling asynchronously. The above code returns null every time, even though the web service runs correctly.

If I add a Thread.Sleep before the return statement, I get the correct value back.

How can I force this to be a synchronous call? And why is it even calling async anyway since that's not the function I'm using?

Note that the return value has to be returned to the client through an ajax call, so I can't see anyway to achieve this using a callback from the web service.

Edit: Just to be a bit more specific because this is definitely not a problem with the client code.

The above method is called from the controller like so:

string orderCode;

try
{
    orderCode = orderService.CreateOrder(purchaseOrder);

    // if i uncomment the below line, I get the order code back and everything works ok.
    // Otherwise the exception is thrown every time.
    // System.Threading.Thread.Sleep(10 * 1000);

    if (orderCode == null)
    {
        throw new DataException("Failed to allocate OrderCode for new order");
    }

    // ... etc
}