Calling WebClient within WebService

I started developing an application in Silverlight that was dealing with downloading the HTML of a website and then parsing it. With Silverlight 4 this can be achieved easily by simply requesting elevated permissions. With Silverlight 3, however, the only way to get the HTML of a website is via a WebService call. My initial idea was to do the following:

public class Service1
{
    [OperationContract]
    public void GetHtml()
    {
        Uri targetUri = new Uri("http://www.google.com", UriKind.RelativeOrAbsolute);

        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += this.WebClient_DownloadStringCompleted;
        webClient.DownloadStringAsync(targetUri);
    }

    private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

    }
}

However, I realized that as soon as I make the call, which is async as well, from my Silverlight application, there is no way for me to retrieve the HTML of the website. That is why I changed to the following:

public class Service1
{
    [OperationContract]
    public string GetHtml()
    {
        Uri targetUri = new Uri("http://www.google.com", UriKind.RelativeOrAbsolute);

        WebClient webClient = new WebClient();
        return webClient.DownloadString(targetUri);
    }
}

I believe the last approach is not that fine since it will freeze the thread. So, my question, is there a way to achieve the first approach a.k.a. make async call from an async call :). Any help would be greatly appreciated.

Best Regards, Kiril

Answers


You can achieve your goal by implementig a Duplex Service. There is some useful information about it on the msdn site and a wonderful podcast entry by Mike Taulty. In general, you would have to modify your operation contract by splitting it into two parts. First part would initiate your WebClient download on the server. Then, on the server, after the html has been downloaded, the server would call back a contract that is implemented on the client side with the payload consisting of the required html content.


Need Your Help

Any difference between calling SaveChanges() inside and outside a foreach loop?

c# entity-framework

Is there any performance benefit/technical differences between calling EF SaveChanges() in a foreach loop or outside a loop, assuming a change is made to an EF entity inside the loop?

EntityManagerFactory not being injected using @PersistenceUnit

jpa-2.0 glassfish-3 jta entitymanager persistence.xml

I'm a java beginner. I'm in trouble to configure a persistance unit using JTA transactions.