views:

26

answers:

2

I am developing a silverlight 4.0 application which communicates with a WCF service.

The WCF configuration (endpoint, ...) is contained in the ServiceReferences.ClientConfig file.

However, I would like the silverlight application to get this config from the web server as I don't want to recompile the application or fiddle with the XAP file (I know it's a zip file) since the users are only allowed to configure the web server and this config would be different per server/site.

Is there a way for a silverlight application to receive some kind of info from the web server or to get such data from it?

A: 

You can certainly override the endpoint. You can specify a new endpoint when you create your servicehost object to consume your service (see this link).

You will obviously need a root service that does not change. That would provide the endpoints on a per-user basis (otherwise the end-points would need to be user settings created and stored in the client app).

Enough already
A: 

Ok, after much searching around the net I was able to code the following solution by aggregating the different pieces of information I found.

First, Silverlight 4 (like SL3) use a different mechanism for creating the silverlight control in the web page on the client. It uses the < object > tag.

To pass initialization parameters to a silverlight application you just need to add

<param name="initParams" value="key1=value1,key2=value2" />

to the page.aspx file (from the web project) under the object tag and the SL app will receive those 2 parameters at startup.

For example, using the default aspx page generated from VS2010:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/MyApp.xap"/>
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="4.0.50401.0" />
    <param name="autoUpgrade" value="true" />
    <param name="initParams" value="key1=value1,key2=value2" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=4.0.50401.0" style="text-decoration:none">
        <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
    </a>
</object>

To access the parameters in the SL app, you just have to code the following in the App.xaml.cs file:

private void Application_Startup(object sender, StartupEventArgs e)
{
    var builder = new StringBuilder ();

    foreach (String key in e.InitParams.Keys)
        builder.AppendFormat ("from InitParams: {0} = {1}", 
                              key, e.InitParams[key]).AppendLine ();

    HtmlPage.Window.Alert (builder.ToString ());

    // Other code...
}

For now, this only allows static values for the parameters. To have dynamic values you just have to change the initParam line to:

<param name="initParams" value="<%= string.Format("WCFReferenceURL={0}", ConfigurationManager.AppSettings["WCFReferenceURL"])%>" />

to get the values from the configuration file on the web server. ;)

I hope this helps some poor soul out there!

Stecy