14

I'm using a wcf service with my silverlight application. The location of the wcf service is stated in the ServiceReferences.ClientConfig file, and have to be changed to location where the application is installed.

However this file is included in the xap file, and is not something that can be easily changed when the application is deployed. Is there another way of doing the reference to the wcf service from the silverlight application? Or how do you change the ServiceReferences.ClientConfig in the xap file?

4 Answers 4

8

There may be better ways, which I'm open to using but this works for me and it's flexible.

In your Web Application's Web.config, add a variable in AppSettings and store the base URL, notice that I'm not storing the location of the SVC file, I will append that later. This is because I have multiple SVC that I usually point to. You may choose to do it differently.

 <appSettings>
    <add key="ServiceURI" value="http://localhost:64457/"/>
 </appSettings>

In my Web Application's Web Page, add a param called InitParms, this allows you to add a list of key, pair values (seperated by comma that will be read by the XAP file)

<div id="silverlightControlHost">
    <object data="data:application/x-silverlight," type="application/x-silverlight-2"
        width="100%" height="100%" ID="Xaml1" >
        <param name="InitParams" value="ServiceURI=<%= ConfigurationManager.AppSettings("ServiceURI") %>" />

In the Silverlight App.xaml.vb, load all the InitParms into a Resource or where ever you want

    Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
    If e.InitParams IsNot Nothing Then
        For Each k As Generic.KeyValuePair(Of String, String) In e.InitParams
            Me.Resources.Add(k.Key, k.Value)
        Next
    End If

Then in any of my XAML files I can initialize the service with the configured URI, I have a method like this

Private Sub InitializeService()
    Dim uri As String = App.Current.Resources("ServiceURI")
    If uri Is Nothing OrElse uri = String.Empty Then
        'if there is no value added in the web.config, I can fallback to default values
        _client = New ServiceClient
    Else
        'Notice I hardcoded the location of the SVC files in the client and append there here, you may choose not to do this
        Dim uri_withservice As String = uri & "svc/secure/Service.svc"
        _client = New ServiceClient("CustomBinding_Service", New EndpointAddress(uri_withservice))
    End If
End Sub
7

Excellent, with these suggestions, I managed to get my WCF ServiceReferences.ClientConfig data to be dynamically altered at application startup, with the Service URI read from web.config. This is possible by using the "web.config transformations" in VS2010.

Here a sample web.config.debug, showing how the ServiceURI is replaced when I choose "publish" for my web site.

<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
     <...>
  </connectionStrings>

  <appSettings>
    <add key="ServiceURI" value="http://my.location.com/myService.svc"
         xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

In my base web.config I then have the same key/value, pointing to the local service. No need to remember to change the ServiceURI every time i deploy to test/production. Great, I've been looking for that for a while now.

2

Found a solution in this blog.

http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=132

Here the wcf service endpoint is calculated from the location of the silverlight application

1
  • This solution is the best as far as I'm concerned. Any other hardcoded solution does not work for me.
    – helios456
    Feb 25, 2013 at 18:59
2

The solutions presented here all impractical in the sense that you modify your application to adapt to your config settings. This blog entry nailed it.

1
  • I found this to be the best solution as it mirrors the xml transformations that can be applied to the web.config on deploy
    – Calanus
    Jan 7, 2013 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.