nzic
nzic

Reputation: 184

Speeding up http web request in WCF invoked method

Hi I am writing a WCF service not hosted on IIS. It runs on my server as console application. I have a static method that is invoked by the mentioned service.

In this method I have async web request that sends sms.

Sometimes it happens that sms is never recieved. After some debuging I found out that when I remove async call the web request sometimes throws an exception with message: 'The operation has timed out'. This happens when i try to send many smss in short period of time.

But when i type the address of the web request in browser everything works fine. (the times i press refresh, no matter how fast, that number of times i receive sms) how can this be achived with what i've got.

So far i have

    public static bool DoTheRequest(string number, string message)
    {
        try
        {
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(string.Format("http://SomeURL?With={0}&something={1}", number, message));
            myReq.BeginGetResponse(FinishWebRequest, myReq);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return true;
    }

    static void FinishWebRequest(IAsyncResult result)
    {
        HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
    }

EDIT:

And the service definition:

    [OperationContract]
    void TestSms(string number);

and implementation:

    public void TestSms(string number)
    {
        Utilities.DoTheRequest(number, "THIS IS A TEST");
    }

Please help

Upvotes: 0

Views: 415

Answers (1)

Moo-Juice
Moo-Juice

Reputation: 38825

Depending on how many times you are calling the send-sms function in a short period of time, I surmise that it's doing it a lot faster than you can refresh in your browser - and you're managing to flood whatever service it is you are using with your SMS calls.

In this scenario, I would suggest that your web-service method actually puts the SMS in to a queue, and some kind of a background worker thread that is designed not to throttle the API that actually sends the SMS, has the job of batching up these SMSs and sending them.

Better yet, to facilitate the issue that machines and software are not perfect, and lightning does indeed strike, I would suggest that you push SMSs in to some kind of back-end data store and leave it at that. A background worker job/thread then has the job of finding all SMSs that are unsent and trying to send them (but sending no more than "x per minute"), each one being marked as "Sent" upon completion.

This has the added advantage that you can throw as many SMSs as you want at your backing data storage, if the machine dies it can pick up where it left off, and if you get some failures, they can be retried.

Upvotes: 2

Related Questions