Mike Ormond's Blog

Musings on mobile development and Windows Phone 7 in particular.

Accessing ASP.NET Application Services from Silverlight

Accessing ASP.NET Application Services from Silverlight

  • Comments 14

A chance conversation at the NxtGenUG got me to thinking about whether you could access the ASP.NET Membership service and Profile service from Silverlight managed code. As ASP.NET AJAX allows you to expose these to JavaScript (using JSON serialisation) then it seemed perfectly feasible that you would be able to do this. So I had a go...

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.

image

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.

  • Okay, between me writing Accessing ASP.NET Application Services from Silverlight part1 and part 2 (haven't

  • Okay, between me writing Accessing ASP.NET Application Services from Silverlight part1 and part 2 (haven&#39;t

  • In part 1 , we set ourselves up for success by creating the necessary project structure, xaml file and

  • In part 1 , we set ourselves up for success by creating the necessary project structure, xaml file and

  • Silverlight Cream for July 12, 2007

  • Mike Ormond is doing a great series of posts about Accessing ASP.NET Application Services in Silverlight.

  • Thanks for the nice words about my post!  It was learned through a lot of frustration.

  • Thank you Peter - I suspect you saved me a lot of frustration! :-) Mike

  • See also part 1 and part 2 . So we're at the point where we've set up our environment, we've created

  • See also part 1 and part 2 . So we&#39;re at the point where we&#39;ve set up our environment, we&#39;ve

  • An email on one of our internal aliases made me realise that I hadn't included the namespaces I'd included

  • An email on one of our internal aliases made me realise that I hadn&#39;t included the namespaces I&#39;d

Page 1 of 1 (14 items)