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
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!