Dynamically changing url/endpoint for WCF service in Silverlight

Posted: June 5, 2010 in Silverlight
Tags: ,

When developing a Silverlight application one of the main challenge we would face is to change the url/endpoint address of the WCF service. I just wanted to share how I have solved this problem in one of my project.

First, besides thesolution is able to change the url/endpoint dynamically, it’s also able (or actually required) to take the path to the WCF in as a InitParameter. Please note, that we are taking a relative path from the root here. Which means something like /Services/PeopleService.svc. Somethings that start with a /. This tells the browser that we want to start from the root of the current domain.

Here you can also see how we can send in the relative path for the service as a InitParameter.

Let’s start in App.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
//Insert InitParams into Global Resources
foreach (string key in e.InitParams.Keys)
{
this.Resources.Add(key, e.InitParams[key]);
}

this.RootVisual = new Page();
}

We start out by taking all the potential InitParams and put them into the global Resources for the application. You could also just pass the Dictionary to the Page class, but I’ll do it like this in this case as I need to access the resources from different places in the application.

If we look in the web project in the HTML we see how and what we pass in here: Default.aspx
asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/Dummy_WCF_Application.xap"
MinimumVersion="2.0.31005.0" Width="100%" Height="100%"
InitParameters="DummyServicePath=/Services/DummyWCFService.svc"

Ok, so now we have the initparams inside our silverlight application inside the resources. Let’s use it to set the endpoint of our application. This can be done anywhere you like, in my case I just have a DataProvider class and this is where my code is: DataProvider.cs

public class DataProvider
{
public event EventHandler GetDataCompleted;

private WCFServiceClassName service;
private const string DummyServicePathKeyName = "DummyServicePath";

public DataProvider()
{
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress;

if (App.Current.Resources.Contains(DummyServicePathKeyName )
&& !String.IsNullOrEmpty
(App.Current.Resources[DummyServicePathKeyName ] as string))
{
string relativePath =
App.Current.Resources[DummyServicePathKeyName ] as string;
string completePath = App.Current.Host.Source.Scheme
+ "://" + App.Current.Host.Source.Host
+ ":" + App.Current.Host.Source.Port + relativePath;
endpointAddress = new EndpointAddress(completePath);
service = new ChannelFactory
(basicHttpBinding, endpointAddress).CreateChannel();
}
else
throw new Exception("Missing init param named: \""
+ MapServicePathKeyName
+ "\" which is a relative Url to the WCF service.");
}
}

This is not a completely working sample, as I have removed curtain things to keep it simple. The most important part if the code above is actually this:

string relativePath = App.Current.Resources[DummyServicePathKeyName] as string;
string completePath = App.Current.Host.Source.Scheme
+ "://" + App.Current.Host.Source.Host
+ ":" + App.Current.Host.Source.Port + relativePath;

Here we create the actual complete url used to access to WCF service with.

App.Current.Host.Source.Scheme: Returns https or http

App.Current.Host.Source.Host: Returns the host ex. “laumania.net” or “silverlight.laumania.net” if you were on a subdomain.

App.Current.Host.Source.Port:
Returns the port number, typically 80, but could be anything actually. If the scheme were https the port would normally be 443 etc.

The code above would generate a url, if ran on this actual page:

Now you should be able to make your own WCF service that you can change the endpoint of, in code.

Hope you find this useful.

Advertisement
Comments
  1. [...] Dynamically changing url/endpoint for WCF service in Silverlight June 2010 3 [...]

  2. Tony A says:

    This can’t work – ChannelFactory does not have a method ‘createchannel()’ – only when you provide an interface type (ie. ChannelFactor) does this method appear?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s