SilverLight WCF References in SharePoint - The right way!

Posted on 7/4/2008 @ 7:43 PM in #SharePoint by | Feedback | 5260 views

In this blogpost, I am going to talk about the right/proper way of adding WCF references for Silverlight apps in SharePoint 2007. Also I will demonstrate leveraging .NET 3.5 facilities that will allow you to modularize and author/write code in client operating systems such as Windows XP and Vista, and then afterwards move code using a simple deployment into SharePoint.

Couple of days ago, I had blogged about a problem I was running into with Silverlight WCF references for code I wanted to run in SharePoint.

The standard way of adding a reference into Silverlight is "Right click Add Service Reference". That is well and good, except, it likes to put all it's address, binding and endpoint information in the web.config. Why is that a bad idea? Because that means in SharePoint, you have to pollute your root website web.config. Yes you heard me right - unlike SharePoint acting as a WCF host where you have the choice of controlling the hosted address, and putting the web.config at a predefined location, WCF as a client means, you have no idea where your code may run, i.e. under which site or page's context.

Thus, you have a problem! And this blogpost is more about the solution :-)

I pinged a number of sources to try and get a resolution to the problem. The only good, acceptable, and frankly really elegant and RTM perfect solution was provided by Christopher Scrosati of the Silveright Web Services team at Microsoft - so a big thanks to him. (Repeating his disclaimer here: This comment reflects my opinion only, and does not reflect nor imply the one of my employer.) I am putting my weight behind his suggestion that this suggested solution is perfectly acceptable and I'd feel comfortable running it in production code (well, except that SilverLight 2 is still beta2).

Here is a step by step implementation of his suggestion.

Start by right click --> Add service reference as shown below --

Note that I was able to replicate my entire dev. setup inside a pure ASP.NET app. This means, you can now develop on Vista, and deploy to SharePoint Win2k8. I will share tips like these and more in my upcoming training (will be available in the US too).

Next, show Hidden Files in your solution explorer, and pick the "Reference.cs" file out as shown below -

Ok good job!

Now, delete that Service Reference, and re-add the "Reference.cs" back into your silverlight app and rename and change namespace as I did below -

Great! Now with the Proxy ready, you're now ready to use your Service Reference.

I wrote up some XAML as shown below:

   1: <Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">
   2:     <Grid.RowDefinitions>
   3:         <RowDefinition Height="30"/>
   4:         <RowDefinition Height="*"/>
   5:     </Grid.RowDefinitions>
   6:     <Grid.ColumnDefinitions>
   7:         <ColumnDefinition Width="*"/>
   8:     </Grid.ColumnDefinitions>
   9:     <StackPanel Grid.Row="0" Orientation="Horizontal">
  10:         <TextBox x:Name="searchTerm" Grid.Row="0" Text="Search Term" VerticalAlignment="Center" Width="300" Margin="5"/>
  11:         <Button Content="Go Search" x:Name="searchButton" Grid.Row="1" Width="75" Margin="5"/>
  12:     </StackPanel>
  13:     <my:DataGrid x:Name="searchResults" AlternatingRowBackground="Beige" AutoGenerateColumns="True" CanUserResizeColumns="True" Grid.Row="1"/>
  14: </Grid>

And then some associated C# as shown below:

   1: public partial class Page : UserControl
   2: {
   3:     public Page()
   4:     {
   5:         InitializeComponent();
   6:         searchButton.Click += new RoutedEventHandler(searchButton_Click);
   7:     }
   9:     void searchButton_Click(object sender, RoutedEventArgs e)
  10:     {
  11:         EndpointAddress endPointAddress = new EndpointAddress("/_wcf/MyMusic.svc");
  12:         BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
  13:         SongQueryClient client = new SongQueryClient(basicHttpBinding, endPointAddress);
  14:         client.GetSongsAsync("me");
  15:         client.GetSongsCompleted += new EventHandler<GetSongsCompletedEventArgs>(client_GetSongsCompleted);
  16:     }
  18:     void client_GetSongsCompleted(object sender, GetSongsCompletedEventArgs e)
  19:     {
  20:         searchResults.ItemsSource = e.Result;
  21:     }
  22: }


Note that in Silverlight,

a) You can only use basicHttpBinding, or use webHttpBinding via the JavaScript using managed code,
b) You must always call your web services asynchronously or you'd freeze the browser.

Lets run it -  First in ASP.NET

.. and then in SharePoint

Deployment was as simple as copying the .xap to a document library. Deploying the WCF endpoint was a tad bit more complex, but with the right homework it can all be deployed using features and solutions. All such details are covered in my upcoming training.

Sound off but keep it civil:

Older comments..

On 5/26/2009 2:55:59 PM Jeff said ..
I am a little unclear on the step to delete the reference.cs and then re-add it with a new name. Would it be possible for you to clarify that a little more?

Is SongQueryClient part of that new proxy you create?