Accessing ASP.NET Application Services from Silverlight
Before I go any further I should say that I fully expect there to be a simple mechanism to do this in Silverlight 1.1 but it's not there now as far as I can see. Having said that, someone will probably contact me to tell me what a numpty I've been and I should have just called xyz API instead. If that happens I shall console myself that this was a learning experience... Oh, and what I'm doing here probably isn't a supported approach :-).
A couple of things to start out with. Firstly you'll need ASP.NET AJAX on your web server as we need the JSON serialization. ie we'll be accessing the Membership and Profile services in exactly the same way as Sys.Services.AuthenticationService and Sys.Services.ProfileService classes in the ASP.NET AJAX client libraries do. Secondly, we need to break free of a "pure" Silverlight 1.1 project and combine that with a website in the same solution. The website will host the Silverlight app and provide the context for the application services. To do this you create an empty solution and add a Silverlight project and a website, linking the website to the Silverlight project. A detailed description of what you need to do can be found in this excellent entry on Peter Kellner's blog.
Follow Peter's guidance and you should end up with something looking a little like my solution on the left. A website hosting the Silverlight page linked to a Silverlight project. Any changes to the Silverlight code should be made in the Silverlight project. These then get copied across to the website (well generally they do, I have had terrible problems at times getting debugging to work and I suspect this is some caching issue throwing the assembly and .pdb files out of sync but I haven't got to the bottom of it TBH).
My XAML content couldn't be much simpler:
<Canvas> <TextBlock x:Name="TextBlock1" Text="Login" Canvas.Left="10" Canvas.Top="10" FontSize="24" MouseLeftButtonDown="OnClick" /> <TextBlock x:Name="TextBlock2" Text="Get Shoe Size" Canvas.Left="10" Canvas.Top="50" FontSize="24" MouseLeftButtonDown="OnClick" /> </Canvas>
(This Canvas is a child of the parentCanvas in the default Page.xaml created by VS). So we simply have two TextBlocks and each is wired up to an event handler called OnClick() . We also need to prepare the website for the Membership and Profile application services. In web.config:
<system.web> <!-- Other Stuff --> <authentication mode="Forms" /> <profile> <properties> <add name="ShoeSize" type="System.Int32" defaultValue="12" /> </properties> </profile> <!-- Other Stuff --> </system.web>
<system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" requireSSL="false" /> <profileService enabled="true" readAccessProperties="ShoeSize" /> </webServices> </scripting> </system.web.extensions>
In other words you need to enable Forms authentication and create one or more Profile properties in <system.web> and then enable both services for script access in <system.web.extensions> .
Okay, that's us pretty much good to go. Rather than make this a monster post, I'll break it into installments. Next installment: successfully calling the application services themselves.