Calling a service relative to the Silverlight XAP (improved)
less than 1 minute read
In my previous post I showed a way to call a service defined with a full path, as created by the designer, relative to that of the location of your XAP, by means of an extension to a user control. I developed that during a training and while blogging it I alreadly realized that there was room for improvement. And here it is:using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows;
namespace LocalJoost.Utilities.Silverlight
{
public static class ServiceEndpointExtension
{
public static void MakeRelative(
this ServiceEndpoint endpoint)
{
var clientbinLocation =
Application.Current.Host.Source.ToString().Substring(0,
Application.Current.Host.Source.ToString().LastIndexOf("/"));
var rootLocation =
clientbinLocation.Substring(0,
clientbinLocation.LastIndexOf("/"));
endpoint.Address = new EndpointAddress(
new Uri(string.Concat(rootLocation,
endpoint.Address.Uri.AbsolutePath)));
}
}
}
In stead of extending the user control I now extend the ServiceEndpoint. If you now want to call a service that is sitting in the root of your web application that is hosting you can call it like this:var client = new CloudMapperDataServiceClient();
client.Endpoint.Address.MakeRelative();
Substitute "CloudMapperDataServiceClient" by your own client proxy class and you are go for launch (Sorry, currently listening to wechoosethemoon.org ;-)