views:

88

answers:

1

Is this the right approach for closing connections with WCF in silverlight?

ServiceClient client = new ServiceClient();

        client.MakeRequestCompleted += (sender, e) =>
            {
                client.CloseAsync();

                //some implementation goes here
            };

        for (int i = 0; i < 1000; i++)
        {
            client.MakeRequestAsync();
        }

I appear to be having problems related to concurrent connections, when the loop gets to the point where its made about 300 requests, it just fails. Thanks.

A: 

I'm not an expert but I've been researching a similar problem and no one else has left an answer...

You are closing your client without guaranteeing your operation to make 1000 web service calls has finished. I have also found it a little tough to find documentation on the CloseAsync method, but the accepted wisdom seems to be to call CloseAsync after you've made all your web service calls.

Also, making a thousand web service calls in your app could be dangerous - obviously I don't know the ins and outs of your situation but it may be better to combine your calls into fewer, larger operations.

Phil
I saw an implementation online that suggested adding "(e as ServiceClient).CloseAsync()" to the completed event, its worked ever since. I believe each asynchronous call is a unique connection to the web service that gets closed automatically but not immediately thereby creating a bottleneck causing the web service requests to fail. If you close each connection in the completed event then you prevent that bottleneck from occuring.
Bablo
Yep, that's the way I'd do it normally but he's using one client object for 1000 concurrent operations. Who knows what's going to happen?
Phil