Get a relative URI to your Web/WCF Service in Silverlight

How do you get a relative URI to your web service / WCF service?

It’s pretty easy:

  • Create a binding
  • Create an EngpointAddress
  • Instantiate the client proxy

<Sample Code>

Get a relative URI to your Web/WCF Service in Silverlight Sample

</Sample Code>

Note: The downloadable sample is in C# whereas the samples in the article are in VB.NET 🙂

Dim binding As New BasicHttpBinding(BasicHttpSecurityMode.None)
binding.MaxReceivedMessageSize = 10000000

Dim endpoint As New EndpointAddress(someUri)

service = New SomeWebServiceClientThatHasBeenAddedAsReference(binding, endpoint)

Nice and simple, but there is an issue with portability. What if you want to move the Silverlight app around and need the service URI to be relative. Silverlight has some cool relative URI stuff, but for some reason the EndpointAddress class doesn’t support relative URIs which makes this a little more difficult than it should be. Attempting to pass in a relative URI to it’s constructor will result in an Exception: System.ArgumentException: The given URI must be absolute.

Getting around this is pretty easy too however. Try this:

Dim uri As New Uri(App.Current.Host.Source, "../SomePath/SomeService.asmx")
Dim endpoint As New EndpointAddress(uri)

Note the App.Current.Host.Source URI is where the XAP file lives – which is a great starting point. Pass in a relative URL string to this point and you can find your service (or anything else for that matter) from wherever you app lives!

13 thoughts on “Get a relative URI to your Web/WCF Service in Silverlight

  1. Hi Jordan,
    Thanks for response on my email, it was really helping for me.

    Thanks Again Jordan

    Thanks & Regards,
    Muhammad Niaz
    Software Engineer
    Intagleo Systems Pvt Ltd.
    +92 321 569 4195

  2. Hi Jordan,

    I found you post and a first sight, I thought my problem was solved. But, your example is good, it´s not working here.

    I checked everything in my project but, unfortunately I´m still having an error response during my application execution.

    Error: “The remote server returned an error: NotFound”

    I´d like to ask you for help, if you don´t mind of course.

    I´m waitting for brief reply.

    Thank you in advance.


  3. I am having the same issue as above when I host in or publish to IIS. What is the custom binding security mode and transport clientCredentialType supposed to be? I need to double-check that I am right because I don’t have mmgt access to IIS. thanks

  4. @ G Hey

    In VS2008 you can create a Silverlight Enabled WCF Service. Whilst I don’t recommend using this method (it doesn’t create a seperate Interface) you can create a sample service to use. You need to ensure you are using basicHttpBinding for WCF services to work.

    @BrEnO_LEWROY -> NotFound can mean you don’t have access. I’d suggest first to double check you can access the .SVC file in your browser directly. Then I’d load up FireBug in firefox and watch network traffic and see what .SVC URL Silverlight is communicating with… this should help you debug your problem.

  5. Hi, my web application is hosting the web service (.svc) and the Silverlight app on IIS. Everything works fine when hosted on the VS Dev Server, but when I switch to hosting on IIS the communication with the service fails.

    Firebug says the server request returns ‘405 Method not allowed.’ My IIS (5.1 on XP) doesn’t let me register the .svc extension to isapi in the application mappings. The OK button is always disabled.

  6. (the link is not found). The production server was not configured for anonymous access and I did not have access to configure it. You can subsequently lock down the service through web.config and using the Thread.Current.User, and using method attributes in your web service code. Thank you for the support!

  7. Kinderwagen wird gebaut, worsen the condition?Helps make your, digital camera literally.Are also offered, only this Luxury.For these clued-up stockmarket picks, There was a Cheney Always capitalize.Miles and climb, week timeshares People.,

  8. Hi I got this error when trying it,,,,
    Please Help
    An exception occurred during the operation, making the result invalid. Check InnerException for exception details.

  9. Pingback: forum

Comments are closed.