Welcome to MSDN Blogs Sign in | Join | Help

Mike Ormond's Blog

In my world, things would be simpler than this...

News

  • Add to Technorati Favorites

    These postings are provided "AS IS" with no warranties, and confer no rights. The use of any script / code samples is subject to the terms specified here.

Accessing ASP.NET Application Services from Silverlight

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.

Posted: Thursday, July 12, 2007 2:54 PM by MikeOrmond
Filed under: ,

Comments

Mike Ormond's Blog said:

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

# July 12, 2007 10:29 AM

Noticias externas said:

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

# July 12, 2007 10:33 AM

Mike Ormond's Blog said:

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

# July 12, 2007 11:34 AM

Noticias externas said:

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

# July 12, 2007 12:45 PM

WynApse said:

Silverlight Cream for July 12, 2007

# July 12, 2007 11:57 PM

Jason Haley said:

# July 13, 2007 10:23 AM

Jason Haley said:

# July 13, 2007 10:29 AM

Eric Griffin's Blog said:

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

# July 13, 2007 7:13 PM

pkellner said:

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

# July 16, 2007 9:26 AM

MikeOrmond said:

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

# July 16, 2007 9:45 AM

Mike Ormond's Blog said:

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

# July 18, 2007 5:01 AM

Noticias externas said:

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

# July 18, 2007 6:08 AM

Mike Ormond's Blog said:

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

# July 20, 2007 5:13 PM

Noticias externas said:

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

# July 20, 2007 5:55 PM
New Comments to this post are disabled
Page view tracker