Service Address Location via Silverlight

by kevin 4/7/2008 5:06:00 PM

Cross-domain WCF service calls from Silverlight are nice to have. But I don't see them being the norm for most Silverlight controls mainly because most of the time, we'll want to keep the services exposed from the web tier in lock-step with the Silverlight controls that consume them from a deployment standpoint. The simplest way to do that is to treat the WCF calls that come from the browser like any other web page or callback that the browser might consume. This will avoid having to inject a location into the container (page) for the Silverlight control or from having to use an external service locator to find it.

OK, so assuming you have a rule requiring that every service object will be located relative to the pages hosting a Silverlight control, this little bit of Silverlight code will build an EndpointAddress that targets a service called MyService.svc in the same virtual directory:

// connect back to the MyService peer of the containing page
string myUri = HtmlPage
.Document.DocumentUri.AbsoluteUri;
string svcAddress = String.Format("{0}/MyService.svc", myUri.Substring(0, myUri.LastIndexOf('/'
)));
EndpointAddress epAddress = new EndpointAddress(svcAddress);

You'll need to import the System.Windows.Browser namespace to use the HtmlPage class in this way. As you can imagine, the static Document property on the HtmlPage gives you almost full access to the DOM for the containing page. We're just using the DocumentUri property of it to find out where the hosting page emanated from, lopping off the page name and adding the service (page) name. When you're debugging with the Cassini web server which assigns port addresses dynamically, this is a nice way to make sure that you always connect back to the same server instance that served the current page.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C# | Silverlight | Software Development | WCF

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/28/2008 1:53:50 AM

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen


Kevin's on Twitter / FriendFeed

W. Kevin Hazzard Welcome to Kevin Hazzard's Blog. Kevin is a Software Architect, Professor and Microsoft MVP specializing in C#, WCF, Silverlight and IronPython.

View Kevin Hazzard's profile on LinkedIn
Microsoft MVP Award When a problem comes along, you must flip it!

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in