Skip to content
/

Calling a SharePoint Web Service from Silverlight over HTTP and HTTPS

The past couple of weeks I’m working with Silverlight controls embedded in SharePoint 2007.
For one of the controls I need to retrieve the data using the Search Query Web Service.
I was using the following code:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    var endpoint = new EndpointAddress(GetParam("SharePointWeb") +
                                                           "/_vti_bin/search.asmx");
    var binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
                      {
                          Name = "QueryServiceSoap",
                          MaxReceivedMessageSize = 2147483647,
                          MaxBufferSize = 2147483647
                      };

    var client = new QueryServiceSoapClient(binding, endpoint);
    client.QueryExCompleted += this.ClientQueryExCompleted;
    client.QueryExAsync(GetParam("Query"));
}

This was working perfectly in the development environment. But when deploying the control to the production environment it didn’t work.

After some digging I found that the control did work, but only when the page was visited over an HTTP connection. As everybody accesses the page over an HTTPS connection I was pointed in the direction of the connection between the control and the web service.

As can be seen in the code the WCF client is used and I remembered that the binding security mode is different if you want to work with HTTPS. So I changed the BasicHttpSecurityMode from None to Transport.

-var binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
+var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)

After deploying the control again it worked nicely over the HTTPS connection, so I know what the source of my problem is. But naturally I want a generic solution so the configuration of the access mapping is not influencing the functioning of the control.

The question is how to detect if the control is hosted on a page over an HTTP or HTTPS connection. This can be found in the SilverlightHost.Source Property which can be compared against the Uri.UriSchemeHttp Field or the Uri.UriSchemeHttps Field.

As a result this is my final code:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    var basicHttpSecurityMode =
        (Application.Current.Host.Source.Scheme == Uri.UriSchemeHttp)
            ? BasicHttpSecurityMode.None
            : BasicHttpSecurityMode.Transport;

    var endPoint = new EndpointAddress(GetParam("SharePointWeb") +
                                                       "/_vti_bin/search.asmx");
    var binding = new BasicHttpBinding(basicHttpSecurityMode)
                      {
                          Name = "QueryServiceSoap",
                          MaxReceivedMessageSize = 2147483647,
                          MaxBufferSize = 2147483647
                      };

    var client = new QueryServiceSoapClient(binding, endPoint);
    client.QueryExCompleted += this.ClientQueryExCompleted;
    client.QueryExAsync(GetParam("Query"));
}

Works like a charm.