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">
3: <RowDefinition Height="30"/>
4: <RowDefinition Height="*"/>
7: <ColumnDefinition Width="*"/>
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"/>
13: <my:DataGrid x:Name="searchResults" AlternatingRowBackground="Beige" AutoGenerateColumns="True" CanUserResizeColumns="True" Grid.Row="1"/>
And then some associated C# as shown below:
1: public partial class Page : UserControl
3: public Page()
6: searchButton.Click += new RoutedEventHandler(searchButton_Click);
9: void searchButton_Click(object sender, RoutedEventArgs e)
11: EndpointAddress endPointAddress = new EndpointAddress("/_wcf/MyMusic.svc");
12: BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
13: SongQueryClient client = new SongQueryClient(basicHttpBinding, endPointAddress);
15: client.GetSongsCompleted += new EventHandler<GetSongsCompletedEventArgs>(client_GetSongsCompleted);
18: void client_GetSongsCompleted(object sender, GetSongsCompletedEventArgs e)
20: searchResults.ItemsSource = e.Result;
Note that in Silverlight,
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.