开发者

Should WebClient instances be reused in Silverlight

I'm writing a Silverlight application for Windows Phone 7 which has a class tha开发者_高级运维t needs to make multiple requests to WebClient.DownloadStringAsync()

Am I better off creating a new instance of WebClient for each request, or is it more efficient to initialise a single instance in a field and use that for each request (making sure to only have one request active at any one time)?

public class MainViewModel : INotifyPropertyChanged
{
    private readonly WebClient _wc;

    public MainViewModel()
    {
        _wc = new WebClient
        {
            Credentials = new NetworkCredential( "yyyyyyy", @"xxxxxx" )
        };

    }

    readonly Uri _baseUrl = new Uri( @"https://some.web.url" );

    public void GetServices()
    {
        _wc.DownloadStringCompleted += GetServicesCompleted;
        var uri = new Uri( _baseUrl, "/path" );
        _wc.DownloadStringAsync( uri );
    }

    private void GetServicesCompleted( object sender, DownloadStringCompletedEventArgs e )
    {
        _wc.DownloadStringCompleted -= GetServicesCompleted;

        string result = e.Result;

        // other logic...

        GetServiceResources();
    }

    private void GetServiceResources()
    {
        _wc.DownloadStringCompleted += GetServicesResourcesDownloaded;
        var url = new Uri( _baseUrl, "/path2" );
        _wc.DownloadStringAsync( url );
    }

    // etc
}


If you're using WebClient I'd create a new one each time. I'd also use a lambda expression for the DownloadStringCompleted event as this will enable you to keep all the related logic together.
e.g.:

var wc = new WebClient();
wc.DownloadStringCompleted += (sender, e) => { GetServiceResources(); };
wc.DownloadStringAsync(new Uri("http://example.com/path", UriKind.Absolute));

This should make the code easier to read and therefore maintain.

In that your smaple code also has nested web requests (the completed event starts another request) reuse of the same client could make debugging more difficult.

Please be aware that the WebClient automatically marshalls the completed event back to the UI thread so any work you do there will block that UI thread. If your completed event does anything other than a simple update to the UI, use of HttpWebRequest is recomended instead for performance and usability issues.

I'd also recommend against making consecutive web requests if you could possibly run them in parallel or (even better) combine the logic (and response) into a single request.


You should also be aware, that if anything like WCF client proxies the web client will probably not remain in a happy state if something goes wrong during a connection. That is, it's probably not fault tolerant.

Therefore, I would think that you should probably just re-instantiate it each time. Plus if used correctly in using blocks you'll more effectively manage your resources (though as BFree points out, WebClient isn't a particularly heavy resource).

Not that this will specifically help, but for a WCF web service specific solution we used an implementation based on http://www.acorns.com.au/blog/?p=113 to provide a fault tolerant web client for our WCF web services proxies. We required this since we were only dependency injecting the WCF endpoints (or mock in some instances) at startup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜